在 charge.succeeded 后,webhook 中的费用扣除和订阅分配之间的条纹区分

Stripe differentiation between charge deduction and Subscription assignment in webhook upon charge.succeeded

纠结了两天,我想区分订阅分配和手动从Stripe扣费。 charge.succeeded webhook 被调用了两次。在 webhook 调用中,我需要区分特定金额的订阅分配和费用扣除。

订阅分配使用以下代码。

$subscription = \Stripe\Subscription::create(array(
                                                "customer" => $customer_id,
                                                "plan" => $stripe_plan_id,
                                                ));

费用扣除使用以下代码。

$charge = \Stripe\Charge::create(array(
                                                'amount' => $price ,
                                                'currency' => 'usd',
                                                'customer' =>  $customer_id
                                                    )
                                    );

如果有人有任何想法,请提供方法。谢谢!!

收到 charge.succeeded event, you can extract the charge object and check the charge's invoice 属性后:

// 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
    }
}

请注意,从技术上讲,费用可以链接到发票但不能链接到订阅,以防您 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 属性以查看它是否 null