如何做一个变量量复发性捐赠计划认购与条纹JS PHP

问题描述:

我不是一个疯狂的编码器专家,但我想用条纹灯箱形式使用这些功能:如何做一个变量量复发性捐赠计划认购与条纹JS PHP

  • 用户可输入变量金额为一次性捐款,然后充电他 (它看起来工作,我可以看到令牌和收费条纹)

  • 用户可以输入年度捐赠的可变金额然后充电他然后发送电子邮件receipe,最低金额是3000日元(约30 $)

问题:

  • 在我使用TECHNIC使用户支付最低3000日元,如果他的值更改为0为例,他在成为3000集中了输入区域的形式再次,这就是为什么我使用值=“3000”,但它会更好,如果它只能 占位符=“3000 YEN最小”

  • 后付费用户条纹不发送电子邮件确认,我读了它没有做它在测试模式下,仍然如此?

  • 我想隐藏的HTML源的公钥,如果有可能

  • 的主要问题是,当用户输入一个变量量和验证它推出条纹灯箱窗口形式则启动当家。 PHP在我的服务器上,但要创建一个计划,金额必须固定一个独特的计划编号。

所以在这里我的代码,这个想法是生成日期和使用它来创建一个唯一的计划ID,但它不工作和IM不能肯定是最好的方法,我读了这一点,但即时通讯丢失了,我不知道如何https://support.stripe.com/questions/metered-subscription-billing

而且我会用这种形式把它列入到我的WordPress用插件的PHP代码换职位 (它的外观上班找到,但我不知道它的最佳实践和安全的方式,建立一个插件对我来说太复杂了)

所以这里我的文件结构:

  • ROOT

  • ../Vendor/Stripe/(与作曲家instaled,是有可能不作曲家安装?)

  • ../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

​​

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但我不知道怎么过......

但如果你可以帮助我解决主要问题,我真的很感激,这段代码让我疯狂!

干杯!

+0

“如果可能,我想将公钥从html源中隐藏起来。”为什么?它是公开的。 – ceejayoz

在我使用TECHNIC使用户支付最低3000日元,如果他的值更改为0为例,当他集中了在输入区的形式成为3000再次,这就是为什么我使用值=“3000 “但它会更好,如果它可以只有占位符=”3000日元最低“

这是由你来执行这一点。我建议您同时添加客户端(Javascript)和服务器端(PHP)检查以确保金额​​高于最低限额。

收费用户Stripe不发送电子邮件确认后,我读取它不在测试模式下,仍然如此?

是的,Stripe不会在测试模式下自动发送任何电子邮件收据。您仍然可以在信息中心查看具体费用,然后点击“发送回执”手动发送收据。

我想隐藏的HTML源的公钥,如果有可能

那是不可能的,但它不是一个问题。可发布的密钥旨在公开可见。它只能用于创建具有CheckoutStripe.js的令牌,而令牌本身不会执行任何操作。

的主要问题是,当用户输入一个变量量和验证它推出条纹灯箱窗口则启动charge2.php我的服务器上,而是创建一个计划的形式,数额必须固定一个独特的计划编号。

有几种方法可以处理可变金额的订阅。最简单的可能是用amount=1创建基本计划。然后,当您对此计划使用create subscriptions时,您可以使用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, 
)); 

在实践中,以条纹的API的所有调用应该被包裹在try/catch块,使确保errors are handled正确。

+0

非常感谢Ywain! – sayato