在strip-charg中传递php变量

passing php varible in strip charg

本文关键字:php 变量 strip-charg      更新时间:2023-09-26

我想在stripe中传递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似乎不会排除这个变量。它在页面上放置了一个"缺少必需参数:金额"错误。它只想接受一个实际的整数,如"1234"

在此处输入图像描述

根据您的说法,如果Stripe_Charge::create()静态方法只需要Numeric数据而不需要String;那么你也可以简单地输入数字数据,比如

    <?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,它会微笑着改变…;-)

干杯&祝你好运

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

<?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。我相信这会解决你的问题。。。