测试模式创建订阅:https://dashboard.stripe/dashboard

选择测试模式 ,添加产品

1. 安装Stripe

composer require stripe/stripe-php

 2. 获取密钥

https://dashboard.stripe/test/apikeys

 

 3. 创建产品及订阅计划

 

4. php 代码

方式1:

创建一个订阅,将经常性价格与客户相结合。要将一次性购买与重复购买捆绑在一起,您可以使用add_invoice_items:官方文档:Subscriptions | Stripe Documentation

public function actionStripeSub()
{
    \Stripe\Stripe::setApiKey($stripe_set['key']);
    $subscription = \Stripe\Subscription::create([
        'customer' => 'cus_M6xxxxxxxxx', // 客户
        'items' => [[
            'price' => $price_id, // 订阅价格id
        ]],
    ]);
    return $subscription;
}

此处的customer 必须传入客户id,客户id不知如何获取实现所以如下,参考:api - Stripe PHP Create customer and add subscription - Stack Overflow

代码:

public function actionStripeSub2()
{
    \Stripe\Stripe::setApiKey($stripe_set['key']);
    $customer = \Stripe\Customer::create();
    $subscription = \Stripe\Subscription::create([
        'customer' => $customer->id, // 客户
        'items' => [[
            'price' => $price_id, // 订阅价格id
        ]],
//        'trial_end' => $time + 86400 * 3, // 试用期的结束时间
//        'trial_period_days' => 3, // 试用期天数 同上取1个
        'payment_behavior' => 'default_incomplete',
        'expand' => [ "latest_invoice.payment_intent" ],
    ]);
    // $subscription->latest_invoice->hosted_invoice_url stripe跳转支付url
    // $subscription->latest_invoice->payment_intent->id 是识别支付与通知关系的唯一凭证
    return [
        'url' => $subscription->latest_invoice->hosted_invoice_url,
        'id' => $subscription->id,
        'payment_intent' => $subscription->latest_invoice->payment_intent->id,
    ];
}

此处返回的结果过长 就不再粘贴了

根据请求返回的$subscription->latest_invoice->hosted_invoice_url,让客户去订阅

官方提供得测试账号:Testing | Stripe Documentation

支付成功会自动跳转到success.html

支付页面:

方式2:集成方式创建订阅

此方法有个问题在于没有返回payment_intent,所以无法关联订单,有解决办法的小伙伴可以下方留言,直接上代码

官方文档地址:https://stripe/docs/billing/subscriptions/build-subscriptions?ui=checkout#create-session

public function actionStripeSub()
{
    \Stripe\Stripe::setApiKey($stripe_set['key']);
    $checkout_session = \Stripe\Checkout\Session::create([
        'line_items' => [[
            'price' => $price_id,
            'quantity' => 1,
        ]],
        'mode' => 'subscription',
//            'trial_end' => $time + 86400 * 3, // 试用期的结束时间
        'success_url' => $stripe_set['success_url'] . '?session_id={CHECKOUT_SESSION_ID}',
        'cancel_url' => $stripe_set['cancel_url'],
        'allow_promotion_codes' => true,
        'payment_method_types' => ['card'],
        'automatic_tax' => [
            'enabled' => true,
        ],
    ]);
    return $checkout_session;
}

 var_dump($checkout_session)返回的结果

object(Stripe\Checkout\Session)#175 (40) {
  ["id"]=>
  string(66) "cs_test_b1B590Ovtl3soto16JcWSuBX0g25A4gPiRddy1BaoRfziQ3VRKxqYDBuVj"
  ["object"]=>
  string(16) "checkout.session"
  ["after_expiration"]=>
  NULL
  ["allow_promotion_codes"]=>
  bool(true)
  ["amount_subtotal"]=>
  int(999)
  ["amount_total"]=>
  int(999)
  ["automatic_tax"]=>
  object(Stripe\StripeObject)#186 (2) {
    ["enabled"]=>
    bool(true)
    ["status"]=>
    string(24) "requires_location_inputs"
  }
  ["billing_address_collection"]=>
  NULL
  ["cancel_url"]=>
  string(44) "https://apitest.xxxx/site/pay-fail"
  ["client_reference_id"]=>
  NULL
  ["consent"]=>
  NULL
  ["consent_collection"]=>
  NULL
  ["currency"]=>
  string(3) "hkd"
  ["customer"]=>
  NULL
  ["customer_creation"]=>
  string(6) "always"
  ["customer_details"]=>
  NULL
  ["customer_email"]=>
  NULL
  ["expires_at"]=>
  int(1658564740)
  ["livemode"]=>
  bool(false)
  ["locale"]=>
  NULL
  ["metadata"]=>
  object(Stripe\StripeObject)#187 (0) {
  }
  ["mode"]=>
  string(12) "subscription"
  ["payment_intent"]=>
  NULL
  ["payment_link"]=>
  NULL
  ["payment_method_options"]=>
  NULL
  ["payment_method_types"]=>
  array(1) {
    [0]=>
    string(4) "card"
  }
  ["payment_status"]=>
  string(6) "unpaid"
  ["phone_number_collection"]=>
  object(Stripe\StripeObject)#191 (1) {
    ["enabled"]=>
    bool(false)
  }
  ["recovered_from"]=>
  NULL
  ["setup_intent"]=>
  NULL
  ["shipping"]=>
  NULL
  ["shipping_address_collection"]=>
  NULL
  ["shipping_options"]=>
  array(0) {
  }
  ["shipping_rate"]=>
  NULL
  ["status"]=>
  string(4) "open"
  ["submit_type"]=>
  NULL
  ["subscription"]=>
  NULL
  ["success_url"]=>
  string(80) "https://apitest.xxxx/site/pay-success?session_id={CHECKOUT_SESSION_ID}"
  ["total_details"]=>
  object(Stripe\StripeObject)#195 (3) {
    ["amount_discount"]=>
    int(0)
    ["amount_shipping"]=>
    int(0)
    ["amount_tax"]=>
    int(0)
  }
  ["url"]=>
  string(355) "https://checkout.stripe/pay/cs_test_b1B590Ovtl3soto16JcWSuBX0g25A4gPiRddy1BaoRfziQ3VRKxqYDBuVj#fidkdWxOYHwnPyd1blpxYHZxWjA0T3xqN2xNa09NcW5MXXNWVFxIaHR2bGtjc0l0VGxSZl9LRGpQQVBwbWM8bENXbn0zSzxJZnxxckhEYG1GTlxVQHNdZj1CcEA0Vj1SUk9cTmdxVkRHaX9SNTVzaTBvYmhEbycpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPydocGlxbFpscWBoJyknYGtkZ2lgVWlkZmBtamlhYHd2Jz9xd3BgeCUl"
}

5.配置事件webhook回调

 

事件文档及说明:

https://stripe/docs/billing/subscriptions/webhooks#events

 

更多推荐

php实现 Stripe订阅