我在我的Laravel网站上实施了Omnipay paypal。 首先,我对paypal进行授权调用,如下所示:
$invoice = $this->find($id);
$gateway = Omnipay::create('PayPal_Express');
$gateway->setUsername(config('payment.paypal_api_username'));
$gateway->setPassword(config('payment.paypal_api_password'));
$gateway->setSignature(config('payment.paypal_signature'));
$gateway->setTestMode(config('payment.paypal_testmode'));
$response = $gateway->purchase([
'cancelUrl' => route('paypal.cancel'),
'returnUrl' => route('paypal.return'),
'amount' => $invoice->price.'.00',
'currency' => $invoice->currency->abbr,
'Description' => 'Sandbox test transaction'
])->send();
if($response->isRedirect()) {
// Redirect user to paypal login page
return $response->redirect();
} else {
Flash::error('Unable to authenticate against PayPal');
}
在此之后,用户被重定向到paypal的网站付费,如果成功则重定向回returnUrl。发生这种情况的地方:
$payment = Payment::where('paypal_token', '=', $token)->first();
$invoice = $payment->invoice;
$gateway = Omnipay::create('PayPal_Express');
$gateway->setUsername(config('payment.paypal_api_username'));
$gateway->setPassword(config('payment.paypal_api_password'));
$gateway->setSignature(config('payment.paypal_signature'));
$gateway->setTestMode(config('payment.paypal_testmode'));
$response = $gateway->completePurchase(array (
'cancelUrl' => route('paypal.cancel'),
'returnUrl' => route('paypal.success'),
'amount' => $invoice->price.'.00',
'currency' => $invoice->currency->abbr
))->send();
if($response->isSuccessful()) {
Event::fire(new MakePaymentEvent($invoice));
Flash::message('Thank you for your payment!');
} else {
Flash::error('Unable to complete transaction. Check your balance');
}
然而,偶尔会因为从PayPal重定向而失败,可能是浏览器关闭或网络故障导致用户无法重定向回我的laravel网站。
因此,我尝试创建一个cron作业(laravel中的调度程序),每隔5分钟检查一次未完成的付款,如果付款已成功转移,请尝试检查它们,并将发票状态设置为付款这样:
$gateway = Omnipay::create('PayPal_Rest');
$gateway->initialize(array (
'clientId' => config('payment.paypal_client_id'),
'secret' => config('payment.paypal_secret_id'),
'testMode' => config('payment.paypal_testmode'),
));
$transaction = $gateway->fetchPurchase();
$transaction->setTransactionReference($token);
$response = $transaction->send();
$data = $response->getData();
dd($data);
但我只能从paypal api获得此回复
array:4 [
"name" => "INVALID_RESOURCE_ID"
"message" => "The requested resource ID was not found"
"information_link" => "https://developer.paypal.com/webapps/developer/docs/api/#INVALID_RESOURCE_ID"
"debug_id" => "8240e7d79fa91"
]
那么,如果我拥有的是在用户被重定向之前发出的授权请求,我该如何获取支付交易:
#data: array:6 [▼
"TOKEN" => "EC-9XF92859YM415352K"
"TIMESTAMP" => "2016-02-12T14:25:09Z"
"CORRELATIONID" => "e6a70075ad9d5"
"ACK" => "Success"
"VERSION" => "119.0"
"BUILD" => "18308778"
]
我尝试使用令牌和correlationid获取事务,但它们都不是有效的资源ID
答案 0 :(得分:2)
因此,例如,查看您的初始代码,我已经添加了一些关于我将做出的更改的评论:
// OK I'm assuming that your $id is a transaction ID, so we will use that. $invoice = $this->find($id); // Make the purchase call $response = $gateway->purchase([ // Add $id to these two URLs. 'cancelUrl' => route('paypal.cancel') . '/' . $id, 'returnUrl' => route('paypal.return') . '/' . $id, 'amount' => $invoice->price.'.00', 'currency' => $invoice->currency->abbr, 'Description' => 'Sandbox test transaction' ])->send(); if($response->isRedirect()) { // Get the transaction reference. $txnRef = $response->getTransactionReference(); // Store the above $txnRef in the transaction somewhere. // This will be the transaction reference you need to search for. // Redirect user to paypal login page return $response->redirect();
请注意:
有关示例,请参阅omnipay-paypal RestFetchTransactionRequest,RestFetchPurchaseRequest和RestListPurchaseRequest类中的文档。