使用自定义cURL和PHP代码进行条带计费时出错

New*_*ner 3 php api curl stripe-payments

经过3天坚实的研究,并尝试我的absoloute最好解决手头的问题,我已经到了没有成功的地步,并不得不意识到这可能是我应该没有尝试过的东西!在我的个人尝试失败的情况下,我不得不求助于需要一些建议的专家,并在我的初学者学习阶段提供帮助.

我曾尝试使用条带提供的PHP代码,但是由于我们在网站上使用的复杂自定义表单以及我们的网站流量很多内部我们需要一种方式来向条带缓动器发送付款而不是集成一个全新的PHP支付流程这也可以处理来自内部客户的付款,所以我认为由于在网站流程中已经使用了几个cURL和PHP API调用,因此我知道一点cURL.

我试过条纹的cURL如下:


    $headers = array(
    'Authorization: Bearer sk_test_my_test_key_here',
);
$ch = curl_init("https://api.stripe.com/v1/charges -H Content-Type=application/x-www-form-urlencoded -u sk_test_my_test_key: -d source[object]=card -d source[number]=4242424242424242 -d source[exp_month]=08 -d source[exp_year]=18 -d source[cvc]=123 -d amount=250 -d currency=gbp -d description=Testing ");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

{"error":{"type":"invalid_request_error","message":"无法识别的请求网址(POST:/ v1/charge%20-H%20Content-Type = application/x-www-form-urlencoded%20- U%20sk_test_keyP:%20-d%20source [对象] =卡%20-d%20source [数字] = 4242424242424242%20-d%20source [exp_month] = 08%20-d%20source [exp_year] = 18%20 -d%20source [cvc] = 123%20-d%20amount = 250%20-d%20currency = gbp%20-d%20description = Testing).请参阅https://stripe.com/docs或我们可以提供帮助在https://support.stripe.com/." }}


我试着阅读文档,但没有解释我得到的错误,我认为问题的最大部分是我没有足够的经验来确切知道要研究什么,以及像你一样的关键短语专家会看到起来.


我已尝试使用和不使用-H Content-Type included仍然得到相同的问题但是如果我要将curl复制并粘贴到命令行并使用命令执行它

curl.exe https://...................
Run Code Online (Sandbox Code Playgroud)

然后消息成功,我得到回复,我想回来说卡已被收费.

also tried  `$string = rawurlencode($data) and then http_build_query` on the $string
Run Code Online (Sandbox Code Playgroud)

这是我尝试的足够的例子:

<?php
$headers = array(
    'Authorization: Bearer sk_test_removed_for_stackoverflow',
);

// create curl resource 
$ch = curl_init(); 

// set url 
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/charges -u sk_test_removed: -d source[object]=card -d source[number]=4242424242424242 -d source[exp_month]=08 -d source[exp_year]=18 -d source[cvc]=123 -d amount=250 -d currency=gbp -d description=Test "); 
Run Code Online (Sandbox Code Playgroud)

也尝试过这样:

// set url 
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/charges -u sk_test_removed: -d source[object]=$card -d source[number]=$number -d source[exp_month]=$expdate -d source[exp_year]=$expmonth -d source[cvc]=$ccv -d amount=$amount -d currency=gbp -d description=$sale "); 

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// $output contains the output string 
$output = curl_exec($ch); 
print($output);

// close curl resource to free up system resources 
curl_close($ch);
?>
Run Code Online (Sandbox Code Playgroud)

我现在诚实地处于一个失败的结局,对于任何能够帮助我并帮助我解决这个问题的人来说,我会非常感激,经过3个艰难的痛苦日子,我真的不知道该怎么办.

作为一个新手学习者,在重新搜索问题时,很难确切知道要搜索什么.我花了3天时间在谷歌上发布了2条警告消息说他们认为我发送的自动查询是因为研究完了多少次!所以任何建议和帮助都会在很长的路要走.

谢谢大家.

Ywa*_*ain 6

使用Stripe PHP绑定不是一个可能的解决方案吗?这会简单得多:

\Stripe\Stripe::setApiKey("sk_test_...");

// Token creation. In production this should be done client-side via Stripe.js or Checkout.
$token = \Stripe\Token::create([
  "card" => array(
    "number" => "4242424242424242",
    "exp_month" => 8,
    "exp_year" => 18,
    "cvc" => "123"
  )
]);

// Charge creation
try {
    $charge = \Stripe\Charge::create([
      "amount" => 250,
      "currency" => "gbp",
      "source" => $token->id,
      "description" => "Testing"
    ]);
} catch(...) {
    // Handle possible failures. See https://stripe.com/docs/api/php#errors
}
Run Code Online (Sandbox Code Playgroud)

如果你真的坚持使用卷曲,这应该工作:

$apiKey = 'sk_test_...';
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => "https://api.stripe.com/v1/charges",
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . $apiKey
    ],
    CURLOPT_POSTFIELDS => http_build_query([
        "amount" => 250,
        "currency" => 'gbp',
        "source" => array(
            "object" => "card",
            "number" => "4242424242424242",
            "exp_month" => 8,
            "exp_year" => 18,
            "cvc" => "123"
        ),
        "description" => "Testing"
    ])
]);
$resp = curl_exec($curl);
curl_close($curl);
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 自己提供卡详细信息,无需通过Stripe.jsCheckout进行客户端标记化,就会引发PCI合规性问题,但我认为您已经意识到这一点.

  2. 使用绑定可以更轻松地通过异常处理电荷创建失败.有关更多信息,请参阅文档.

  • 嗨NewLearner!使用Stripe PHP绑定的最简单方法是使用Composer(参见[GitHub页面](https://github.com/stripe/stripe-php#composer)).我还用一些细节和链接编辑了我的答案.如果您想了解更多信息,请与我们联系! (2认同)