将会话变量包含到条带支付php

时间:2015-09-13 19:01:05

标签: php concatenation session-variables

非常简单,但我试图将其包括在内:

<?php echo $_SESSION['totalprice']; ?>

进入charge.php的金额字段,这是:

<?php
  require_once('config.php');

  $token  = $_POST['stripeToken'];

  $customer = \Stripe\Customer::create(array(
      'email' => 'customer@example.com',
      'card'  => $token
  ));

  $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => "".$_SESSION['totalprice']."",
      'currency' => 'eur'
  ));

  echo '<h1>Successfully charged $50.00!</h1>';
?>

我认为这是一个缩进问题,但无法解决问题。这是错误:

  

*致命错误:未捕获的异常&#39; Stripe \ Error \ InvalidRequest&#39;消息&#39;无效的整数:&#39;在   /home/barceloneta/public_html/stripe-php-3.2.0/lib/ApiRequestor.php:98   来自API请求&#39; req_6ycxcj8LmrLt9T&#39;堆栈跟踪:#0   /home/barceloneta/public_html/stripe-php-3.2.0/lib/ApiRequestor.php(210):   Stripe \ ApiRequestor-&gt; handleApiError(&#39; {\ n&#34;错误&#34;:{\ n ...&#39;,400,数组,   数组)#1   /home/barceloneta/public_html/stripe-php-3.2.0/lib/ApiRequestor.php(60):   Stripe \ ApiRequestor-&gt; _interpretResponse(&#39; {\ n&#34;错误&#34;:{\ n ...&#39;,400,   数组)#2   /home/barceloneta/public_html/stripe-php-3.2.0/lib/ApiResource.php(105):   Stripe \ ApiRequestor-&gt;请求(&#39;发布&#39;,&#39; / v1 / charge&#39;,数组,数组)#3   /home/barceloneta/public_html/stripe-php-3.2.0/lib/ApiResource.php(137):   Stripe \ ApiResource :: _ staticRequest(&#39; post&#39;,&#39; / v1 / charges&#39;,Array,NULL)

     

4 /home/barceloneta/public_html/stripe-php-3.2.0/lib/Charge.php(37):Stripe \ ApiResource :: _ create(Array,NULL)#5

     

/home/barceloneta/public_html/charge.php(15):   Stripe \ Charge :: create(Array)#6 / home / barceloneta / public_htm in   /home/barceloneta/public_html/stripe-php-3.2.0/lib/ApiRequestor.php on   第98行

1 个答案:

答案 0 :(得分:0)

您将数量包装在引号中,使其成为字符串类型。似乎Stripe对此非常严格,所以删除引号并确保发送整数。

$charge = \Stripe\Charge::create(array(
    'customer' => $customer->id,
    'amount'   => (int) $_SESSION['totalprice'],
    'currency' => 'eur'
));

此外,确保会话密钥已设置并且在将其发送到条带之前具有整数值。