条纹连接充电费返回错误(PHP)

时间:2015-03-29 04:40:08

标签: php stripe-connect

我正在开发一个使用条带支付网关的Web应用程序。我需要从每笔交易中向我的条带账户收取5美元,并将其余交易转移到供应商的条带账户。

我做了以下步骤。

1)。连接用户我分享了这个网址
https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_5slZHMozXs9B7rrgh6MDIFcdTvYqSdfz&scope=read_write

2)。设置重定向网址到我的应用程序并执行以下代码来处理请求

if (isset($_GET['code'])) { // Redirect w/ code
          $code = $_GET['code'];

          $token_request_body = array(
            'grant_type' => 'authorization_code',
            'client_id' => 'ca_5slZHMozXs9B7rrgh6MDIFcdTvYqSdfz',
            'code' => $code,
            'client_secret' => 'sk_test_WcXfM3Tsb0IlkSKmbZTYnq5d'
          );

          $req = curl_init("https://connect.stripe.com/oauth/token");
          curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($req, CURLOPT_POST, true );
          curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body));
          curl_setopt($req, CURLOPT_SSL_VERIFYPEER, FALSE);
          //curl_setopt($req, CURLOPT_CAINFO, "assets/cacert.pem");

          // TODO: Additional error handling
          $respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
          $resp = json_decode(curl_exec($req), true);
          curl_close($req);

          echo $resp['access_token'];
                echo "<br/>";
          echo  $resp['stripe_publishable_key'];
                echo "<br/>";
          echo $resp['stripe_user_id'];

        } else if (isset($_GET['error'])) { // Error
          echo $_GET['error_description'];
        } else { // Show OAuth link
          $authorize_request_body = array(
            'response_type' => 'code',
            'scope' => 'read_write',
            'client_id' => 'ca_5slZHMozXs9B7rrgh6MDIFcdTvYqSdfz'
          );

          $url = AUTHORIZE_URI . '?' . http_build_query($authorize_request_body);
          echo "<a href='$url'>Connect with Stripe</a>";
        }  

我收到了以下回复

sk_test_iLSNjRezyNiOiRpQk83UwQ6q
pk_test_0WXzhiA6xehlaZnmPZqP5VBi
acct_15iIu1I4jMsDTdhX

2)。然后我在html文件中使用以下代码来获取信用卡详细信息。如上面返回的可发布密钥的输出可发布密钥。

<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
              data-key="pk_test_0WXzhiA6xehlaZnmPZqP5VBi"
              data-email= "<?php echo $usremail; ?>"
              data-amount="<?php echo $valCoursefee*100; ?>" data-description="Pay & Enroll">
            </script>  

3)。然后我做了以下编码以从卡中接收令牌和费用。上面使用了条带帐户参数返回stripe_user_id

// Get the credit card details submitted by the form
        $token = $_POST['stripeToken'];
        // Stripe_Stripe::setApiKey("sk_test_WcXfM3Tsb0IlkSKmbZTYnq5d");
        // Create the charge on Stripe's servers - this will charge the user's card
        $charge = Stripe_Charge::create(
          array(
            "amount" => 1000, // amount in cents
            "currency" => "usd",
            "source" => $token,
            "description" => "testm@example.com",
            "application_fee" => 5 // amount in cents
          ),
          array("Stripe-Account" => "acct_15iIu1I4jMsDTdhX")
        );

但它返回我的错误

Fatal error: Uncaught exception 'Stripe_InvalidRequestError' with message 'There is no token with ID tok_15lW5jI4jMsDTdhXhG9vfnjk.' in C:\wamp2\www\NEW\application\libraries\payment_gateway\lib\Stripe\ApiRequestor.php on line 147

如果我在这里做错事,你能告诉我吗

4 个答案:

答案 0 :(得分:3)

直接在已连接的帐户上处理费用需要进行身份验证,最好通过提供平台帐户的密钥来实现。

因此,如果您想以5美元的申请费向卡收费,那么您必须通过Strip API对您的网络应用进行身份验证。哪个返回令牌然后你必须传递该令牌的充值卡。可能是这个链接会帮助你更多。 https://stripe.com/docs/connect/payments-fees

答案 1 :(得分:0)

我通过直接放置访问令牌来解决此错误。

// Get the credit card details submitted by the form
    $token = $_POST['stripeToken'];
    // Stripe_Stripe::setApiKey("sk_test_WcXfM3Tsb0IlkSKmbZTYnq5d");
    // Create the charge on Stripe's servers - this will charge the user's card
    $charge = Stripe_Charge::create(
      array(
        "amount" => 1000, // amount in cents
        "currency" => "usd",
        "source" => $token,
        "description" => "testm@example.com",
        "application_fee" => 5 // amount in cents
      ),
       ACCESS_TOKEN
    );

