I'm using Stripes PHP SDK in an app that has a subscription and one-off charges.
I am tracking the following events via a webhook:
// Handle the event
switch ($event->type) {
// Fired for every payment, including subscriptions and one-offs
case 'payment_intent.succeeded':
$payment = $event->data->object;
$this->handleOneOffPaymentSuccess($payment);
break;
// When a one-off payment fails
case 'charge.failed':
$payment = $event->data->object;
$this->handleOneOffPaymentFailed($payment);
break;
case 'payment_intent.payment_failed':
$payment = $event->data->object;
$this->handleOneOffPaymentFailed($payment, 'The payment was not authorized (#Virtuoso-3)');
break;
// Subscription handling
case 'invoice.payment_succeeded':
$payment = $event->data->object;
$this->handleSubscriptionPaymentSuccess($payment);
break;
// Subscription failure
case 'invoice.payment_failed':
$payment = $event->data->object;
$this->handleSubscriptionPaymentFailed($payment);
break;
// ... handle other event types
default:
// Unexpected event type
http_response_code(400);
exit();
}
When a one-off payment is made I get a payment_intent.succeeded. This has my metafield data in, so I can mark the item as purchased. All good.
When I make a subscription I get a invoice.payment_succeeded event. This has my metafield data in, so I can mark the subscription as paid. Also all good.
However, along with the subscription I'm getting a payment_intent.succeeded event. I presume this is because there's a charge taken. I'm alright with that, but it doesn't contain any of the metafield data from when I created the subscription, so I have no way of tracking what it's for. At the moment it's just failing, and that doesn't seem right.
Here's how I create the subscription:
$subscription = \Stripe\Subscription::create([
'customer' => $payment_method->stripe_customer_id,
'items' => [
[
'plan' => App::environment('production') ? $user->userType->stripe_plan : $user->userType->stripe_plan_test,
],
],
'expand' => ['latest_invoice.payment_intent'],
'metadata' => [
'internal_subscription_id' => $internalSubscription->id,
'user_id' => $user_id,
]
]);
Ideally I'd want that user_id to be part of the charge, then I can just log it against the user and show it to them along with their one-off charges.
Any ideas where I'm going awry?