gpt4 book ai didi

javascript - 将 strip 支付(JS 和 PHP)与自定义金额(JS 变量)集成

转载 作者:行者123 更新时间:2023-11-28 04:57:11 25 4
gpt4 key购买 nike

我几天来一直试图解决这个问题,但没有成功。我正在尝试在我的网站中实现 Stripe Payments Checkout。付款金额作为 JS 变量显示在付款页面上。我能够使基本结帐工作,但显然不能使用自定义金额,或将任何数据发送到 PHP 处理页面(电子邮件和一些订单属性)。我一直在尝试使用自定义结帐,但我无法弄清楚。有什么帮助吗?

到目前为止,我在 config.php 中有这个:

<?php
require_once('vendor/autoload.php');

$stripe = array(
"secret_key" => "MY SECRET KEY IS HERE",
"publishable_key" => "MY PUBLISHED KEY IS HERE"
);

\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

这是在一个名为 process.php 的文件中:

<?php
require_once('./config.php');

$token = $_POST['stripeToken'];
$input = $_POST["totalprice"];
$customer = \Stripe\Customer::create(array(
'email' => 'customer@example.com',
'source' => $token
));

$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $input,
'currency' => 'usd'
));

echo $input;
?>

在最初的 PHP 文件中,我有:

<?php require_once('./config.php'); ?>
<form action="process.php" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="MY PUBLIC TEST KEY IS HERE"
data-amount= amt * 100
data-name="Test Name"
data-description="Widget"
data-image="/img/logo.jpg"
data-locale="auto"
>
<form type=hidden name="totalprice" value=amt*100 action="process.php" method="POST">
</script>
</form>

话虽如此,我之前尝试过很多其他代码都不起作用,所以当前的代码可能应该被废弃。我真的很感激我能得到的任何帮助!

最佳答案

好了,下面是自定义集成的示例代码。

<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton">Purchase</button>

<script>
var handler = StripeCheckout.configure({
key: 'pk_test_6pRNASCoBOKtIshFeQd4XMUh',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
}
});

document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options:
handler.open({
name: 'Stripe.com',
description: '2 widgets',
zipCode: true,
amount: 2000
});
e.preventDefault();
});

// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>

源代码在 this page 上给出

这就是您要找的吗?

关于javascript - 将 strip 支付(JS 和 PHP)与自定义金额(JS 变量)集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42451126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com