Stripe PaymentIntents PHP,设置价格
Stripe PaymentIntents PHP, setting price
基本上,我遇到的问题在 php 和 javascript 之间,其中我的 php 函数是 createPayment()
我无法将 javascript 变量所以我无法输入订单金额($)
我真的不知道该怎么做。
也许 post 事件到服务器以通过 javascript 创建付款,但我不知道该怎么做
Javascript:
document.getElementById("stripe").onclick = function(){
const stripe = Stripe('public_key');
const options = {
clientSecret: "<?php include('payments.php'); $payment = createPayment(500); echo $payment->client_secret ?>",
// Fully customizable with appearance API.
appearance: {/*...*/},
};
// Set up Stripe.js and Elements to use in checkout form, passing the client secret obtained in step 2
const elements = stripe.elements(options);
// Create and mount the Payment Element
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');
}
服务器端:
<?php
require_once('vendor/autoload.php');
function createPayment($amount){
$stripe = new \Stripe\StripeClient('sk_key');
$intent = $stripe->paymentIntents->create(
[
'amount' => $amount,
'currency' => 'usd',
'automatic_payment_methods' => ['enabled' => true],
]
);
return $intent;
};
?>
方法一
解决该问题的方法之一是添加一个中间值 PHP(例如 intermediate.php)
所以如果你的原始流量是
cart.php > processpayment.php (with javascript) 调用 payment.php 得到 client_secret
然后将流程更改为
cart.php > intermediate.php > processpayment.php(with javascript) 调用 payment.php 得到 client_secret
所以对于 cart.php,首先调用带有金额参数的 intermediate.php,比如 intermediate.php?amount=1234 :
intermediate.php
<?php
session_start();
$_SESSION["amount"]=$_REQUEST["amount"];
header('Location: processpayment.php');
exit;
?>
现在在processpayment.php
改变
clientSecret: "<?php include('payments.php'); $payment = createPayment(500); echo $payment->client_secret ?>",
到
clientSecret: "<?php include('payments.php'); $payment = createPayment($_SESSION["amount"]); echo $payment->client_secret ?>",
注意:确保 processpayment.php 的顶部也有 <?php session_start(); ?>
方法二#(使用ajax)
另一种方法是通过 ajax 将金额值(例如 1234)传递给服务器脚本,这将 return 客户端密码,并在条带支付处理中使用客户端密码
因此,使用以下 html/js:
HTML 将在 window 加载时触发名为 trigger1() 的 JS 函数
JS trigger1() 将 运行 一个 ajax 函数,将金额(例如 1234)传递给 server2.php 并等待 return 的客户端密码,然后将其放入 ID 为“secret”的隐藏输入字段中
现在您可以通过 document.getElementById("secret").value 获得条带支付功能的正确客户端密码,所以现在只需使用:
clientSecret: document.getElementById("secret").value;
所以,下面是HTML/JS:
<script>
document.getElementById("stripe").onclick = function(){
const stripe = Stripe('public_key');
const options = {
clientSecret: document.getElementById("secret").value;
// Fully customizable with appearance API.
appearance: {/*...*/},
};
// Set up Stripe.js and Elements to use in checkout form, passing the client secret obtained in step 2
const elements = stripe.elements(options);
// Create and mount the Payment Element
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');
}
</script>
<input type=hidden id=secret value="">
<script>
function trigger1() {
$.ajax({
type: 'GET',
url: 'server2.php',
data: {"amount": 1234},
success: function (response) {
document.getElementById("secret").value=response
},
error: function () {
alert("Error !!");
}
});
}
window.onload= trigger1();
</script>
并使用以下 php (server2.php)
<?php
require_once('vendor/autoload.php');
$amount=$_GET["amount"];
$stripe = new \Stripe\StripeClient('sk_key');
$intent = $stripe->paymentIntents->create(
[
'amount' => $amount,
'currency' => 'usd',
'automatic_payment_methods' => ['enabled' => true],
]
);
echo $intent->client_secret;
基本上,我遇到的问题在 php 和 javascript 之间,其中我的 php 函数是 createPayment()
我无法将 javascript 变量所以我无法输入订单金额($)
我真的不知道该怎么做。
也许 post 事件到服务器以通过 javascript 创建付款,但我不知道该怎么做
Javascript:
document.getElementById("stripe").onclick = function(){
const stripe = Stripe('public_key');
const options = {
clientSecret: "<?php include('payments.php'); $payment = createPayment(500); echo $payment->client_secret ?>",
// Fully customizable with appearance API.
appearance: {/*...*/},
};
// Set up Stripe.js and Elements to use in checkout form, passing the client secret obtained in step 2
const elements = stripe.elements(options);
// Create and mount the Payment Element
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');
}
服务器端:
<?php
require_once('vendor/autoload.php');
function createPayment($amount){
$stripe = new \Stripe\StripeClient('sk_key');
$intent = $stripe->paymentIntents->create(
[
'amount' => $amount,
'currency' => 'usd',
'automatic_payment_methods' => ['enabled' => true],
]
);
return $intent;
};
?>
方法一
解决该问题的方法之一是添加一个中间值 PHP(例如 intermediate.php)
所以如果你的原始流量是
cart.php > processpayment.php (with javascript) 调用 payment.php 得到 client_secret
然后将流程更改为
cart.php > intermediate.php > processpayment.php(with javascript) 调用 payment.php 得到 client_secret
所以对于 cart.php,首先调用带有金额参数的 intermediate.php,比如 intermediate.php?amount=1234 :
intermediate.php
<?php
session_start();
$_SESSION["amount"]=$_REQUEST["amount"];
header('Location: processpayment.php');
exit;
?>
现在在processpayment.php
改变
clientSecret: "<?php include('payments.php'); $payment = createPayment(500); echo $payment->client_secret ?>",
到
clientSecret: "<?php include('payments.php'); $payment = createPayment($_SESSION["amount"]); echo $payment->client_secret ?>",
注意:确保 processpayment.php 的顶部也有 <?php session_start(); ?>
方法二#(使用ajax)
另一种方法是通过 ajax 将金额值(例如 1234)传递给服务器脚本,这将 return 客户端密码,并在条带支付处理中使用客户端密码
因此,使用以下 html/js:
HTML 将在 window 加载时触发名为 trigger1() 的 JS 函数
JS trigger1() 将 运行 一个 ajax 函数,将金额(例如 1234)传递给 server2.php 并等待 return 的客户端密码,然后将其放入 ID 为“secret”的隐藏输入字段中
现在您可以通过 document.getElementById("secret").value 获得条带支付功能的正确客户端密码,所以现在只需使用:
clientSecret: document.getElementById("secret").value;
所以,下面是HTML/JS:
<script>
document.getElementById("stripe").onclick = function(){
const stripe = Stripe('public_key');
const options = {
clientSecret: document.getElementById("secret").value;
// Fully customizable with appearance API.
appearance: {/*...*/},
};
// Set up Stripe.js and Elements to use in checkout form, passing the client secret obtained in step 2
const elements = stripe.elements(options);
// Create and mount the Payment Element
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');
}
</script>
<input type=hidden id=secret value="">
<script>
function trigger1() {
$.ajax({
type: 'GET',
url: 'server2.php',
data: {"amount": 1234},
success: function (response) {
document.getElementById("secret").value=response
},
error: function () {
alert("Error !!");
}
});
}
window.onload= trigger1();
</script>
并使用以下 php (server2.php)
<?php
require_once('vendor/autoload.php');
$amount=$_GET["amount"];
$stripe = new \Stripe\StripeClient('sk_key');
$intent = $stripe->paymentIntents->create(
[
'amount' => $amount,
'currency' => 'usd',
'automatic_payment_methods' => ['enabled' => true],
]
);
echo $intent->client_secret;