2016-06-16 8 views
2

私はユーザーがStripeを通じて寄付の価値を動的に設定できるようにしようとしています。私はStripeAmount変数を使って 'amount'の値を割り当てることができましたが、私のcharge.phpファイルでこの変数を呼び出そうとすると、変数はnullを返します。私は解決のために無限にGoogleを検索しているが、何もしていない。私は相対的な初心者です。サーバー側のコードでSTRIPE - charge.phpで呼び出されたときに動的に設定された金額がnullを返す

=フォームのコード=

 <form id="target_form" action="charge.php" method="POST"> 

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

     <button class="stripe_button_style" id="myStripeButton">Give By Card</button>   

       <script> 
        var handler = StripeCheckout.configure({ 
         key: "pk_test_xxxxxxxxxxxxxxxx", 
         image: "includes/images/stripe_logo.png", 
         token: function(token, args){ 
          var form = $('#target_form'); 
          form.append($('<input type="hidden" name="stripeToken" />').val(token.id)); 

          form.get(0).submit(); 
          } 
        }); 

        document.getElementById('myStripeButton').addEventListener('click', function(e) { 

         var StripeAmount = $("#donation_amount").val() *100; 

         handler.open({ 
          name: "company name", 
          description: "donation", 
          panelLabel: "Give:", 
          allowRememberMe: true, 
          amount: StripeAmount, 
          zipCode: true, 
          shippingAddress: true, 
          billingAddress: true 
         }); 
          e.preventDefault(); 
         }); 

         $(window).on('popstate', function() { 
         handler.close(); 
        }); 
       </script> 

      </form> 

= Charge.phpコード=

<?php require_once('includes/stripe/init.php'); 
$stripe = array(
    "secret_key"  => "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx", 
    "publishable_key" => "pk_test_xxxxxxxxxxxxxxxxxxxxxxxx" 
); 
    \Stripe\Stripe::setApiKey($stripe['secret_key']); 
    $token = $_POST['stripeToken']; 
    $email = $_POST['stripeEmail']; 
    try { 
    $customer = \Stripe\Customer::create(array(
    'email' => $_POST['stripeEmail'], 
    'source' => $token)); 
$charge = \Stripe\Charge::create(array(
    'amount' => $amount, 
    'customer' => $customer->id, 
    'currency' => "usd", 
    'description' => $email)); 
    echo 'Thank you for your donation. We will send you a receipt by email for your tax records within 48hours'; 

    var_dump($charge); 
}catch(\Stripe\Error\Card $e){ 

echo $e->getMessage(); 

} 

答えて

0

$amount初期化されることはありません。

また、Checkoutに渡された金額と通貨は、表示目的で使用されています。チェックアウトは、サーバーに金額と通貨を送信しません。

あなたはこのような何かをする必要があるでしょう:

  1. 更新クライアント側のコードをサーバに量を送信するために:

    token: function(token, args) { 
        var form = $('#target_form'); 
        form.append($('<input type="hidden" name="amount" />').val($("#donation_amount").val() * 100;)); 
        form.append($('<input type="hidden" name="stripeToken" />').val(token.id)); 
        form.get(0).submit(); 
    } 
    
  2. は、あなたのサーバ - の金額を取得しますサイドコード:

    $amount = $_POST['amount']; 
    
+0

私はOを行うために必要なne small edit、それ以外は完璧でした。大変感謝します。 –

関連する問題