我希望你能帮我解决这个问题,我正在尝试使用curl与Paypal在Laravel。我要把它和Ionic整合起来。当我按下要购买的按钮时,它会中断,而应用程序会出现以下错误:
无法获得/undefined。
我遵循一个教程,结果应该是通过按下购买按钮,应用程序将用户重定向到Paypal。请注意,在我按下按钮后,控制台在开发工具上变为空白(可能是因为它试图将用户重定向到paypal,在应用程序之外吗?),所以我不能进一步调查这些错误,但是我可以在空白之前眨眼看到的错误就是我提到的错误!
下面的代码与我所遵循的教程完全相同,我知道它可能不会说太多,因为在其他地方可能会有问题,但也有。我正在将代码粘贴在Laravel api.php文件中,我得到的错误显示了输出:“错误购买!- 1”--这是我在最后一个与Laravel代码的第二个“of”块中打印的消息,这可能是我无法捕捉到的问题的提示。我还在调用api的离子应用程序中粘贴函数。
我知道我可能提供有限的信息,所以让我知道你需要的其他信息,或甚至帮助我如何进一步调试这将是非常感谢!
Route::middleware('auth:api')->post('/paypal', function (Request $request) {
$user = $request->user();
$data = $request->all();
$list_products_id = $data;
$products = [];
$total = 0;
$titles = '';
foreach($list_products_id as $key => $value) {
$product = Product::find($value);
if($product){
$products[$key] = $product;
$total += $product->price;
$titles .= $product->title." ";
}
}
if($total){
$paypal = config('app.paypal', "sandbox");
if($paypal == "sandbox"){
$userProvider = '';
$pwdProvider = '';
$signProvider = '';
$url = 'https://api-3t.sandbox.paypal.com/nvp';
$url2 = 'https://www.sandbox.paypal.com/cgi-bin/webscr?%s';
} else {
$userProvider = '';
$pwdProvider = '';
$signProvider = '';
$url = 'https://api-3t.paypal.com/nvp';
$url2 = 'https://www.paypal.com/cgi-bin/webscr?%s';
}
$data = [];
$data['USER'] = $userProvider;
$data['PWD'] = $pwdProvider;
$data['SIGNATURE'] = $signProvider;
$data['METHOD'] = 'SetExpressCheckout';
$data['VERSION'] = '108';
$data['LOCALECODE'] = 'en_US';
$data['L_PAYMENTREQUEST_0_NAME0'] = "Products Orders";
$data['L_PAYMENTREQUEST_0_DESC0'] = $titles;
$data['PAYMENTREQUEST_0_AMT'] = number_format($total, 2).'';
$data['PAYMENTREQUEST_0_CURRENCYCODE'] = 'EUR';
$data['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale';
$data['L_PAYMENTREQUEST_0_QTY0'] = '100'; //number of the same product the user is ordering
$data['L_PAYMENTREQUEST_0_AMT0'] = number_format($total, 2).'';
$data['L_BILLINGAGREEMENTDESCRIPTION0'] = $titles;
$data['CANCELURL'] = url('/');
$data['RETURNURL'] = url('/');
// curl
$data = http_build_query($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
$nvp = array();
if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
foreach ($matches['name'] as $offset => $name) {
$nvp[$name] = urldecode($matches['value'][$offset]);
}
}
if(isset($nvp['ACK']) && $nvp['ACK'] == "Success" ){
$query = array(
'cmd' => '_express-checkout',
'token' => $nvp['TOKEN']
);
$redirectURL = sprintf($url2, http_build_query($query));
return ['date'=>$redirectURL];
}else{
return ['status'=>'error purchasing! - 1'];
}
}
echo "total: " . $total;
return ['status'=>'error purchasing! - 2'];
});下面是离子密码:
purchase() {
console.log("Order");
if (!this.user) {
this.navCtrl.push(LoginPage);
return;
}
this.cartProvider.purchase(this.cart, this.user).subscribe(
res => {
if (res) {
console.log(res);
// this.showToast("top", "Checkout completed successfully!");
window.location.href = res.date;
this.removeAll();
}
},
error => {
console.log("Error: " + error);
this.showToast("top", "Error checking out");
}
);
}发布于 2019-05-02 17:39:21
@BizzyBob谢谢您的建议,最后我给参数添加了一个错误的值:
$data‘l_PAYMENTREQUEST_QTY0 0’= '1';
以前我有:
$data‘l_PAYMENTREQUEST_QTY0 0’= '100';
将其改为"1“解决了这个问题。在我看来很难发现我们一直在寻找其他地方的解决方案。所以,只要快速提醒外面的任何人,总是重复检查参数值和诸如此类的东西,因为它们可能会导致错误,您可能会尝试在完全不同的地方调试!
https://stackoverflow.com/questions/55911658
复制相似问题