![](/img/trans.png)
[英]Integrating stripe payments (JS and PHP) with a custom amount (JS variable)
[英]How to do a variable amount recurent donation plan subscription with Stripe JS PHP
我不是一個瘋狂的編碼專家,但我想使用具有以下功能的Stripe燈箱形式:
用戶可以為一擊捐贈輸入可變金額,然后向他收取費用(看起來可以使用,我可以在Stripe中看到令牌並收取費用)
用戶可以輸入可變數量的“年度捐款”,然后向他收費,然后發送電子郵件收據,最低金額為3000日元(約30美元)
問題 :
在表格中,我使用一種技術使用戶在將輸入區域再次變為3000時,例如將值更改為0,則使用戶至少支付3000日元,這就是為什么我使用value =“ 3000”的原因,但這樣會更好如果只能是占位符=“最低3,000日元”
收費用戶Stripe不發送電子郵件確認后,我讀到它在測試模式下不這樣做,是否還正確?
如果可能,我想從html源隱藏公鑰
主要問題是,當用戶輸入可變金額並驗證其啟動Stripe燈箱窗口的形式,然后在我的服務器上啟動charge2.php時,但是要創建計划,必須使用唯一的計划ID固定金額。
所以在這里,我的代碼的想法是生成一個日期,並使用它來創建唯一的計划ID,但是它不起作用,我不確定這是最好的方法,我讀了這篇文章,但我迷路了,我不知道如何https:// support.stripe.com/questions/metered-subscription-billing
我也將使用這種形式通過插件php-code-for-posts將其包含在我的wordpress中(看起來可以工作,但是我不知道它的最佳實踐和安全方式,構建插件對我來說太復雜了)
所以這是我的文件結構:
根
../ Vendor / Stripe /(隨作曲家安裝,是否可以在沒有作曲家的情況下安裝?)
../Wordpress/config.php
../Wordpress/Stripepay.php
../Wordpress/charge.php
../Wordpress/charge2.php
該代碼的靈感來自https://jsfiddle.net/ywain/g2ufa8xr/
我在代碼中加了// // !! 有關我的問題的信息
config.php文件
<?php
// require with composer install
require_once('../vendor/autoload.php');
$stripe = array(
"secret_key" => "sk_test_ your secret_key",
"publishable_key" => "pk_test_ your publishable_key"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>
stripepay.php
<?php require_once('../config.php');?>
<!-- Stripe Panel, This script only work remotely -->
<script src="https://checkout.stripe.com/checkout.js"></script>
<!-- JQuery auto update CDN -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<!-- /////////////////////////////// ONE SHOT PAYMENT ///////////////////////////////// -->
<h2>One Donation</h2>
Please add any value below
<!-- Form and Charge Validation -->
<form id="payment-form" action="charge1" method="POST">
<input type="number" id="amount" name="amount" value="3000" placeholder="3,000 Minimum" min="3000" step="500.00" />
<input type="hidden" id="stripeToken" name="stripeToken"/>
<input type="hidden" id="stripeEmail" name="stripeEmail"/>
</form>
<input type="button" id="btn" value="Donate" class="strbtn">
<script type="text/javascript">
// JS Script Handle amount value and other Stripe options.
var handler = StripeCheckout.configure({
key: '< ?php echo $stripe['publishable_key']; ?>', // !! Possible to hide from HTML source view ??
image: '../logo.jpg',
token: function(token) {
$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#payment-form").submit();
}
});
$('#btn').on('click', function(e) {
var amount = $("#amount").val() *1; // !! normaly it's *1000 but with YEN need to apply this ??
// Open Checkout with further options
handler.open({
// OPTIONAL, UNCHECK THE // TO ACTIVATE IT:
//bitcoin: 'true',
//alipay: 'true',
billingAddress: 'true',
zipcode: 'true',
allowRememberMe: 'true',
//stripeEmail: 'true', // !! Stripe in test mode doesn't send email confirmation, there is any way to check if it will works ??
name: 'company name',
description: 'company description',
locale: 'auto', // !! on reults it show i'm from USA but i'm in Japan, is it based on navigator ? There is a way to be more accurate ??
panelLabel: 'DONATE',
currency: 'jpy',
amount: amount
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
//to prevent zero amount in the form, add 3000 when focus out of the field
$(document).ready(function(){
$("body").delegate('#amount', 'focusout', function(){ // !! There is a better way to make the minimum amount 3000Yen ??
if($(this).val() < 3000){
$(this).val('3000');
}
});
});
</script>
<!-- /////////////////////////////// ANNUAL PAYMENT SUBSCIPTION ///////////////////////////////// -->
<h2>Annual Recurring Donations</h2>
Please add any value below
<!-- Form and Charge Validation -->
<form id="payment-form2" action="charge2" method="POST">
<input type="number" id="amount2" name="amount" value="3000" placeholder="3,000 Minimum" min="3000" step="500.00" />
<input type="hidden" id="stripeToken2" name="stripeToken"/>
<input type="hidden" id="stripeEmail2" name="stripeEmail"/>
<input type="hidden" id="idplan" name="idplan"/>
</form>
<input type="button" id="btn2" value="Subscription" class="strbtn">
<script type="text/javascript">
// JS Script Handle amount value and other Stripe options.
var handler = StripeCheckout.configure({
key: '<?php echo $stripe['publishable_key']; ?>',
image: '../str-gps-logo.jpg',
token: function(token) {
$("#stripeToken2").val(token.id);
$("#stripeEmail2").val(token.email);
$("#payment-form2").submit();
}
});
$('#btn2').on('click', function(e) {
var amount = $("#amount2").val() *1;
var plan = $("#idplan").val(Date); // !! Generate Date ID for the Plan to be a unique id value, possible to add milisecond too ??
// Open Checkout with further options
handler.open({
billingAddress: 'true',
zipcode: 'true', // !! is it like this or zip-code: ??
name: 'Year Plan',
description: 'Variable Amount Year Plan',
locale: 'auto',
panelLabel: 'Subscribe',
currency: 'jpy',
amount: amount
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
//to prevent zero amount in the form, add 3000 when focus out of the field
$(document).ready(function(){
$("body").delegate('#amount2', 'focusout', function(){
if($(this).val() < 3000){
$(this).val('3000');
}
});
});
</script>
charge.php
<?php
require_once('./config.php');
// Check if the user have javascript and if the token is validated.
// !! code below needed or more simple with $token = $_POST['stripeToken']; ??
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array();
if (isset($_POST['stripeToken'])) {
$token = $_POST['stripeToken'];
} else {
$errors['token'] = 'The order cannot be processed. You have not been charged.
Please confirm that you have JavaScript enabled and try again.';
}
}
// Create the Customer:
$customer = \Stripe\Customer::create(array(
'email' => $_POST['stripeEmail'],
'source' => $token
));
// Create the Charge:
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $_POST['amount'],
'currency' => 'jpy'
));
echo '<h1>Thanks for your donation ! </h1>'; // !! There is a way to show error to user and redirect to index ?
?>
charge2.php
<?php
require_once('./config.php');
// Check if the user have javascript and if the token is validated.
//$token = $_POST['stripeToken'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array();
if (isset($_POST['stripeToken'])) {
$token = $_POST['stripeToken'];
} else {
$errors['token'] = 'The order cannot be processed. You have not been charged.
Please confirm that you have JavaScript enabled and try again.';
}
}
// TEST 1 inspired from http://stackoverflow.com/questions/36075869/how-to-create-variable-subscriptions-in-stripe
// !! Find the way to generate unique id to create the plan or any other solution for a variable amount recurent donation plan subscription
$plan = \Stripe\Plan::create(array(
'name' => $_POST['idplan'],
'id' => $_POST['idplan'],
'interval' => 'day', // !! interval daily for testing purpose but the final will be in years
'interval_count' => '1',
'currency' => 'jpy',
'amount' => $_POST['amount']
));
$customer = \Stripe\Customer::create(array(
'source' => $token,
'email' => $_POST['stripeEmail'],
'plan' => $_POST['idplan'],
'description' => 'plan description'
));
echo '<h1>Thanks for your annual donation ! </h1>';
?>
我敢肯定,有一種方法可以使清潔程序更清潔,僅包含一個收費文件,減少ID混亂,但我也不知道如何...
但是,如果您能幫助我解決主要問題,我將不勝感激,那么這段代碼會讓我發瘋!
干杯!
在表格中,我使用一種技術使用戶在將輸入區域再次變為3000時,例如將值更改為0,則使用戶至少支付3000日元,這就是為什么我使用value =“ 3000”的原因,但這樣會更好如果只能是占位符=“最低3,000日元”
強制執行此操作。 我建議您同時添加客戶端(Javascript)和服務器端(PHP)檢查,以確保金額超過您的最低要求。
收費用戶Stripe不發送電子郵件確認后,我讀到它在測試模式下不這樣做,是否還正確?
是的,Stripe不會在測試模式下自動發送任何電子郵件回執。 您仍然可以在信息中心中查看特定的費用,然后點擊“發送收據”以手動發送收據。
如果可能,我想從html源隱藏公鑰
那是不可能的,但這不是問題。 可發布的密鑰旨在公開可見。 它只能用於使用Checkout或Stripe.js創建令牌,而令牌本身不起作用 。
主要問題是,當用戶輸入可變金額並驗證其啟動Stripe燈箱窗口的形式,然后在我的服務器上啟動charge2.php時,但是要創建計划,必須使用唯一的計划ID固定金額。
有兩種方法可以處理可變數量的訂閱。 最簡單的方法可能是創建一個amount=1
的基本計划。 然后,當您創建此計划的訂閱時,可以使用quantity
參數來確保按正確的金額計費。
一個更優雅但更復雜的解決方案是為您的每個客戶創建一個新計划。 在這種情況下,您需要確保每個計划都具有唯一的ID-例如,您可以使用客戶的電子郵件地址:
$plan_id = "plan_" . $_POST["stripeEmail"];
$plan = \Stripe\Plan::create(array(
"id" => $plan_id,
"name" => "Subscription plan for " . $_POST["stripeEmail"],
"amount" => $_POST["amount"], // amount sent by the customer's browser
"currency" => "jpy",
"interval" => "day",
));
$customer = \Stripe\Customer::create(array(
"email" => $_POST["stripeEmail"],
"source" => $_POST["stripeToken"], // token returned by Stripe.js/Checkout
"plan" => $plan_id,
));
實際上,應將所有對Stripe API的調用包裝在try/catch
塊中,以確保正確處理錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.