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

时间:2015-12-07 11:46:28

标签: php stripe-payments

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

  • 一次性费用1英镑,持续90天
  • 试用结束后 - 将用户添加到新计划
  • 添加到新计划时再次向用户收费

据我所知,我需要使用Web钩子,并创建了一个测试Web钩子来执行此操作,目前看起来像这样:

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

但是,如何在Web挂钩中使用此事件来获取客户ID,然后将它们添加到计划中,同时为其收费?

亲切的问候, NIC

1 个答案:

答案 0 :(得分:3)

根据您想要做什么,您可能根本不需要使用webhook。

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

这将产生以下结果:

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

要回答您的初始问题,发送的customer.subscription.trial_will_end事件将在其subscription object属性中包含data.object。然后,您可以通过查看customer属性来使用此订阅对象来检索客户ID。

代码看起来像这样:

// 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
相关问题