0

Does anyone have an example I can follow of integrating the SagePay server integration method (payments taken off site) using Omnipay? Unfortunately the documentation is a little sparse and I'm just getting into a muddle with what methods I'm meant to be using!

This is my code (I'm using CodeIgniter):

use Omnipay\Omnipay;

class Payment_gateway extends CI_Controller {

//live details
private $live_merchant_id = 'xxx';
//test details
private $test_merchant_id = 'yyy';

//payment settings
private $test_mode = TRUE;
private $merchant_id = '';
private $gateway = NULL;

public function __construct() 
{
    parent::__construct();

    if ($this->test_mode) :
        $this->merchant_id = $this->test_merchant_id;
    else :
        $this->merchant_id = $this->live_merchant_id;
    endif;


    $this->gateway = Omnipay::create('SagePay_Server');
    $this->gateway->setVendor($this->merchant_id);
    $this->gateway->setTestMode($this->test_mode);
    $this->gateway->setSimulatorMode($this->test_mode);
}


public function index()
{

    $response = $this->gateway->Purchase();

    var_dump($response);

}
}

I do get a response object using the code above. If I try using:

if ($response->isRedirect()) :
        $response->redirect(); // this will automatically forward the customer
else :
    // not successful
endif;

instead I get an error:

Fatal error: Call to undefined method Omnipay\SagePay\Message\ServerPurchaseRequest::isRedirect()

If I try using:

$response = $this->gateway->Purchase()->send();

I get a message saying certain parameters are required, so if I add them in:

$response = $this->gateway->Purchase(array(
        'returnUrl' => 'http://www.madeupdomain.com/',
        'amount' => '10.00'
    ))->send();

I get the error:

The card parameter is required.

If I try:

$response = $this->gateway->completePurchase(array(
        'transactionId' => 111,
        'transactionReference' => 'Online payment'
    ))->send();

I get:

Invalid response from payment gateway

Updated: I misinterpreted the "card parameter is required" error as being I hadn't passed any credit card details to SagePay and not that I hadn't passed the card array within the completePurchase function, e.g., my index function should look like:

public function index()
{    
$response = $this->gateway->Purchase(array(
        'description'=> 'Online order',
        'currency'=> 'GBP',
        'transactionId'=> mt_rand(99, 9999),
        'transactionReference' => 'test order',
        'amount'=> '1.00',
        'returnUrl' => 'http://www.test.com/returnURL/',
        'cancelUrl' => 'http://www.test.com/cancelURL/',
        'card' => array(
            'email' =>  'test@test.com',
            'clientIp' => '123.123.123.123',
            'firstName' => 'Joe',
            'LastName' => 'Bloggs',
            'billingAddress1' => 'Address 1',
            'billingAddress2' => 'Address 2',
            'billingCity' => 'City',
            'billingPostcode' => 'AB1 1BA',
            'billingCountry' => 'GB',
            'billingPhone' => '01234 567 890'
        )))->send();

     $response = $this->gateway->Purchase($this->params)->send();
   if ($response->isRedirect()) {
        $response->redirect();
    } else {
        echo '<pre> ';
        var_dump($response);
        echo '</pre> ';
        exit;
        }
    }

This gives me a whole new problem but I think I'm now using the correct function.

JoJo
  • 161
  • 1
  • 12
  • Realised that the error "the card parameter is required" is referring to passing an array key of "card" in the purchase function and is not referring to a credit card number e.g. – JoJo Jul 21 '14 at 07:18
  • $response = $this->gateway->Purchase(array( 'description'=> 'Online order', 'currency'=> 'GBP', 'transactionId'=> mt_rand(99, 9999), 'transactionReference' => 'test order', 'amount'=> '1.00', 'returnUrl' => 'http://www.domain.com/return/', 'cancelUrl' => 'http://www.domain.com/cancel/', 'card' => array( 'country' => 'GB', 'email' => 'test@domain.com', 'clientIp' => '123.123.123.123', 'firstName' => 'Joe', 'LastName' => 'Bloggs' ))->send(); – JoJo Jul 21 '14 at 07:20
  • Can you update your question based on the findings in your comments? Struggling to follow the problem with so many different threads. – beech Jul 22 '14 at 11:48
  • I have another thread relating to Sagepay and Omnipay with a working example (using Sagepay Server protocol 3). See http://stackoverflow.com/questions/29370534/does-anyone-have-a-working-example-of-omnipay-and-sagepay-server-or-sagepay-dire – JoJo Apr 08 '15 at 13:57

0 Answers0