thephpleague / omnipay-sagepay

Sage Pay driver for the Omnipay PHP payment processing library
MIT License
54 stars 78 forks source link

SagePay\Form redirect #144

Closed dan-pascu closed 4 years ago

dan-pascu commented 4 years ago

Hi,

I'm trying to use Laravel 6 with SagePay\Form and, as per the documentation, I have the following:

$gateway = OmniPay::create('SagePay\Form')->initialize([ 'vendor' => 'my_vendor', 'testMode' => true, 'encryptionKey' => 'my_encryption_key', ]);

$response = $gateway->authorize([ 'amount' => '9.99', 'currency' => 'GBP', 'transactionId' => 12345,, 'returnUrl' => 'https://example.com/success', 'failureUrl' => 'https://example.com/failure', ]);

$response->redirect();

And I get this error Call to undefined method Omnipay\SagePay\Message\Form\AuthorizeRequest::redirect() When I dd the reponse I have this: image

I think I'm missing something but I cannot figure out what, can you guide me please?

judgej commented 4 years ago

It looks like you have set $response to a request. To get the response, you need to "send" it. That does not actually send anything to Sage Pay, but it keeps the steps consistent across different gateways.

$gateway = OmniPay::create('SagePay\Form')->initialize([...]);
$request = $gateway->authorize([...]);
$response = $request->send(); // The step you have have overlooked
$response->redirect();

Or for the last step. you can construct your own form or redirect to suit your framework using:

$method = $response->getRedirectMethod();
$url = $response->getRedirectUrl()
$hiddenFormItems = $response->getRedirectData();

So for Laravel constructing a self-submitting form, or a form with a button to redirect, would be the preference.

<form method="{{ $method }}" action="{{ $url }}">
@foreach($hiddenFormItems as $name => $value)
<input type="hidden" name="{{ $name }}" value="{{ $value }}" />
@endforeach
<button type="submit">Go pay!</button>
</form>
dan-pascu commented 4 years ago

Following your instructions I manage to make it work. Thank you.

Previously I tried the send() method but it required a card, and for Form method I don't have a card to send. My mistake. Also in the card details are stored the billing and shipping information that are required and I didn't thought on this.

Another mistake I made is the encryptionKey. I included a wrong one and I received an encryption error: "openssl_encrypt(): IV passed is 20 bytes long which is longer than the 16 expected by selected cipher, truncating". I then used the correct Form Integration Encryption Password from SagePay and everything went fine.