Forum

Thread tagged as: Question, Shop

Passing more order information to Stripe

I'm currently using the following on my cart page:

            if (perch_member_logged_in() && perch_post('stripeToken')) {

            $return_url = '/pay/success';
            $cancel_url = '/pay/error';

            perch_shop_checkout('stripe', [
            'return_url' => $return_url,
            'cancel_url' => $cancel_url,
            'token' => perch_post('stripeToken')
            ]);
            }

When the orders show up in Stripe they come through as "Order X" with just the customers email address. How can I pass over more information? I'd like to pass over the SKU of the items ordered, for example. I've also noticed that the customer address doesn't get passed over, so in Stripe you get:

"object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": "MK17 6UT",

The postcode is there because I have that as an optional extra field when the customer enters their card details. How do I get the rest of the info filled in?

Stephen Turvey

Stephen Turvey 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you can set metadata as an array of key/value pairs.

perch_shop_checkout('stripe', [ 
'return_url' => $return_url, 
'cancel_url' => $cancel_url, 
'token' => perch_post('stripeToken') 
'metadata' => [
    'foo' => 'bar',
],
]);

Great. Here's what I've got so far:

$customer_name = perch_member_get('first_name');
            $customer_surname = perch_member_get('last_name');
            $customer_email = perch_member_get('email');
            $customer_order = ??;

            if (perch_member_logged_in() && perch_post('stripeToken')) {

            $return_url = '/pay/success';
            $cancel_url = '/pay/error';

            perch_shop_checkout('stripe', [
            'return_url' => $return_url,
            'cancel_url' => $cancel_url,
            'token' => perch_post('stripeToken'),
            'metadata' => [
                'firstname' => $customer_name,
                'surname' => $customer_surname,
                'email' => $customer_email,
                'purchase' => $customer_order,
                ],
            ]);
            }

For my $customer_order is there a way I can pass in what the customer has in their cart?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, everything you need should be available from perch_shop_cart() with skip-template.

It works for the 'perch:shop' cart values but I can't seem to be able to grab the 'perch:cartitem' values which is what I need (title/description). Here's what I've got:

// pass on to stripe
    $customer_firstname = perch_member_get('first_name');
    $customer_surname = perch_member_get('last_name');
    $customer_name = $customer_firstname . ' ' . $customer_surname;
    $customer_email = perch_member_get('email');

    $cart = perch_shop_cart(['skip-template'=>true]);
    $customer_order = $cart['title'];

    // stripe urls
    if (perch_member_logged_in() && perch_post('stripeToken')) {

    $return_url = '/pay/success';
    $cancel_url = '/pay/error';

    // stripe data
    perch_shop_checkout('stripe', [
    'return_url' => $return_url,
    'cancel_url' => $cancel_url,
    'token' => perch_post('stripeToken'),
    'metadata' => [
        'email' => $customer_email,
        'name' => $customer_name,
        'purchased' => $customer_order,
        ],
    ]);
    }
Drew McLellan

Drew McLellan 2638 points
Perch Support

No? What do you get?

The following works fine so I know I can access the template data:

$cart = perch_shop_cart(['skip-template'=>true]);
$customer_order = $cart['currency_code'];
...
<?php echo $customer_order ?>

