integration – simple sagepay payment php-ThrowExceptions

Exception or error:

I’ve been given the task of redesigning a site where the user ends up buying a ticket for an event.

The old site used a PayPal buy now button but the client wants the site to do the same thing but wants to use SagePay instead.

The site’s at the stage where the variables $ticketNumber and $ticketAmount are displayed in a confirm page, and this is where the previous design had a PayPal button, so I have to somehow integrate SagePay here.

I’ve been all over the SagePay site and documentation, I’ve downloaded the Integration_and_Protocol_Guidelines pdf’s and the php integration kit, but I can’t make heads or tails out of anything as there are about a hundred pages, includes, libraries, and templates, etc.

I’m not bothered if I use the drop-in or own form method as the site is secured.

I have a test vendorname, Integration Key and Integration Password from sagepay.

I was told by sagepay tech support that the sage pay pi is the way to go, but the 3 easy steps it explains are still confusing to me.

I don’t have any code to show as everything I’ve tried displays a blank page, so I’m doing something wrong.

What I did do (using the drop-in method), was I added these js includes in the confirm page:

<script src="https://pi-test.sagepay.com/api/v1/js/sagepay.js"></script>
<script src="https://pi-test.sagepay.com/api/v1/js/sagepay-dropin.js">

then I added this script under the confirmation text where it displays the ticket number and amount:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/merchant-session-keys",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => '{ "vendorName": "<myvendorname>" }',
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic <Base64 Encoded Integration Key:Integration Password>",
    "Cache-Control: no-cache",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
?>

Then under that, I added this code:

<form>
  <h2>Payment Details</h2>
    <div id="sp-container"></div>
    <div id="submit-container">
      <input type="submit">
    </div>
</form>
<script>
  sagepayCheckout({ merchantSessionKey: 'F42164DA-4A10-4060-AD04-F6101821EFC3' }).form();
</script>

It did display a card input form, but nothing after that when I clicked the submit button.

I did add this code after the form thinking that it would use it to send the payment out, but the page just goes blank:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/transactions",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => '{' .
                          '"transactionType": "Payment",' .
                          '"paymentMethod": {' .
                          '    "card": {' .
                          '        "merchantSessionKey": "' . $merchantSessionKey . '",' .
                          '        "cardIdentifier": "' . $cardIdentifier . '"' .
                          '    }' .
                          '},' .
                          '"vendorTxCode": "$ticketNo",' .
                          '"amount": 100,' .
                          '"currency": "GBP",' .
                          '"description": "Online Payment",' .
                          '"apply3DSecure": "UseMSPSetting",' .
                          '"customerFirstName": "Sam",' .
                          '"customerLastName": "Jones",' .
                          '"billingAddress": {' .
                          '    "address1": "407 St. John Street",' .
                          '    "city": "London",' .
                          '    "postalCode": "EC1V 4AB",' .
                          '    "country": "GB"' .
                          '},' .
                          '"entryMethod": "Ecommerce"' .
                        '}',
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic <Base64 Encoded Integration Key:Integration Password>",
    "Cache-Control: no-cache",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
?>

I also have to get the user to input their billing address etc., as that seems to be required by sagepay, so I have to think about grabbing that from them somewhere.

I don’t know where I’m going wrong as the instructions are throwing a lot of code at me without telling me what to do with it. I read somewhere in their documentation that I don’t need a lot of developer experience, I thought I’d give it a shot. I’m not a complete novice, but I am pretty new to php as my career has kept me in ASP for many many years.

Any help would be greatly appreciated.

Thanks in advance.
Nico

How to solve:

I don’t have any code to show as everything I’ve tried displays a blank page, so I’m doing something wrong.

I am pretty new to php as my career has kept me in ASP for many many years.

The first thing I would do is a sanity check. When PHP returns a blank page this is often because there are errors being kept from you (eg PHP is running in production configuration). So the first thing you should do is this:

Create the following code in a file that the web server is obviously going to open (I’d use index.php)

<?php
echo('This is some PHP');

You should see the words ‘This is some PHP’ on the screen.

Now try this:

<?php
echo($This_is_some_PHP);

As long as you don’t have a variable called $This_is_some_PHP (please say you don’t – and if you do, please comment it out for now) – then you should see an error on-screen.

If you see a blank screen your PHP is running in production config – switch it to developer config by enabling printing errors to the screen and error reporting. That way when things go wrong you at least start to get the opportunity to debug them. Then you can start to debug the issue.

Answer´╝Ü

Please check the Apache or Nginx logs. I believe that you don’t have curl installed.

Leave a Reply

Your email address will not be published. Required fields are marked *