How To Add Custom Metadata To WooCommerce Stripe Purchase

In this lunchtime #WPQuickies, I show you how to add WooCommerce customer and order data into your Stripe credit card transactions as metadata.

What Is Stripe?

Stripe is a popular payment gateway that can take online card payments directly from your WooCommerce store.

Stripe doesn’t charge a monthly or yearly fee to use their services.

PayPal can also take card payments but that happens outside your site on PayPal.com for the free version or embedded on your site with their monthly paid subscription pro versions.

Stripe also makes handles recurring billing very easy.  

Recurring payments with PayPal is not so easy to set up and manage.

Stripe also offers chargeback and fraud protection which PayPal standard does not.

Many people choose Stripe to handle their online card payments because it’s a free service.

The Stripe payment gateway is not part of the core WooCommerce app.

It is a separate free plugin that needs to be installed after WooCommerce.

How Do Stripe Payouts Work?

Stripe bundles incoming transactions together into a payout which is transferred to your linked bank account.

Payouts can vary depending on your country and the “risk” value Stripe determines your business is operating in.

Full details can be found on the Stripe documentation site at https://stripe.com/docs/payouts.

In general, payouts are delivered into your bank account between 2 and 7 days.

A Reconciliation Problem

Because multiple Stripe transactions can be lumped together in a single payout, this can make it challenging to reconcile invoices in your accounting system.

This is especially true if you have a large volume of low-cost transactions.

You have to log in to your Stripe dashboard and try to identify individual transactions.

The default Stripe transaction only records the date, price, website URL, email address of the customer so it can be a challenge to reconcile that transaction in your accounting system.

By default, WooCommerce adds the product short description and the order ID which can be helpful but we can do better.

Stripe Transactions

Let’s look at a default Stripe transaction.

WooCommerce Stripe Transaction

The transaction has a payment details section where we can see the WooCommerce short description and order number.

Scrolling down, the transaction also has a Metadata section.

This section contains the customer name, customer email address, the WooCommerce Order ID and the website URL.

Having the order ID is helpful and we can log in to our WooCommerce store to see exactly what was purchased.

This works OK if for single purchases but what happens if the customer has purchased many items and you need to reconcile each separately in your accounting software?

That’s going to take some time going back and forth.

An easier way is to add more data to the Metadata section on the Stripe transaction.

Additional Stripe Metadata

Let’s have a look at the metadata section from a Stripe transaction that has been fed more data from a WooCommerce store.

Custom Stripe metadata

The Payment section stays the same but now we can see additional fields in the metadata for the Stripe transaction.

For this transaction, we are passing in the customers full name, the customer phone number and a list of line items from the order, including line item totals.

The example above shows only one line item but the code will store each line item separately in the metadata allowing you to see exactly what the customer purchased on your WooCommerce store.

That means you shouldn’t need to go back and forth between your store and Stripe to reconcile all your payout transactions.

You can add any customer or product data you need to the Stripe metadata section of the transaction.

The Code

You can find the full code at https://gist.github.com/DeveloperWil/0decf69258a03d6a18eea8e8a6eac252

Place this code in your theme’s functions.php file.

The code uses the WooCommerce Stripe plugin filter ‘wc_stripe_payment_metadata’ which provides three arguments 1) Stripe metadata array, 2) The Order object and 3) Prepared Stripe source object (the data object being sent to Stripe).

In this code, we get the order data object and extract the billing company name if it exists as well as the customer name and phone number.

The next bit of code loops through all the order line items, concatenating the line item number, product name, quantity and line item total together into a single line.

The $metadata array is where we store this additional information, which is then passed to Stripe during the payment process.

WooCommerce Customer and Product Data

For this demo, I just inserted added some basic WooCommerce customer and order data but you can add extra fields if needed.

I’ve added some sample data fields in the Gist below for your reference.

You should also have a look at the WC_Order object in the WooCommcerce documentation, especially the $data property
https://woocommerce.github.io/code-reference/classes/WC-Order.html#property_data 

Conclusion

So there you have it – a way to add WooCommerce order and customer data to a Stripe transaction metadata.

#WPQuickies

Join me every Thursday at 1 pm Sydney time for some more WPQuickies – WordPress tips and tricks in thirty minutes or less.

Broadcasting live on YouTube and Facebook.

Suggest a #WPQuickies Topic

If you have an WordPress topic you’d like to see explained in 30 mins or under, fill out the form below.

https://forms.gle/mMWCNd3L2cyDFBA57

Was this article helpful?
YesNo

Keep In Touch

Wil

Wil is a dad, WordPress consultant, WordPress developer, business coach and mentor. He co-organizes the WordPress Sydney meetup group and has been on the organising committee for WordCamp Sydney since 2014. He speaks at many special events and contributes to the WordPress open source project. His likes are chillies, craft beer and electrogravitics.