当前位置:网站首页>php实现 Stripe订阅
php实现 Stripe订阅
2022-07-24 00:14:00 【向右看齐--】
测试模式创建订阅:https://dashboard.stripe.com/dashboard
选择测试模式 ,添加产品

1. 安装Stripe
composer require stripe/stripe-php2. 获取密钥
https://dashboard.stripe.com/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.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, // 试用期的结束时间
'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.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.配置事件webhook回调



事件文档及说明:
https://stripe.com/docs/billing/subscriptions/webhooks#events
边栏推荐
- The universal esp32c3 configures partition tables based on the Arduino ide framework
- 深度学习之 9 前馈神经网络 基本概念
- 【微信小程序】拍卖商品详情页设计与交互实现(包含倒计时、实时更新出价)
- Beijing University qingniaochangping Campus: how about the current situation of operation and maintenance employment? Are skills required?
- kubernetes error
- 今天在家里补觉
- [for loop if conditional statement] summary
- Mysql database foundation
- [wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
- 权重分析——熵权法
猜你喜欢

kubernetes error

YOLOv1

Sum of submatrix

QT create a background mask, pop up the child window, and the background of the parent window turns black and dark

腾讯将关闭“幻核”,数字藏品领域发展是否面临阻力?

Structured streaming programming model (input table, result table, output mode...)

Docker builds sonarqube, mysql5.7 environment

子矩阵的和

FPGA - SPI bus control flash (3) including code

【译】Go RPC 入门:Hello World
随机推荐
Super simple training of force deduction and problem brushing
Happiness of progress and growth
Mysql database foundation
The differences between text and image drawing, data storage, localstorage, sessionstorage, and cookies
【Android Kotlin】Property、Getter 和 Setter
作为一个程序员,有什么想对新人说的吗?
[for loop if conditional statement] summary
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
权重分析——CRITIC权重法
kubernetes error
单目标追踪——【相关滤波】MOSSE:Visual Object Tracking using Adaptive Correlation Filters
How to open a low commission account? Is it safe?
[wechat Payment]
Pytest interface automated testing framework | how to get help
云原生的概念
473-82(40、662、31、98、189)
权重分析——熵权法
YOLOv1
docker搭建sonarqube,mysql5.7环境
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)