我也遇到了這個問題 - 在我的情況下,我實際上發送了虛假的數據給貝寶。
在第一步嘗試捕獲異常,並獲得actualy errormessage的
// For testing purpose use the general exception (failed to catch with paypal for me)
catch (Exception $ex) {
if (\Config::get('app.debug')) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
$err_data = json_decode($ex->getData(), true);
exit;
} else {
die('Some error occur, sorry for inconvenient');
}
}
得到的消息應該給你足夠的信息來解決你得到了什麼錯。
下面我粘貼你我的代碼,這是我使用paypal REST api工作。您將需要3路
- /支付/創建(創建付款)
- /支付/成功(確認付款成功&來自PayPal重定向)
- /支付/取消(貝寶的手柄取消)
您還需要添加您的paypal配置並將其初始化到您的控制器中。如果您還沒有PayPal配置文件,您可以直接在該功能中設置客戶端ID和密碼。這些設置應該是這樣的
'settings' => array(
/**
* Available option 'sandbox' or 'live'
*/
'mode' => 'sandbox',
/**
* Specify the max request time in seconds
*/
'http.ConnectionTimeOut' => 30,
/**
* Whether want to log to a file
*/
'log.LogEnabled' => true,
/**
* Specify the file that want to write on
*/
'log.FileName' => storage_path() . '/logs/paypal.log',
/**
* Available option 'FINE', 'INFO', 'WARN' or 'ERROR'
*
* Logging is most verbose in the 'FINE' level and decreases as you
* proceed towards ERROR
*/
'log.LogLevel' => 'FINE'
)
控制器
$paypal_conf = config('paypal');
$this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
$this->_api_context->setConfig($paypal_conf['settings']);
的構造函數創建路線
// create a payment
public function create(Request $request)
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$price = '10.00'; // 10 € for example
if($price == 0) { // ensure a price above 0
return Redirect::to('/');
}
// Set Item
$item_1 = new Item();
$item_1->setName('My Item')
->setCurrency('EUR')
->setQuantity(1)
->setPrice($price);
// add item to list
$item_list = new ItemList();
$item_list->setItems(array($item_1));
$amount = new Amount();
$amount->setCurrency('EUR')
->setTotal($price); // price of all items together
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Fitondo Fitnessplan');
$redirect_urls = new RedirectUrls();
$redirect_urls->setReturnUrl(URL::to('/payment/status'))
->setCancelUrl(URL::to('/payments/cancel'));
$payment = new Payment();
$payment->setIntent('Sale')
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions(array($transaction));
try {
$payment->create($this->_api_context);
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
if (config('app.debug')) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
$err_data = json_decode($ex->getData(), true);
exit;
} else {
die('Error.');
}
}
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
/* here you could already add a database entry that a person started buying stuff (not finished of course) */
if(isset($redirect_url)) {
// redirect to paypal
return Redirect::away($redirect_url);
}
die('Error.');
}
成功路線
public function get(Request $request)
{
// Get the payment ID before session clear
$payment_id = $request->paymentId;
if (empty($request->PayerID) || empty($request->token)) {
die('error');
}
$payment = Payment::get($payment_id, $this->_api_context);
// PaymentExecution object includes information necessary
// to execute a PayPal account payment.
// The payer_id is added to the request query parameters
// when the user is redirected from paypal back to your site
$execution = new PaymentExecution();
$execution->setPayerId($request->PayerID);
//Execute the payment
$result = $payment->execute($execution, $this->_api_context);
if ($result->getState() == 'approved') { // payment made
/* here you should update your db that the payment was succesful */
return Redirect::to('/this-is-what-you-bought')
->with(['success' => 'Payment success']);
}
return Redirect::to('/')
->with(['error' => 'Payment failed']);
}
我希望我得到的一切 - 我不得不清理我的編碼以簡化它。
嘗試'dd($ payment-> getId());'Session :: put('paypal_payment_id',$ payment-> getId());'之前看看它的迴應是什麼。 – Abbasi
第二件事是確保您向paypal提供所有數據,否則它會拋出相同的異常。 – Abbasi
嗨穆罕默德感謝您的答覆。我在PayPalHttpConnection.php第176行中得到了同樣的錯誤「PayPalConnectionException:176: 訪問https://api.sandbox.paypal.com/v1/payments/payment時得到了Http響應代碼400」。 。我怎麼能檢查,看看我是否提供所有的數據貝寶。 –