Stripe PHP创建客户并添加订阅

zazmityj  于 2023-05-27  发布在  PHP
关注(0)|答案(4)|浏览(255)

旧的方法如下

$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 Jmeter 板中,我创建了一个产品,它是按月订阅的,它有一个ID,如prod_BlMuxdEQJSxfKJ,所以我猜我需要以某种方式将该ID作为一个项目传递进来?

rta7y2nd

rta7y2nd1#

我建议您使用read about Prices,计划的继任者,但您也可以向订阅创建请求提供现有计划,如plan_123,它将转换为您的价格:

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

您不能在此处直接提供产品,因为产品不直接与任何金额或时间间隔相关联。您需要使用API或 Jmeter 板为这些产品创建价格。
创建订阅时,您可以选择使用price_data(API文档)并引用要使用的产品来定义定期定价:

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

6ojccjat2#

感谢Nolan,看起来您需要获取 Jmeter 板中提供的产品定价API ID。
下面是更新后的代码

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

这里的价格ID是附加的产品,这是一个订阅的客户。

6qqygrtg

6qqygrtg3#

如果你正在使用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 secret的帮助下处理支付。

nafvub8i

nafvub8i4#

$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,
                    ]],
                ]);

相关问题