当前位置:网站首页>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
边栏推荐
- STL case - judges' scoring
- 【语音合成】TensorFlowTTS 中文文本转语音
- [Android kotlin] property, getter and setter
- 文本和图片的绘制、数据存储、localStorage、sessionStorage、cookie三者的区别
- 二叉搜索树的简易实现及剖析
- My meeting of OA project (query)
- Super simple training of force deduction and problem brushing
- Pipeline pipeline project is built by declarative and jenkinsfile under Jenkins
- Delete all data of specified query criteria in Oracle
- Happiness of progress and growth
猜你喜欢
随机推荐
Gbase 8C system table information function (II)
GBase 8c系统表信息函数(二)
YOLOv1
Gbase 8C string Operator
docker搭建sonarqube,mysql5.7环境
Gbase 8C string operator
GBase 8c 访问权限访问函数(四)
Gbase 8C session information function (III)
GBase 8c 会话信息函数(一)
GBase 8c 会话信息函数(五)
Gbase 8C binary string operator
腾讯将关闭“幻核”,数字藏品领域发展是否面临阻力?
mysql数据库基础
【低代码】低代码发展的局限性
Esp8266 - at command + network transparent transmission
YOLOv1
Redis主从同步机制
蓝绿部署、金丝雀发布、A/B测试是什么
GBase 8c 访问权限查询函数(一)
jenkins下使用声明式(Declarative)和Jenkinsfile的方式构建Pipeline流水线项目









