【问题标题】:Self enter charge amount for Stripe Payment自行输入 Stripe Payment 的收费金额
【发布时间】:2015-04-02 18:25:55
【问题描述】:

我正在尝试在我的网站中实施 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>

我希望我的客户更改“数据金额”,以便他们可以更改付款的价值。我确信下面给出的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来处理?

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

【问题讨论】:

    标签: javascript php stripe-payments


    【解决方案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.
    }
    

    我使用此代码设置了循环支付系统。它奏效了!我希望这对你也有帮助。 :)

    【讨论】:

    • 谢谢!让我看看我能在这里做什么。我会回来告诉你的。
    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 2017-07-18
    • 2013-07-23
    • 1970-01-01
    • 2014-04-23
    • 2021-06-12
    • 2012-08-09
    • 2018-06-29
    相关资源
    最近更新 更多