自行输入条纹付款的费用金额

时间:2015-04-02 18:25:56

标签: javascript php stripe-payments

我正在尝试将Stripe付款实施到我的网站中,以便客户可以自行收取金额。条带文档并不简单,因为集成有困难。

<form action="charge.php" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_xxxxxxxxxxxxxxxxxxxx"
    data-amount="CHARGE AMOUNT"
    data-name="Maid In Raleigh"
    data-description="Service Charge"
    data-image="/128x128.png">
  </script>
</form>

我希望我的客户更改&#34;数据量&#34;这样他们就可以改变付款的价值。我确信下面给出的charge.php是一团糟。虽然仪表板在其日志文件中注册了令牌,但我无法使其工作。

<?php
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxx");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
  "amount" => CHARGEAMOUNT, // amount in cents, again
  "currency" => "usd",
  "source" => $token,
  "description" => "Service Charge")
);
    echo "<h2>Thank you!</h2>"
    echo $_POST['stripeEmail'];
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}
    echo "<h2>Thank you!</h2>"
?>

有没有办法避免从客户端的javascript收费,而是使用PHP来处理?

谢谢,如果有人可以提供帮助!

1 个答案:

答案 0 :(得分:1)

几天前我不得不使用stripe api处理动态金额的付款。我使用了以下代码,我没有使用名称空间。但我相信你能够工作。

$card = array(
    "number" => '',     // credit card number you are about to charge
    "exp_month" => '', // card expire month
    "exp_year" => '',   // card expire year
    "cvc" => '' // cvc code
);

此数组是生成令牌所必需的。

$token_id = Stripe_Token::create(array(
    "card" => $card 
));

现在是时候处理付款了。但首先检查令牌是否有效

if($token_id->id !=''){
    $charge = Stripe_Charge::create(array( 
        "amount" => '', // amount to charge
        "currency" => '', // currency
        "card" => $token_id->id, // generated token id
        "metadata" => '' // some metadata that you want to store with the payment
    ));

    if ($charge->paid == true) {
      // payment successful
    }
    else{
      // payment failed
    }
}
else{
    // card is declined.
}

我使用此代码设置定期付款系统。它工作了!我希望这对你也有帮助。 :)