But if I use the following nothing gets output (as if 'title' doesn't exist in the template):

$cart = perch_shop_cart(['skip-template'=>true]);
$customer_order = $cart['title'];
...
<?php echo $customer_order ?>

I'm trying to access <perch:cartitem id="title" />

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does print_r($cart) show you?

Array ( [discount_code] => [promotions] => Array ( ) [product_count] => 1 [item_count] => 1 [items] => Array ( [0] => Array ( [discount] => 0 [price_with_tax] => 300.00 [price_with_tax_formatted] => £300.00 [price_without_tax] => 300.00 [price_without_tax_formatted] => £300.00 [tax] => 0.00 [tax_discount] => 0 [tax_formatted] => £0.00 [tax_rate] => 0 [total_tax] => 0.00 [total_tax_formatted] => £0.00 [total_with_tax] => 300.00 [total_with_tax_formatted] => £300.00 [total_without_tax] => 300.00 [total_without_tax_formatted] => £300.00 [weight] => 0 [sku] => argentina_teach_learn [title] => Pay deposit: £300 [Product] => PerchShop_Product Object ( [factory_classname:protected] => PerchShop_Products [table:protected] => perch2_shop_products [pk:protected] => productID [index_table:protected] => shop_index [modified_date_column:protected] => productUpdated [deleted_date_column] => productDeleted [duplicate_fields:protected] => Array ( [productStockOnParent] => stock_location [productSlug] => slug [productStatus] => status ) [pk_is_int:protected] => 1 [date_fields:protected] => Array ( ) [api:protected] => PerchAPI Object ( [app_id] => perch_shop [version] => 1 [Lang:PerchAPI:private] => ) [db:protected] => [details:protected] => Array ( [productID] => 20 [title] => Pay deposit: £300 [sku] => argentina_teach_learn [productSlug] => pay-deposit-gbp-300-argentinateachlearn [stock_level] => [parentID] => [productVariantDesc] => [productOrder] => 1 [productHasVariants] => 0 [productStockOnParent] => 0 [productDynamicFields] => {"slug":"pay-deposit-gbp-300-argentinateachlearn","status":"1","category":["39","40"],"price":{"47":"300","_default":"300"},"sale_price":{"47":"","_default":""},"trade_price":null,"on_sale":null,"tax_group":"1","stock_status":"0","max_in_cart":null} [productTemplate] => product.html [productCreated] => 2017-01-24 21:50:53 [productUpdated] => 2017-01-24 22:07:59 [productDeleted] => [productStatus] => 1 ) [event_prefix:protected] => [suppress_events] => 1 [can_log_resources:protected] => 1 ) [qty] => 1 [id] => 20 ) ) [shipping_id] => [shipping_method] => [shipping_tax] => 0.00 [shipping_tax_formatted] => £0.00 [shipping_tax_rate] => 0 [shipping_with_tax] => 0.00 [shipping_with_tax_formatted] => £0.00 [shipping_without_tax] => 0.00 [shipping_without_tax_formatted] => £0.00 [total_items] => 300.00 [total_items_formatted] => £300.00 [total_items_tax] => 0.00 [total_items_tax_formatted] => £0.00 [total_shipping] => 0.00 [total_shipping_formatted] => £0.00 [total_shipping_tax] => 0.00 [total_shipping_tax_formatted] => £0.00 [total_items_with_shipping] => 300.00 [total_items_with_shipping_formatted] => £300.00 [total_tax] => 0.00 [total_tax_formatted] => £0.00 [total_items_discount] => 0.00 [total_items_discount_formatted] => £0.00 [total_items_tax_discount] => 0.00 [total_items_tax_discount_formatted] => £0.00 [total_items_discounted] => 300.00 [total_items_discounted_formatted] => £300.00 [total_items_discounted_with_tax] => 300.00 [total_items_discounted_with_tax_formatted] => £300.00 [total_shipping_discount] => 0.00 [total_shipping_discount_formatted] => £0.00 [total_shipping_tax_discount] => 0.00 [total_shipping_tax_discount_formatted] => £0.00 [total_tax_discount] => 0.00 [total_tax_discount_formatted] => £0.00 [total_discounts] => 0.00 [total_discounts_formatted] => £0.00 [total_discounts_with_tax] => 0.00 [total_discounts_with_tax_formatted] => £0.00 [grand_total] => 300.00 [grand_total_formatted] => £300.00 [shipping_weight] => 0.00 [tax_rate_totals] => Array ( [0] => Array ( [exchange_rate] => unknown [tax_rate] => 0 [tax_rate_formatted] => 0 % [total_tax] => 0.00 [total_tax_formatted] => £0.00 [total_value] => 300.00 [total_value_formatted] => £300.00 [reporting_tax] => 0.00 [reporting_tax_formatted] => £0.00 [reporting_value] => 300.00 [reporting_value_formatted] => £300.00 ) ) [currency_id] => 47 [currency_code] => GBP [currency_name] => Pound sterling [currency_symbol] => £ )

I can see the information I want inside that array (sku, title, slug) but how do I get it out...

Drew McLellan

Drew McLellan 2638 points
Perch Support

Looks like it's $cart['items']

If I do:

$cart = perch_shop_cart(['skip-template'=>true]);
$customer_order = $cart['items'];
...
<?php echo $customer_order ?>

That outputs "Array". How do I specify the title and sku within the items array?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd need to loop through the array and find the items you want.

Could you help me with that please? My brain is hurting trying to figure it out!

Rachel Andrew

Rachel Andrew 394 points
Perch Support

This is PHP rather than Perch specific, a Google for information about how to loop through a PHP array would be a good start.

With some help I have done it. For anyone else who needs this in future:

// pass to stripe basics
    $customer_firstname = perch_member_get('first_name');
    $customer_surname = perch_member_get('last_name');
    $customer_name = $customer_firstname . ' ' . $customer_surname;
    $customer_email = perch_member_get('email');

    // pass to stripe cart titles, skus
    $cart = perch_shop_cart(['skip-template'=>true]);
    $cart_items = $cart['items'];

    foreach($cart_items as $item) {
      $product_titles[] = $item['title'];
      $product_skus[] = $item['sku'];
    }

    $product_title = implode(', ', $product_titles);
    $product_sku = implode(', ', $product_skus);

    // stripe data
    perch_shop_checkout('stripe', [
    'return_url' => $return_url,
    'cancel_url' => $cancel_url,
    'token' => perch_post('stripeToken'),
    'metadata' => [
        'email' => $customer_email,
        'name' => $customer_name,
        'product(s)' => $product_title,
        'sku(s)' => $product_sku,
        ],
    ]);
    }

Can I also use the metadata to pass information to Paypal? The order description in paypal currently shows as "Order #X" which isn't very.... descriptive.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What result are you getting?

No result - nothing is being passed to the paypal checkout or is visible in the transaction details. I tried:

perch_shop_checkout('paypal-express', [
    'return_url' => $return_url,
    'cancel_url' => $cancel_url,
    'metadata' => [
        'email' => 'testing@testing.com',
        'name' => 'testing name',
        'product(s)' => 'testing product',
        'sku(s)' => 'testing sku',
        'description' => 'testing desc'
        ],
    ]);
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, I'll have to dig through the Omnipay driver and see if they've implemented it.

That would be great because currently in Paypal there's no way of knowing what the customer bought, only the amount they paid. You'd have to cross-check with the Perch admin to find out the purchase.

Will you post back here when you've found the answer?

Thanks.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Why do you need to know this at the PayPal end when you have it in Perch?