答案 2 :(得分:0)

基于版本更新,语法趋于稍微改变,有时我们混合使用不同的语法。尝试使用更高版本的Stripe

并使用以下代码替换您的代码:

$token = $_POST['stripeToken'];
$charge = \Stripe\Charge::create(
array(
  "amount" => 1000, // amount in cents
  "currency" => "usd",
  "source" => $token,
  "description" => "Example charge",
  "application_fee" => 123 // amount in cents
),
ACCESS_TOKEN

);

答案 3 :(得分:0)

我终于成功设置了一切,并成功地为客户收费。

第1步: 使用以下代码将客户连接到您的平台:

define('AUTHORIZE_URI', 'https://connect.stripe.com/oauth/authorize');

if(isset($ _ GET [&#39; code&#39;])){//重定向w /代码       $ code = $ _GET [&#39; code&#39;];

  $token_request_body = array(
    'grant_type' => 'authorization_code',
    'client_id' => ' ca_xxxxxxxxxxxxxxxxxxxxxxxxx',
    'code' => $code,
    'client_secret' => 'sk_live_xxxxxxxxxxxxxxxxxxxxx'
  );

  $req = curl_init("https://connect.stripe.com/oauth/token");
  curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($req, CURLOPT_POST, true );
  curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body));
  curl_setopt($req, CURLOPT_SSL_VERIFYPEER, FALSE);
  //curl_setopt($req, CURLOPT_CAINFO, "assets/cacert.pem");

  // TODO: Additional error handling
  $respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
  $resp = json_decode(curl_exec($req), true);
  curl_close($req);

  echo $resp['access_token'];
        echo "<br/>";
  echo  $resp['stripe_publishable_key'];
        echo "<br/>";
  echo $resp['stripe_user_id'];

} else if (isset($_GET['error'])) { // Error
  echo $_GET['error_description'];
} else { // Show OAuth link
  $authorize_request_body = array(
    'response_type' => 'code',
    'scope' => 'read_write',
    'client_id' => 'ca_xxxxxxxxxxxxxxxxxxxxxx'
  );

  $url = AUTHORIZE_URI . '?' . http_build_query($authorize_request_body);
  echo "<a href='$url'>Connect with Stripe</a>";
}  

客户连接您的帐户后,重定向网址将返回已连接的帐户ID(acc_xxxxxxxxxxxxxxxxxxx)

第2步: 创建表单以进行付款,如下所示:

<form action="confirmed.php" method="POST" class="form" >
<input type="hidden" name="pay" value="pay" />
<script
    src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
    data-key="<?php echo $publishable_key; ?>"
    data-image="http://www.domain.co.uk/pay/img/stripe.png"
    data-name="<?php echo $company_name; ?>"
    data-currency="gbp"
    data-email="<?php echo $email; ?>"
    data-description="<?php echo $payment_description; ?>t"
    data-label = "Confirm Payment of <?php echo currency($amount); ?>"
    data-billingAddress="false"
    data-amount="<?php echo $stripe_amount; ?>">
</script>

<input type="hidden" name="stripe_amount" value="<?php echo $stripe_amount; ?>">
<input type="hidden" name="email" value="<?php echo $email; ?>">
<input type="hidden" name="amount" value="<?php echo $amount; ?>">

我假设您知道要为php变量写什么。此表格将转至确认.php。

第3步:

将以下代码添加到confirmed.php。

<?php
                    if (isset($_POST['pay']))     { 
                        $stripe_amount = $_POST['stripe_amount'];
                        $amount = $stripe_amount / 100;
                        $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);

                        // Set your secret key: remember to change this to your live secret key in production
                        // See your keys here: https://dashboard.stripe.com/account/apikeys
                        \Stripe\Stripe::setApiKey($secret_key);

                        // Get the credit card details submitted by the form
                        $token = $_POST['stripeToken'];

                        // Create the charge with Stripe
                        $charge = \Stripe\Charge::create(
                          array(
                            "amount" => $stripe_amount, // amount in cents
                            "currency" => $currency,
                            "source" => $token,
                            "description" => "Example charge",
                            "application_fee" => $stripe_amount * 0.01 //charging %1 of the total amount
                          ),
                          array("stripe_account" => 'acct_xxxxxxxxxxxxxxx')
                        );

                        if ($charge){
                        //do something like sending email or updating database

                        echo '
                        <div class="col-md-12">
                            <div class="alert alert-success ">
                                <i class="icon fa fa-check"></i> Payment of <b>&pound;'.$amount.'</b> has been completed.
                            </div>
                        </div>
                        <a href="index.php">Make another payment</a>
                        ';

                        }

                        else {echo 'Something went wrong. Your payment may not have been confirmed. Please contact administrator.';}


                    } 

                ?>

这对我有用,所以欢迎任何问题。希望它可以帮助某人