【问题标题】:Dynamic Payments with Stripe使用 Stripe 进行动态支付
【发布时间】:2014-07-04 21:29:01
【问题描述】:

我正在为 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>

【问题讨论】:

    标签: javascript php payment stripe-payments


    【解决方案1】:

    为什么要对data-amount 中的任何 收费?你从哪里得到这个价值? data-amount 告诉 Stripe 用户允许你收取什么费用。 Stripe_Charge::create 中的金额是您实际收取的金额。

    您可以使用与 data-amount 相同的值填充隐藏的输入字段。但我不知道你会从中得到什么。 您的 PHP 脚本应该计算要支付的金额。不要相信客户。他可以更改 data-amount 的值以减少支付,即 50。 使用以下费用会起作用,但客户支付 50 便士而不是 1900 便士。

    $charge = Stripe_Charge::create(array( 'customer' => $customer->id, 'amount' => $_POST['hidden_amount']

    向 Stripe 询问计算的总付款。如果客户端弄乱了数据量,则收费将失败。 'amount' =&gt; $shoppingcart-&gt;getTotal();,

    【讨论】:

      【解决方案2】:

      更全面,从 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
      }
      ?>
      

      【讨论】:

      • 谢谢这真的帮了我
      猜你喜欢
      • 2016-10-25
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 2013-07-17
      • 2017-02-08
      • 2018-09-26
      • 2021-01-22
      相关资源
      最近更新 更多