使用自定义 cURL 和 PHP 代码进行条纹收费时出错
Errors When Making a Stripe Charge using Custom cURL and PHP Code
经过 3 天的扎实研究并尽我最大努力解决手头的问题后,我已经到了没有成功的地步,不得不意识到也许这是我不应该尝试的事情!在我自己的个人尝试失败时,我不得不求助于您的专家,在我的初学者学习阶段需要一些建议和帮助。
我曾尝试使用 stripe 提供的 PHP 代码,但是由于我们在网站中使用的自定义表单很复杂,而且我们网站的大量流量来自内部,因此我们需要一种向 stripe 发送付款的方式比集成一个全新的 php 支付流程更容易,该流程也可以处理来自内部客户的支付,所以我想我知道一点 cURL,因为使用了几个 cURL 和 PHP API 调用已经在网站进程。
我试过的stripe的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);
{ "error": { "type": "invalid_request_error", "message": "Unrecognized request URL (POST: /v1/charges%20-H%20Content-Type=application/x-www-form-urlencoded%20-u%20sk_test_keyP:%20-d%20source[object]=card%20-d%20source[number]=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). Please see https://stripe.com/docs or we can help at https://support.stripe.com/." } }
我已经尝试阅读文档,但没有任何关于我遇到的错误的解释,我认为问题的最大部分是我没有足够的经验来确切地知道要研究什么以及专家喜欢的关键短语抬头就知道了
我尝试过使用和不使用 -H Content-Type included
,但仍然遇到同样的问题,但是如果我将 curl 复制并粘贴到命令行中并使用
从命令中执行它
curl.exe https://...................
然后消息成功,我收到回复,我想说卡已被扣款。
also tried `$string = rawurlencode($data) and then http_build_query` on the $string
这是我试过的足够的例子:
<?php
$headers = array(
'Authorization: Bearer sk_test_removed_for_Whosebug',
);
// 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 ");
也这样试过:
// 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);
?>
老实说,我现在迷路了,如果有人能够在这个问题上帮助我并帮助我,我将非常感激,在经历了 3 天痛苦的痛苦之后,我真的不知道该怎么办。
作为新手学习者,在重新搜索问题时很难确切地知道要搜索什么。我在 google 上花了 3 天时间,收到 2 条警告消息,说他们认为我发送了自动查询,因为如果做了多少次研究!!因此,任何建议和帮助都会对此大有帮助。
谢谢大家。
使用 Stripe PHP bindings 不是可行的解决方案吗?这样会简单得多:
\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
}
如果你真的坚持使用 curl,这应该可行:
$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);
备注:
您自己提供银行卡详细信息,无需通过 Stripe.js or Checkout, raises the problem of PCI compliance 进行客户端标记化,但我想您已经意识到这一点。
使用绑定可以更轻松地通过异常处理费用创建失败。有关详细信息,请参阅 the documentation。
1 step :
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script src="https://js.stripe.com/v2/stripe-debug.js" type="text/javascript"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_gETzDYx9EWwlU5ZQYvU78OWv');
</script>
<form action="" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_gETzDYx9EWwlU5ZQYvU78OWv"
data-amount="895367"
data-name="GMR"
data-description="98000.00"
data-image="https://d1p3fwm9yfjbr.cloudfront.net/home-pagenew/images/gmr_logo.jpg"
data-locale="auto">
</script>
</form>
2nd step :
function curl_stripe($parameters){
$username="sk_test_TmbzPtTg1KVJz6Y87D0nwaUJ";
$password= "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_TIMEOUT, 1024); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"amount=".$parameters['amount']."¤cy=usd&source=".$parameters['token']."&metadata[order_id]=".$parameters['order_id']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec ($ch);
curl_close ($ch);
return $response = json_decode($result);
}
$parameter = array('path' =>'https://api.stripe.com/v1/charges','token'=>$_POST['stripeToken'],'amount'=>5986,'order_id'=>99999);
$this->curl_stripe($parameters)
经过 3 天的扎实研究并尽我最大努力解决手头的问题后,我已经到了没有成功的地步,不得不意识到也许这是我不应该尝试的事情!在我自己的个人尝试失败时,我不得不求助于您的专家,在我的初学者学习阶段需要一些建议和帮助。
我曾尝试使用 stripe 提供的 PHP 代码,但是由于我们在网站中使用的自定义表单很复杂,而且我们网站的大量流量来自内部,因此我们需要一种向 stripe 发送付款的方式比集成一个全新的 php 支付流程更容易,该流程也可以处理来自内部客户的支付,所以我想我知道一点 cURL,因为使用了几个 cURL 和 PHP API 调用已经在网站进程。
我试过的stripe的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);
{ "error": { "type": "invalid_request_error", "message": "Unrecognized request URL (POST: /v1/charges%20-H%20Content-Type=application/x-www-form-urlencoded%20-u%20sk_test_keyP:%20-d%20source[object]=card%20-d%20source[number]=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). Please see https://stripe.com/docs or we can help at https://support.stripe.com/." } }
我已经尝试阅读文档,但没有任何关于我遇到的错误的解释,我认为问题的最大部分是我没有足够的经验来确切地知道要研究什么以及专家喜欢的关键短语抬头就知道了
我尝试过使用和不使用 -H Content-Type included
,但仍然遇到同样的问题,但是如果我将 curl 复制并粘贴到命令行中并使用
curl.exe https://...................
然后消息成功,我收到回复,我想说卡已被扣款。
also tried `$string = rawurlencode($data) and then http_build_query` on the $string
这是我试过的足够的例子:
<?php
$headers = array(
'Authorization: Bearer sk_test_removed_for_Whosebug',
);
// 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 ");
也这样试过:
// 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);
?>
老实说,我现在迷路了,如果有人能够在这个问题上帮助我并帮助我,我将非常感激,在经历了 3 天痛苦的痛苦之后,我真的不知道该怎么办。
作为新手学习者,在重新搜索问题时很难确切地知道要搜索什么。我在 google 上花了 3 天时间,收到 2 条警告消息,说他们认为我发送了自动查询,因为如果做了多少次研究!!因此,任何建议和帮助都会对此大有帮助。
谢谢大家。
使用 Stripe PHP bindings 不是可行的解决方案吗?这样会简单得多:
\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
}
如果你真的坚持使用 curl,这应该可行:
$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);
备注:
您自己提供银行卡详细信息,无需通过 Stripe.js or Checkout, raises the problem of PCI compliance 进行客户端标记化,但我想您已经意识到这一点。
使用绑定可以更轻松地通过异常处理费用创建失败。有关详细信息,请参阅 the documentation。
1 step :
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script src="https://js.stripe.com/v2/stripe-debug.js" type="text/javascript"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_gETzDYx9EWwlU5ZQYvU78OWv');
</script>
<form action="" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_gETzDYx9EWwlU5ZQYvU78OWv"
data-amount="895367"
data-name="GMR"
data-description="98000.00"
data-image="https://d1p3fwm9yfjbr.cloudfront.net/home-pagenew/images/gmr_logo.jpg"
data-locale="auto">
</script>
</form>
2nd step :
function curl_stripe($parameters){
$username="sk_test_TmbzPtTg1KVJz6Y87D0nwaUJ";
$password= "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_TIMEOUT, 1024); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"amount=".$parameters['amount']."¤cy=usd&source=".$parameters['token']."&metadata[order_id]=".$parameters['order_id']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec ($ch);
curl_close ($ch);
return $response = json_decode($result);
}
$parameter = array('path' =>'https://api.stripe.com/v1/charges','token'=>$_POST['stripeToken'],'amount'=>5986,'order_id'=>99999);
$this->curl_stripe($parameters)