在条形charg中传递php varible

时间:2016-05-08 11:55:22

标签: javascript php jquery ajax strip

我想在条带中传递php变量,但是它们显示缺少参数量的错误。

这是我的代码我从上一页获得此金额,并希望在此代码中传递金额变量,但我在网上搜索他们说只传递分值。  是否有任何解决方案通过变量传递金额我想动态传递值而不是静态如何可能

    $amountvalue = '123';
     try {
        if (empty($_POST['street']) || empty($_POST['city']) || empty($_POST['zip']))
          throw new Exception("Fill out all required fields.");
        if (!isset($_POST['stripeToken']))
          throw new Exception("The Stripe Token was not generated correctly");
        Stripe_Charge::create(array("amount" => $amountvalue,
                                    "currency" => "aud",
                                    "card" => $_POST['stripeToken'],
                                    "description" => $_POST['email']));
        $success = '<div class="alert alert-success">
                    <strong>Success!</strong> Your payment was successful.
                    </div>';
      }
      catch (Exception $e) {
        $error = '<div class="alert alert-danger">
                  <strong>Error!</strong> '.$e->getMessage().'
                  </div>';
      }

但似乎除了这个变量外,Stripe不会。它在页面上放置了“Missing required param:amount”错误它只想接受一个实际的整数,例如'1234'

enter image description here

2 个答案:

答案 0 :(得分:0)

根据你的说法, Stripe_Charge :: create()静态方法只需要数字数据而不是字符串;那么你也可以简单地输入数字数据,如下所示:

    <?php
        // THIS IS EXACTLY THE SAME CODE YOU SUPPLIED ABOVE WITH JUST A SIMPLE CHANGE:
        // THIS TIME WE ARE USING CENTS RATHER TO MAKE IT EASIER.
        $amountvalue = '123';
        try {
            if (empty($_POST['street']) || empty($_POST['city']) || empty($_POST['zip']))
                throw new Exception("Fill out all required fields.");
            if (!isset($_POST['stripeToken']))
                throw new Exception("The Stripe Token was not generated correctly");
            // NOTICE THAT THE ONLY CHANGE HERE IS TO WRAP THE $amountvalue INSIDE OF intval()
            // THIS CONVERTS THAT $amountvalue TO AN INTEGER, 
            // REGARDLESS OF WHETHER IT IS DYNAMIC OR HARD-CODED AS IT IN THIS CASE.
            // NOTICE ALSO THAT WE ARE MULTIPLYING THE PASSED AMOUNT BY 100
            // THAT IS CONVERTING THE $amountvalue TO CENTS...
            Stripe_Charge::create(array("amount"      => intval($amountvalue)*100,
                                        "currency"    => "aud",
                                        "card"        => $_POST['stripeToken'],
                                        "description" => $_POST['email']));
            $success = '<div class="alert alert-success">
                        <strong>Success!</strong> Your payment was successful.
                        </div>';
        }
        catch (Exception $e) {
            $error = '<div class="alert alert-danger">
                      <strong>Error!</strong> '.$e->getMessage().'
                      </div>';
        }

Stripe要求您传递包含信用卡数据 的源参数 ,如下所示:

    \Stripe\Charge::create(array(
      "amount" => 400,
      "currency" => "usd",
      // YOU'RE NOT PASSING THIS source ARRAY PARAMETER...
      // YOU'RE PASSING JUST card INSTEAD... PROBABLY THE CREDIT-CARD NR. ONLY
      "source" => array(
        "number" => "4242424242424242",
        "exp_month" => 5,
        "exp_year" => 2017,
        "cvc" => "314"
      ),
      "description" => "Charge for test@example.com"
    ));

参考:https://stripe.com/us/features

如果你的功能真的是 NUMBER HUNGRY ,它会微笑那里的那个小小的改变......; - )

干杯&amp;祝你好运....

答案 1 :(得分:0)

问题似乎不是数量,而是您在创建函数中传递的数据。看看这段代码:

<?php

        // THIS IS EXACTLY THE SAME CODE YOU SUPPLIED ABOVE WITH JUST A SIMPLE CHANGE:
        // THIS TIME WE ARE USING CENTS RATHER TO MAKE IT EASIER.
        $amountvalue = '123';
        try {
            if (empty($_POST['street']) || empty($_POST['city']) || empty($_POST['zip']))
                throw new Exception("Fill out all required fields.");
            if (!isset($_POST['stripeToken']))
                throw new Exception("The Stripe Token was not generated correctly");

            Stripe_Charge::create(array("amount"        => intval($amountvalue)*100,
                                        "currency"      => "aud",
                                        //HERE WAS THE ISSUE:
                                        'source'        => array(           
                                            "number"    => $_POST['stripeToken'],
                                            "exp_month" => $_POST['expiry_month'],  // GET THE CORRESPONDING POST VALUE FOR EXPIRY MONTH
                                            "exp_year"  => $_POST['expiry_year'],   // GET THE CORRESPONDING POST VALUE FOR EXPIRY MONTH
                                            "cvc"       => $_POST['cvc']            // GET THE CORRESPONDING POST VALUE FOR EXPIRY MONTH
                                        ),
                                        // REMOVE THIS ENTRY: "card"
                                        // "card"          => $_POST['stripeToken'],
                                        "description" => $_POST['email']));

            $success = '<div class="alert alert-success">
                        <strong>Success!</strong> Your payment was successful.
                        </div>';
        }
        catch (Exception $e) {
            $error = '<div class="alert alert-danger">
                      <strong>Error!</strong> '.$e->getMessage().'
                      </div>';
        }

您只需要一种获取和传递信用卡信息的方法:信用卡号,到期月,到期年份和CVC 。我相信这可以解决你的问题...