Stripe - Web Hooks - 免费试用后更改订阅计划

Stripe - Web Hooks - Change Subscription Plan After Free Trial

我正在尝试执行以下操作:

我知道我需要为此使用网络挂钩,并创建了一个测试网络挂钩来执行此操作,目前看起来像这样:

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_......");

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Do something with $event_json

http_response_code(200); // PHP 5.4 or greater

我需要监听的事件是:

customer.subscription.trial_will_end

但是我如何在网络挂钩中使用此事件来获取客户 ID,然后将他们添加到计划中同时向他们收费?

亲切的问候, 尼克

根据您具体要执行的操作,您可能根本不需要使用 webhook。

如果您想在客户订阅时收取 1 美元的安装费,然后在 3 个月内不向他们收取任何费用,然后开始向他们收取 $x/月(或任何其他时间间隔),您应该这样做:

这将导致以下结果:

  • 客户将立即支付 1 美元
  • 如果此次付款失败,则不会创建订阅
  • 如果成功,将创建订阅
  • 3 个月后,试用期结束,您的客户将开始根据计划的参数进行计费

为了回答您最初的问题,customer.subscription.trial_will_end event that is sent will include a subscription object in its data.object attribute. You can then use this subscription object to retrieve the customer ID by looking at the customer 属性。

代码看起来像这样:

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_...");

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Verify the event by fetching it from Stripe
$event = \Stripe\Event::retrieve($event_json->id);

// Do something with $event
if ($event->type == "customer.subscription.trial_will_end") {
  $subscription = $event->data->object;
  $customer_id = $subscription->customer;
}

http_response_code(200); // PHP 5.4 or greater