Cho*_*uan 3 javascript php json stripe-payments
已更新我构建了一个定价页面,该页面使用Stripe Checkout来使用产品 1 的一次性付款按钮和产品 2 的订阅付款按钮。
我的目标是将一次性付款按钮重定向到一次性付款的 Stripe Checkout,并单独将订阅付款重定向到定期付款的结账。
根据 STRIPE,这可以使用订阅作为 create-checkout-session.php(示例项目)中的 CheckoutSession 中的模式来完成:
结帐会话的模式。使用价格或设置模式时需要。如果结帐会话包含至少一项重复项目,则通过订阅。
与 Stripe 文档相反,以下代码行:'mode' => 'subscription',仅激活订阅付款,但不会重定向一次性付款。为了使一次性付款有效,我必须将其更改为:'mode' => 'payment',但是订阅付款不起作用。
这是有问题的 php 代码:
<?php
require_once 'shared.php';
$domain_url = $config['domain'];
// Create new Checkout Session for the order
// Other optional params include:
// [billing_address_collection] - to display billing address details on the page
// [customer] - if you have an existing Stripe Customer ID
// [payment_intent_data] - lets capture the payment later
// [customer_email] - lets you prefill the email input in the form
// For full details see https://stripe.com/docs/api/checkout/sessions/create
// ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' => $domain_url . '/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $domain_url . '/canceled.html',
'payment_method_types' => ['card'],
'mode' => 'subscription',
'line_items' => [[
'price' => $body->priceId,
'quantity' => 1,
]]
]);
echo json_encode(['sessionId' => $checkout_session['id']]);
Run Code Online (Sandbox Code Playgroud)
这是 JavaScript 代码:
// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function(priceId) {
return fetch("./create-checkout-session.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId
})
}).then(function(result) {
return result.json();
});
};
// Handle any errors returned from Checkout
var handleResult = function(result) {
if (result.error) {
var displayError = document.getElementById("error-message");
displayError.textContent = result.error.message;
}
};
/* Get your Stripe publishable key to initialize Stripe.js */
fetch("./config.php")
.then(function(result) {
return result.json();
})
.then(function(json) {
var publishableKey = json.publishableKey;
var subscriptionPriceId = json.subscriptionPrice;
var onetimePriceId = json.onetimePrice;
var stripe = Stripe(publishableKey);
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("subscription-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(subscriptionPriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("onetime-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(onetimePriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
});
Run Code Online (Sandbox Code Playgroud)
是否可以使用 Stripe Checkout 在同一页面上同时进行一次性付款和定期付款?我怎样才能做到这一点?
根据 Bemn更新:
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' => $domain_url . '/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $domain_url . '/canceled.html',
'payment_method_types' => ['card'],
'mode' => $body->mode
'line_items' => [[
'price' => $body->price_xxx,
// For metered billing, do not pass quantity
'quantity' => 1,
]],
'line_items' => [[
'price' => $body->price_zzz,
// For metered billing, do not pass quantity
'quantity' => 1,
]]
]);
echo json_encode(['sessionId' => $checkout_session['id']]);
Run Code Online (Sandbox Code Playgroud)
还有JS:
// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function(priceId, mode) {
return fetch("./create-checkout-session.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId,
mode: mode // <-- passing the mode, e.g. 'payment' or 'subscription'
})
}).then(function(result) {
return result.json();
});
};
Run Code Online (Sandbox Code Playgroud)
和 HTML:
<div data-stripe-priceid="pricexxx" data-stripe-mode="payment" id="onetime-btn" class="bold mt-2 d-inline-block w-100-after-md max-width-xxs py-2 btn btn-secondary">Ore Time</div>
<div data-stripe-priceid="pricexxx" data-stripe-mode="subscription" id="subscription-btn" class="bold mt-2 d-inline-block w-100-after-md max-width-xxs py-2 btn btn-secondary">Ore Time</div>
Run Code Online (Sandbox Code Playgroud)
是的。关键是您应该传递正确的选项来生成相应的 Stripe Checkout 会话 ID。
后端:有一个函数接受Stripe 的价格 ID和支付模式作为输入,并返回 Stripe Checkout 会话 ID 作为输出。
前端:将支付模式信息传递给/create-checkout-session.php. (如果您无法这样做,请参阅注释)
以下解决方案假设:
.createCheckoutSession()js 前端。我认为你很接近。您需要做的是将mode信息传递到您的 API 端点:
// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function(priceId, mode) { // <-- add a mode parameter
return fetch("./create-checkout-session.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId,
mode: mode // <-- passing the mode, e.g. 'payment' or 'subscription'
})
}).then(function(result) {
return result.json();
});
};
Run Code Online (Sandbox Code Playgroud)
如果是这样,页面中的每个结账按钮都应该有相应的priceId和支付方式信息。您可以通过使用 data 属性存储它们来做到这一点:
<div data-stripe-priceid="price_yyy" data-stripe-mode="subscription">Recurrent</div>
<div data-stripe-priceid="price_zzz" data-stripe-mode="payment">1-time</div>
Run Code Online (Sandbox Code Playgroud)
如果是这样,您可以通过事件等方式获取数据属性click。
注意:如果您无法添加额外的参数来指示模式,则需要在后端确定给定的价格 ID 是一次性产品还是重复产品。有关更多详细信息,请参阅https://stripe.com/docs/api/prices/object?lang=php#price_object-type 。
以下是 Stripe 文档中的 2 个示例代码片段。直接复制它们是行不通的。
付款参考: https: //stripe.com/docs/checkout/integration-builder
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
Run Code Online (Sandbox Code Playgroud)
就您而言,您可能不需要定义'price_data'. 相反,您应该使用'price',就像下一个示例一样。
SUB参考: https: //stripe.com/docs/billing/subscriptions/checkout#create-session
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
Run Code Online (Sandbox Code Playgroud)
line_items,您可以简单地使用'price'并传递价格 ID(例如price_xxx),这意味着您的'line_items'遗嘱如下所示:$checkout_session = \Stripe\Checkout\Session::create([
'success_url' => 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'https://example.com/canceled.html',
'payment_method_types' => ['card'],
'mode' => 'subscription',
'line_items' => [[
'price' => $body->priceId,
// For metered billing, do not pass quantity
'quantity' => 1,
]],
]);
Run Code Online (Sandbox Code Playgroud)
对于'mode',请使用 API 请求中的值。它应该是这样的:
'line_items' => [[
'price' => $body->priceId,
'quantity' => 1,
]],
Run Code Online (Sandbox Code Playgroud)
这意味着在您的后端您最好定义一个函数(例如generate_checkout_session)来:
priceId和modepriceIdandmode\Stripe\Checkout\Session::createcheckout_sessionID希望这个(以及参考网址)可以帮助您。
| 归档时间: |
|
| 查看次数: |
11566 次 |
| 最近记录: |