doutang1873 2016-12-14 11:05
浏览 63
已采纳

charge.ucceeded在webhook中扣除费用和订阅分配之间的条带差异

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 !!

  • 写回答

1条回答 默认 最新

  • dongxia8656 2016-12-14 11:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?