dongleibeng5602 2013-08-20 15:22
浏览 155
已采纳

如何在使用Stripe创建订阅后获取费用?

I am using Stripe as a payment gateway. Now there's a big problem bothers me.

I used code below to create a subscription:

<?php
require_once('lib/Stripe.php');

Stripe::setApiKey(API_KEY);

$token = $_POST['stripeToken'];

$customer = Stripe_Customer::create(array(
    "card"  => $token,
    "plan"  => $_POST['plan'],
    "email" => "fakeuser@gmail.com",
));

This works fine, but I can not get Charge ID from $customer, and I found out there's no way in Stripe API to get it.

How to get it when creating a subscription? I really need Charge ID.

  • 写回答

6条回答 默认 最新

  • dragon19720808 2013-09-07 22:00
    关注

    This is exactly what Stripe's webhooks are for. After creating a customer with an initial subscription, you'll get six webhook notifications:

    1. customer.created, with the customer data (which you already have if you're saving what the API returns)
    2. charge.succeeded (or charge.failed), which contains the initial charge data you're looking for
    3. invoice.created, which is the associated invoice
    4. invoice.payment_succeeded (or invoice.payment_failed), also telling you the status of the charge
    5. customer.card.created, with the details of the new card
    6. customer.subscription.created, with the details of the customer's subscription.

    Stripe's API, like many APIs and many payment solutions, is built to be used with webhooks. If you're not taking advantage of webhooks, you're going to be missing functionality, and you're probably working too hard for what can be done without webhooks.

    Stripe works to deliver the data to you. If you're writing code to poll Stripe, you're working way, way too hard.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?