[英]How to downgrade/upgrade plan in Stripe payments using API in PHP?
[英]How can I creat a form for future payments using Stripe and PHP?
您好,我正在嘗試創建一個表單,該表單將收集客戶信息(包括付款信息),以便在以后向他們收取服務費用。 我在獲取付款方式 ID 時遇到問題,當我在所有測試客戶處檢查我的條帶帳戶時,付款方式似乎根本沒有更新。 我正在按照此頁面上的說明進行操作。
到目前為止,這是我的代碼:
<?php
require_once('../vendor/stripe/stripe-php/init.php');
require_once('../vendor/autoload.php');
// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey('SECRET_KEY');
$customer = \Stripe\Customer::create([
'description' => 'My First Test Customer (created for API docs)',
]);
$intent = \Stripe\SetupIntent::create([
'customer' => $customer->id
]);
$buildForm = true;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$buildForm = false;
$stripe = new \Stripe\StripeClient('SECRET_KEY');
$cardholder_name = $_POST['cardholder_name'];
$customer_id = $_POST['customer_id'];
$stripe->customers->update(
$customer_id,
['name' => $cardholder_name,]
);
$payment_method = \Stripe\PaymentMethod::all([
'customer' => $customer_id,
'type' => 'card',
]);
// THIS PRINTS AN OBJECT BUT THE DATA ARRAY IS EMPTY.
print "<pre>"; print_r($payment_method); print "</pre>"; exit;
}
?>
<html>
<head>
</head>
<body>
<!-- placeholder for Elements -->
<?php if($buildForm): ?>
<form id="setup-form" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="post">
<input id="cardholder-name" type="text" required>
<div id="card-element"></div>
<input type="hidden" name="customer_id" value="<?php echo $customer->id ?>">
<button id="card-button" data-secret="<?php echo $intent->client_secret ?>">
Save Card
</button>
</form>
<?php endif; ?>
<script src="https://js.stripe.com/v3/"></script>
<script type="text/javascript">
var stripe = Stripe('PRIVATE_KEY');
var elements = stripe.elements();
var cardElement = elements.create('card');
cardElement.mount('#card-element');
</script>
<script type="text/javascript">
var cardholderName = document.getElementById('cardholder-name');
var cardButton = document.getElementById('card-button');
var clientSecret = cardButton.dataset.secret;
cardButton.addEventListener('click', function (ev) {
stripe.confirmCardSetup(
clientSecret, {
payment_method: {
card: cardElement,
billing_details: {
name: cardholderName.value,
},
},
}
).then(function (result) {
if (result.error) {
// Display error.message in your UI.
} else {
console.log(result);
}
});
});
</script>
</body>
</html>
您是否收到任何控制台錯誤? 您的儀表板日志中有任何錯誤嗎?
如果這沒有任何意義,您可能需要寫信給支持,以便他們可以為您查看詳細信息: https : //support.stripe.com/contact/email
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.