当前位置:网站首页>PHP implements stripe subscription
PHP implements stripe subscription
2022-07-24 00:19:00 【Align to the right--】
Test mode create subscription :https://dashboard.stripe.com/dashboard
Choose test mode , Add product

1. install Stripe
composer require stripe/stripe-php2. To get the key
https://dashboard.stripe.com/test/apikeys

3. Create product and subscription plans


4. php Code
The way 1:
Create a subscription , Combine recurring prices with customers . We should bundle one-time purchase with repeat purchase , You can use add_invoice_items: Official documents :Subscriptions | Stripe Documentation
public function actionStripeSub()
{
\Stripe\Stripe::setApiKey($stripe_set['key']);
$subscription = \Stripe\Subscription::create([
'customer' => 'cus_M6xxxxxxxxx', // Customer
'items' => [[
'price' => $price_id, // Subscription price id
]],
]);
return $subscription;
}Here customer Must pass in the customer id, Customer id I don't know how to get the implementation, so the following , Reference resources :api - Stripe PHP Create customer and add subscription - Stack Overflow
Code :
public function actionStripeSub2()
{
\Stripe\Stripe::setApiKey($stripe_set['key']);
$customer = \Stripe\Customer::create();
$subscription = \Stripe\Subscription::create([
'customer' => $customer->id, // Customer
'items' => [[
'price' => $price_id, // Subscription price id
]],
// 'trial_end' => $time + 86400 * 3, // The end time of the probation period
// 'trial_period_days' => 3, // Days of probation Same as above 1 individual
'payment_behavior' => 'default_incomplete',
'expand' => [ "latest_invoice.payment_intent" ],
]);
// $subscription->latest_invoice->hosted_invoice_url stripe Jump payment url
// $subscription->latest_invoice->payment_intent->id It is the only voucher to identify the relationship between payment and notice
return [
'url' => $subscription->latest_invoice->hosted_invoice_url,
'id' => $subscription->id,
'payment_intent' => $subscription->latest_invoice->payment_intent->id,
];
}The result returned here is too long No longer paste
Returned upon request $subscription->latest_invoice->hosted_invoice_url, Let customers subscribe
Official test account number :Testing | Stripe Documentation
Successful payment will automatically jump to success.html
Payment page :
The way 2: Create subscriptions in an integrated way
The problem with this method is that it does not return payment_intent, Therefore, the order cannot be associated , If you have a solution, you can leave a message below , Go straight to the code
Official document address :https://stripe.com/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, // The end time of the probation period
'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) The result returned
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.net/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.net/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.com/pay/cs_test_b1B590Ovtl3soto16JcWSuBX0g25A4gPiRddy1BaoRfziQ3VRKxqYDBuVj#fidkdWxOYHwnPyd1blpxYHZxWjA0T3xqN2xNa09NcW5MXXNWVFxIaHR2bGtjc0l0VGxSZl9LRGpQQVBwbWM8bENXbn0zSzxJZnxxckhEYG1GTlxVQHNdZj1CcEA0Vj1SUk9cTmdxVkRHaX9SNTVzaTBvYmhEbycpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPydocGlxbFpscWBoJyknYGtkZ2lgVWlkZmBtamlhYHd2Jz9xd3BgeCUl"
}
5. Configure events webhook Callback



Event documents and descriptions :
https://stripe.com/docs/billing/subscriptions/webhooks#events
边栏推荐
- Gbase 8C access authority query function (6)
- As a programmer, is there anything you want to say to the newcomer?
- iNFTnews | 呵护“雪山精灵”,42VERSE“数字生态保护”公益项目即将盛启
- Nacos
- What are blue-green deployment, Canary release and a/b test
- GBase 8c 字符串操作符
- Gbase 8C session information function (V)
- Intel Intel realsense realistic depth camera self calibration operation steps explanation D400 series is applicable
- After openfeign sets circuitbreaker=true, feign's readtimeout expires
- Linked list - 707. Design linked list
猜你喜欢

Multi knapsack explanation of dynamic programming knapsack problem

Redis主从同步机制
![[translation] Introduction to go RPC: Hello World](/img/dd/7887f056bc031fe50253078dd355d0.png)
[translation] Introduction to go RPC: Hello World
![[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)](/img/b5/dd4316b83ef4b80c36b532de658bb2.png)
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)

【译】Go RPC 入门:Hello World

Tencent will close the "magic core". Is there any resistance to the development of digital collections?

Docker builds sonarqube, mysql5.7 environment

vulnhub wpwn: 1

Summarize the plan, clarify the direction, concentrate and start a new situation -- the Counterpart Assistance Project of hexu software has achieved remarkable results

YOLOv1
随机推荐
C语言详解系列——函数的认识(2)如何使用函数实现交换两个整型变量的值
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
合宙ESP32C3基于Arduino IDE框架下配置分区表
GBase 8c模式可见性查询函数(二)
分布式之 CAP 原则
Docker builds sonarqube, mysql5.7 environment
JMeter中的自动转义处理
YOLOv1
Redis数据结构
Try new methods
GBase 8c 会话信息函数(四)
As a programmer, is there anything you want to say to the newcomer?
今天在家里补觉
【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
Redis cluster construction (cluster cluster mode, fragment cluster)
盘点为下个牛市做准备的10个新Layer1
Gbase 8C system table information function (II)
Gbase 8C string Operator
【语音合成】TensorFlowTTS 中文文本转语音
C language explanation series -- understanding of functions (2) how to use functions to exchange the values of two integer variables