I am struggling for this for two days, I want to differentiate between subscription assignment and manually deducting the charge from Stripe. charge.succeeded webhook is called both times. In webhook call, I need to differentiate between Subscription assignment and Charge deduction for a particular amount.
Subscription Assignment is using below code.
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer_id,
"plan" => $stripe_plan_id,
));
And charge deduction is using below code.
$charge = \Stripe\Charge::create(array(
'amount' => $price ,
'currency' => 'usd',
'customer' => $customer_id
)
);
If anyone has any idea please suggest the way. Thank you !!
Upon receiving the charge.succeeded
event, you can extract the charge object and check the charge's invoice
attribute:
// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);
if ($event_json->type == "charge.succeeded") {
$charge = $event_json->data->object;
if ($charge->invoice == null) {
// One-off charge
} else {
// Subscription charge
}
}
Note that technically, a charge can be linked to an invoice but not to a subscription, in case you created an invoice manually. If you need to distinguish in this case, you'll need to use the invoice's ID to retrieve the invoice, and check the invoice's subscription
attribute to see if it's null
or not.