使用测试信用卡时 PHP 解析 Cloud 和 Stripe 时出现问题
Problem with PHP Parse Cloud and Stripe when using Testing Credit Cards
希望你能帮助我。我在 Sashido (Parse Server) 中有一个 Parse Cloud 函数来管理 Stripe 订阅,如下所示:
var stripe = require("stripe")("sk_test_CCCCccCCXXXXXXxXXX");
Parse.Cloud.define("crearCargo", function(request, response) {
var token = request.params.stripeToken;
var mail = request.params.email;
//var mail = request.params.email;
//Crear Customer
const customer = stripe.customers.create({
email: mail,
source: token,
}, function(err, customer) {
// asynchronously called
if(err){
response.error("Fallo Customer");
}else{
//const{id} = customer;
id = customer.id;
stripe.subscriptions.create({
customer: id,
items: [
{
plan: "plan_E0jrObw8X7Le2F",
},
]
}, function(err, subscription) {
if(err){
response.error(err.message);
}else{
response.success(subscription.id);
}
}
);
}
});
});
我通过 php 从我的站点调用该函数,如下所示:
$results = ParseCloud::run("crearCargo", ["stripeToken" => "$stripeToken", "email" => "$email"]);
当信用卡没问题时这工作正常,但是当我使用被拒绝的信用卡来处理错误时,我无法在我的 php 代码中收到错误消息,即使我在日志中看到错误在 Sashido 仪表板中。这是日志:
Failed running cloud function crearCargo for user undefined with:
Input: {"stripeToken":"tok_1DaoDfHWMeJb0DRPDaAgN7rS","email":"shisuka11.08@gmail.com"}
Error: {"code":141,"message":"Your card was declined."}
Nov 26, 2018, 12:50:44 -05:00 - ERROR
Error generating response for [POST] /1//functions/crearCargo
"Your card was declined."
{
"stripeToken": "tok_1DaoDfHWMeJb0DRPDaAgN7rS",
"email": "myemail@email.com"
}
所以我无法处理错误,而是在我的浏览器中收到 HTTP 500 错误,您知道原因吗?
这是处理 $result 的方式,如果信用卡有效,它可以正常工作,我确实收到了订阅代码:
try {
if(substr( $results, 0, 3 ) === "sub"){
echo $results;
}
} catch (ParseException $e) {
echo 'Caught exception: '.$e->getMessage()."\n";
}
所以当我使用 4242424242424242 信用卡时,我确实收到了订阅代码,但是当我使用信用卡号 4100000000000019 强制出错时,我无法收到错误消息。
这正是我在使用 PHP 时收到的显示错误 ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
收到错误:
致命错误:未捕获 Parse\ParseException:您的卡被拒绝。在 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseClient.php:357 堆栈跟踪中:#0 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseCloud.php(32): Parse\ParseClient::_request('POST', 'functions/crear...', NULL, '{"stripeToken":...', false) #1 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/suscripcion.php(28): Parse\ParseCloud::运行('crearCargo', Array) #2 {main} 抛入 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseClient.php 第 357 行
我怀疑问题是您的 ParseCloud::run
调用不在您的 try-catch 块中。尝试这样的事情:
try {
$results = ParseCloud::run("crearCargo", ["stripeToken" => "$stripeToken", "email" => "$email"]);
}
catch(ParseException $e) {
echo 'Caught exception: '.$e->getMessage()."\n";
}
catch(Exception $e) {
// do something with other exceptions, for testing we'll just print it out
print_r($e);
}
希望你能帮助我。我在 Sashido (Parse Server) 中有一个 Parse Cloud 函数来管理 Stripe 订阅,如下所示:
var stripe = require("stripe")("sk_test_CCCCccCCXXXXXXxXXX");
Parse.Cloud.define("crearCargo", function(request, response) {
var token = request.params.stripeToken;
var mail = request.params.email;
//var mail = request.params.email;
//Crear Customer
const customer = stripe.customers.create({
email: mail,
source: token,
}, function(err, customer) {
// asynchronously called
if(err){
response.error("Fallo Customer");
}else{
//const{id} = customer;
id = customer.id;
stripe.subscriptions.create({
customer: id,
items: [
{
plan: "plan_E0jrObw8X7Le2F",
},
]
}, function(err, subscription) {
if(err){
response.error(err.message);
}else{
response.success(subscription.id);
}
}
);
}
});
});
我通过 php 从我的站点调用该函数,如下所示:
$results = ParseCloud::run("crearCargo", ["stripeToken" => "$stripeToken", "email" => "$email"]);
当信用卡没问题时这工作正常,但是当我使用被拒绝的信用卡来处理错误时,我无法在我的 php 代码中收到错误消息,即使我在日志中看到错误在 Sashido 仪表板中。这是日志:
Failed running cloud function crearCargo for user undefined with:
Input: {"stripeToken":"tok_1DaoDfHWMeJb0DRPDaAgN7rS","email":"shisuka11.08@gmail.com"}
Error: {"code":141,"message":"Your card was declined."}
Nov 26, 2018, 12:50:44 -05:00 - ERROR
Error generating response for [POST] /1//functions/crearCargo
"Your card was declined."
{
"stripeToken": "tok_1DaoDfHWMeJb0DRPDaAgN7rS",
"email": "myemail@email.com"
}
所以我无法处理错误,而是在我的浏览器中收到 HTTP 500 错误,您知道原因吗?
这是处理 $result 的方式,如果信用卡有效,它可以正常工作,我确实收到了订阅代码:
try {
if(substr( $results, 0, 3 ) === "sub"){
echo $results;
}
} catch (ParseException $e) {
echo 'Caught exception: '.$e->getMessage()."\n";
}
所以当我使用 4242424242424242 信用卡时,我确实收到了订阅代码,但是当我使用信用卡号 4100000000000019 强制出错时,我无法收到错误消息。
这正是我在使用 PHP 时收到的显示错误 ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
收到错误:
致命错误:未捕获 Parse\ParseException:您的卡被拒绝。在 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseClient.php:357 堆栈跟踪中:#0 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseCloud.php(32): Parse\ParseClient::_request('POST', 'functions/crear...', NULL, '{"stripeToken":...', false) #1 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/suscripcion.php(28): Parse\ParseCloud::运行('crearCargo', Array) #2 {main} 抛入 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseClient.php 第 357 行
我怀疑问题是您的 ParseCloud::run
调用不在您的 try-catch 块中。尝试这样的事情:
try {
$results = ParseCloud::run("crearCargo", ["stripeToken" => "$stripeToken", "email" => "$email"]);
}
catch(ParseException $e) {
echo 'Caught exception: '.$e->getMessage()."\n";
}
catch(Exception $e) {
// do something with other exceptions, for testing we'll just print it out
print_r($e);
}