Stripe PHP创建客户并添加订阅

3
这种做法的老方式如下:
$customer = \Stripe\Customer::create(array(
    "description" => $domain,
    "email" => $email,
    "source" => $token,
    "metadata" => array(
        "name" => $name,
        "phone" => $phone
    ),
));

$cus = $customer->id;

\Stripe\Subscription::create(array(
    "customer" => $cus,
    "plan" => "1",
));

但是现在我在订阅创建页面上找不到“计划”选项。这是我目前的进展...

$customer = $stripe->customers->create([
  'description' => 'Description text here nomadweb.design',
  'name' => 'Sammy Malones',
  'email' => 'name@email.com',
  'phone' => '5124592222'
]);

$cus = $customer->id;

$stripe->subscriptions->create([
    'customer' => $cus,
    'plan' => '1'
]);

在API文档中指出必须使用“items”参数。 我的问题是如何使用新的api向客户添加订阅?
这是他们的代码,但我不理解。
$stripe->subscriptions->create([
  'customer' => 'cus_J34i3JonNQQXdO',
  'items' => [
    ['price' => 'price_0IQyZLH7HxDXZRHqJfpwwqBB'],
  ],
]);

https://stripe.com/docs/api/subscriptions/create

在我的 Stripe 仪表板上,我创建了一个名为“月度订阅”的产品,它有一个 ID,例如prod_BlMuxdEQJSxfKJ。因此,我猜想我需要以某种方式将该ID作为项目传递给API?


1
计划被价格所取代。https://stripe.com/docs/billing/migration/migrating-prices - ceejayoz
4个回答

4
我建议你阅读有关价格的内容,这是计划的后继者,但你也可以在订阅创建请求中提供一个现有的计划,例如plan_123,它将被转换为一个价格:
$stripe->subscriptions->create([
  'customer' => 'cus_123',
  'items' => [
    ['price' => 'plan_123'],
  ],
]);

由于产品与任何金额或时间间隔没有直接联系,因此您无法直接在此处提供产品。您需要为这些产品创建价格,可以使用API或仪表板。

创建订阅时,您可以使用price_data(API文档)和引用要使用的产品,可选择即席定义循环定价

$subscription = $stripe->subscriptions->create([
  'customer' => 'cus_123',
  'items' => [[
    'price_data' => [
      'unit_amount' => 5000,
      'currency' => 'usd',
      'product' => 'prod_456',
      'recurring' => [
        'interval' => 'month',
      ],
    ],
  ]],
]);

2
感谢Nolan的提醒,看起来您需要获取仪表板中提供的产品定价API ID。

以下是更新后的代码:

$stripe->subscriptions->create([
    'customer' => $cid,
    'items' => [['price' => 'price_0IR0OGH7HxDXZRHq3sIg9biB'],],
]);

这里的价格ID将订阅产品附加到客户身上。


0
如果您正在使用 Laravel 和 Stripe PHP SDK,则可以按照以下方式执行此操作:
\Stripe\Stripe::setApiKey(env('STRIPE_PRIVATE_KEY'));

        // Use an existing Customer ID if this is a returning customer.
        // $customer = \Stripe\Customer::create([
        //     "description" => $domain,
        //     "email" => $email,
        //     "source" => $token,
        //     "metadata" => [
        //         "name" => $name,
        //         "phone" => $phone
        //     ],
        // ]);

        $customer = \Stripe\Customer::create();

        // $customer_id = $customer->id;

        $Subscription = \Stripe\Subscription::create([
            
            'customer' => $customer->id,
            'items' => [[
                'price' => $price_id,
            ]],
            'payment_behavior' => 'default_incomplete',
            'expand' => ['latest_invoice.payment_intent'],
        ]);

        $ephemeralKey = \Stripe\EphemeralKey::create(
            ['customer' => $customer->id],
            ['stripe_version' => '2020-08-27']
        );

        // $paymentIntent = \Stripe\PaymentIntent::create([
        //     'amount' => $amount,
        //     'currency' => $currency,
        //     'customer' => $customer->id
        // ]);
          
        $response = [
            'request' => $data,
            'paymentIntent' => $subscription->latest_invoice->payment_intent->client_secret,
            'ephemeralKey' => $ephemeralKey->secret,
            'customer' => $customer->id,
            'subscriptionId' => $subscription->id,
        ];
          
        return response($response, 200);

然后在您的前端,您可以借助您的paymentIntent密钥处理付款。


0
    $stripe = new StripeClient(
           stripe_secret
       );

   $stripeToken = $stripe->tokens->create([
                    'card' => [
                        'number' => $request->card_number,
                        'exp_month' =>$request->exp_month,
                        'exp_year' =>$request->exp_year,
                        'cvc' => $request->cvc,
                    ],
    
     $product = $stripe->products->create([
                'name' => "Product Name",
            ]);
     $stripe_plan = $stripe->plans->create([
                'amount' => 60 * 100,
                'currency' => 'usd',
                'interval' => 1,
                'product' => $product->id,
            ]);
    
  
            $stripe_customer = $stripe->customers->create([
                            'name' => $request->name,
                            'email' => $request->email,
                            'source' => $stripeToken['id']
                        ]);
                        $customer_id = $stripe_customer->id;
    
     $subscription = $stripe->subscriptions->create([
                    'customer' => $customer_id,
                    'items' => [[
                        'price' => $stripe_plan->id,
                    ]],
                ]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接