我希望您能帮助我解决在Laravel中与Paypal一起使用curl的问题。我正在将其与Ionic集成。当我按下购买按钮时,它中断了,该应用给出了以下错误:
无法获取/未定义。
我正在关注一个教程,其结果应该是,通过按下按钮进行购买,该应用会将用户重定向到Paypal。请注意,在按按钮出现错误后,控制台在开发人员工具上变为空白(可能是因为它试图将用户重定向到应用程序外部的贝宝?),因此我无法真正进一步调查错误,但是我提到的那些瞬间就可以看到,就是我提到的那些!
下面的代码与我正在遵循的教程完全相同,我知道它可能不会说太多,因为在其他地方可能有问题,但是确实如此。我将代码粘贴到Laravel api.php文件中,并且收到的错误向我显示了输出:“错误购买!-1”,这是我在Laravel的第二个“ else”块中打印的消息代码,这可能暗示了我无法捕获的问题。我还将粘贴该函数的功能称为api的ionic应用程序。
我知道我可能会提供有限的信息,所以请让我知道您还需要什么其他信息,甚至可以帮助我进一步调试这些信息!
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");
}
);
}
答案 0 :(得分:0)
@BizzyBob感谢您的建议,最后我发现我为参数添加了错误的值:
$ data ['L_PAYMENTREQUEST_0_QTY0'] ='1';
以前我有:
$ data ['L_PAYMENTREQUEST_0_QTY0'] ='100';
将其更改为“ 1”解决了该问题。在我看来,很难找到解决方案,因为我们一直在其他地方寻找解决方案...因此,我想提醒一下外面的任何人,请务必仔细检查参数值,否则可能会引起错误,您可能会尝试这样做在不同的地方调试!