<leftside.dev>
<menu>
</menu>
<title>
WordPress CDN - Stripe Integration
</title>
<content>

The piece that I've been having the most trouble with is the Stripe integration. I've worked on it historically, and have as-yet been unable to figure out how to achieve the functionality I want.

Background

To give some background on my problem, and the plugin that I'm developing: It's a CDN plugin. It replaces the URLs for core assets with a CDN URL (In the future we'll look at expanding to plugin and theme assets) that we can heavily cache and globally distribute. The plugin costs a nominal amount per year, to cover the hosting of the static assets.

A member can have multiple subscriptions to the plugin on a per-domain basis (if they have two domains, that's two subscriptions), each being billed and managed independently.

On the WordPress side, the plugin makes an API call to our server with the domain name of the site and their WP version. The server checks whether that domain has an active subscription, and if so returns a set of core urls that the site can use in place of the default assets.

Stripe Integration Issue

The problem I'm having with the Stripe integration is that I'm using Stripe Checkout to generate the subscription, and I can't seem to add the domain that's linked to the subscription to the invoices that are generated. This is crucial; users need to know what domains they are being billed for.

Stripe provides a few methods for adding this information into a subscription:

  • Metadata associated with the Subscription itself
  • Descriptions, Metadata and Custom Fields associated with the Invoices linked to the subscription

I've been successful in setting the metadata on the subscription, but this is not appearing on the auto-generated invoices.

Here's what I plan to try:

  • Manually create an invoice, and assign some Custom Fields to it. See if I can create the invoice I want manually.
  • Create a subscription that renews every day (or more frequent), so that I can test the invoice creation on renewal. It might just be that I can't add the custom fields on the initial invoice, but I can on subsequent invoices.

Once I know what's going on there, then I can assess what I need to change. I'm hopeful that I can continue to use the Stripe Checkout functionality, as that gives me a lot of the checkout and "my account" functionality that I don't want to have to build myself.

Stay tuned, I'll keep you updated...

Update 19th November 2022:

One of my main issues is that the custom fields are not added to the initial invoice, and I appear unable to do anything about that because the invoice is already finalised when the user comes out of the checkout process. However, I have found a setting that may be helpful here: payment_behaviour="default_incomplete" (official documentation). With this setting, it appears that a user can complete the checkout and the invoice will not automatically be finalised. With this in place, I should be able to add my custom fields to the invoice, then finalise the invoice.

This excites me, as it sounds like the goods. I'll try that out.

Update 22nd November 2022:

Creating an invoice manually works. I can add a "Domain" custom field and set the user's domain, and all that appears on their invoice. It's not the end-solution as I can't manually create invoices every time.

Unfortunately, the above solution (regarding payment behaviour) does not work. That field is only available when creating subscriptions directly through the API, whereas I'm creating the subscription indirectly through the checkout process.

Another option that I can explore is creating a new product, and associated price, for each checkout. This solves a few problems, as it would be clearer on the invoices (it's right there as a line item, rather than hidden in the header), and I wouldn't need as many webhook integration points (renewal invoices would automatically include this product name).
The draw-back here is one of data management on the Stripe side: I would need to create a product for each checkout session, even those that are abandoned. This creates a (potentially) huge amount of data that's either unused or invalid.

Ideally, I stick with the one product solution, and figure out a way to add the domain. At this point it appears that I will have to move away from the Stripe Checkout functionality, and build a direct integration, to achieve the functionality I want. For now, I think I'll just leave the invoices as not having the domain listed on them. I can circle-back to this problem in the future and build out a deeper integration with Stripe to achieve what I want.

</content>
</leftside.dev>