Reputation: 1618
I've implemented Omnipay paypal into my Laravel site. First I do an authorization call to paypal which looks like this:
$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');
}
After this the user gets redirected to pay in paypal's site and if successful gets redirected back to returnUrl. Where this happens:
$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');
}
However occasionally something fails with the redirect back from paypal, perhaps the browser shuts down or a network failure prevents the user from getting redirected back to my laravel site.
So I've tried to create a cron job (scheduler in laravel) which checks every 5 minutes for payments that are not complete and try check them if the payment has been transfered successfully and set the invoice status to payed if so:
$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);
But I only get this response from 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"
]
So how am I supposed to fetch the payment transaction when all I have is this from the authorization request made before the user gets redirected:
#data: array:6 [▼
"TOKEN" => "EC-9XF92859YM415352K"
"TIMESTAMP" => "2016-02-12T14:25:09Z"
"CORRELATIONID" => "e6a70075ad9d5"
"ACK" => "Success"
"VERSION" => "119.0"
"BUILD" => "18308778"
]
I have tried fetching the transaction with both the token and the correlationid, but none of them are valid resource id's
Upvotes: 1
Views: 1659
Reputation: 3681
So, for example, looking at your initial code, I have added some comments as to the changes that I would make:
// 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();
Note that:
See the documentation in the omnipay-paypal RestFetchTransactionRequest, RestFetchPurchaseRequest and RestListPurchaseRequest classes for some examples.
Upvotes: 4