javascript - Stripe 动态支付

标签 javascript php payment stripe-payments

我正在为 Stripe 苦苦挣扎。我正在使用 PHP,我正在尝试建立一个没有 CMS 的简单商店。想知道如何将金额传递到 charge.php 中,以便我可以针对不同的商品收取不同的金额产品。这是我的代码:

$charge = Stripe_Charge::create(array(
      'customer' => $customer->id,
      'amount'   => 1900;,
      'currency' => 'gbp'
  ));

这是 index.php 中的代码 - 我想向客户收取下表中“数据量”中的任何内容。不太清楚该怎么做。

<form action="inc/charge.php" method="POST">
    <script
            src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="<?php echo $stripe['publishable_key']; ?>"
            data-amount="1900"
            data-currency="GBP"
            data-name="Pure - Tumblr Theme"
            data-allow-remember-me="false"
            data-description="Premium Tumblr Theme"
            data-image="/128x128.png">
          </script>
</form>

最佳答案

更全面,从 index.php 到 charge.php 而不是相反。

<?php
 #set your variables
 $amount       = 500;
 $name         = 'My Company';
 $currency     = 'gbp';
 $description  = 'Value Plan';
 $uid          = get->your->uid;
 $email        = get->your->email;
?>

<center><form action="../charge.php" method="post">
<!-- make these hidden input types for the post action to charge.php -->
<input type="hidden" name="amount"      value="<?php echo $amount?>">
<input type="hidden" name="name"        value="<?php echo $name;?>">
<input type="hidden" name="currency"    value="<?php echo $currency;?>">
<input type="hidden" name="description" value="<?php echo $description;?>">
<input type="hidden" name="uid"         value="<?php echo $uid;?>">
<input type="hidden" name="email"       value="<?php echo $email;?>">

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"

        data-key =           "<?php echo $stripe['publishable_key']; ?>"
        data-amount =        "<?php echo $amount;?>"
        data-name =          "<?php echo $name;?>"
        data-currency =      "<?php echo $currency;?>"
        data-description =   "<?php echo $description;?>"
        data-email =         "<?php echo $user->data()->email; ?>"
        data-billing-address =     "true"
        data-allow-remember-me =   "false"
        >

</script>
</form></center>

然后在charge.php中你可以调用你隐藏在index.php中的输入值

<?php
$token        = $_POST['stripeToken'];
$email        = $_POST['email'];
$uid          = $_POST['uid'];
$currency     = $_POST['currency'];
$amount       = $_POST['amount'];
$description  = $_POST['description'];

#This is the standard try catch block stripe suggests
try{
$charge = Stripe_Charge::create(array(
"amount"        => $amount,
"currency"      => $currency,
"customer"      => $charge_to,
"description"   => $description
));

} catch(Stripe_CardError $e) {

$error = $e->getMessage();
// Since it's a decline, Stripe_CardError will be caught
$body = $e->getJsonBody();
$err  = $body['error'];

print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (Stripe_InvalidRequestError $e) {

// Invalid parameters were supplied to Stripe's API
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
} catch (Stripe_Error $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}
?>

关于javascript - Stripe 动态支付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23695085/

相关文章:

javascript - Redux-saga put方法不触发reducer

Php、MySql连接两张表并限制嵌套记录

php - 处理从表单输入到 SQL 的空值

android - 通过 HCE 模拟信用卡

ios - iTunes Connect 何时更新财务报告?

php - 为什么支付网关使用校验和?

javascript - 如何在点击时更改多个div的内容

javascript - 使用 dropkick 进行 Parsley js 表单验证 - 无法在自定义选择下拉列表中添加错误类

javascript - 如何从单独的表单中预填充数据电子邮件

php - 选择框多级