dwmsw/sagepay

Sagepay Direct V3 Wrapper

1.9.0 2018-03-10 00:34 UTC

This package is auto-updated.

Last update: 2024-11-12 04:24:40 UTC


README

Build Status Code Climate Test Coverage

Description

dwmsw/sagepay is a library for interacting with the Sagepay Direct v3.00 protocol

It aims to make interacting with Sagepay as easy as possible and is available as a composer package on packagist

This wrapper doesn't include any database implementations, it is purely here to make interacting with the Sagepay API easier and to add a level of validation. All methods that send the data to Sagepay will return the response to you in an associative array, with nothing added or removed.

It has been built this way as to not restrict developers to certain database implementations or structures. We prefer for you to be able to use the data however you like.

##Installation

Installing via composer is the best way. Run the following from the CLI

composer require dwmsw/sagepay

What is implemented?

  • Payments (with or without tokens)
  • Deferred Payments w/ release
  • Discounts
  • Refunds
  • 3D Secure

Basic Usage

Payment

// Create instance of Direct
$sagepay = new dwmsw\sagepay\Direct();

// New Basket instance
$basket = new dwmsw\sagepay\Basket();
// Add an item to the basket
$basket->addItem(new dwmsw\sagepay\Item('Test Item', 30.00, 6, 1));
// Add another item to the basket
$basket->addItem(new dwmsw\sagepay\Item('Test Item Two', 30.00, 6, 2));
// Add a discount
$basket->addDiscount(new dwmsw\sagepay\Discount(50.00, 'This is a discount'));

// Set the Basket
$sagepay->setBasket($basket);

// Set up the config
$sagepay->setConnectionMode(CONNECTION MODE TEST/LIVE);
$sagepay->setVendorName(YOUR VENDOR NAME);
$sagepay->setCurrency('GBP');
$sagepay->setApplyAvsCv2(1);
$sagepay->setApply3dSecure(0);
$sagepay->setGiftAid(0);

$vendorTxCode = md5(rand(1, 1000));

// TX Specific bits
$sagepay->setVendorTxCode($vendorTxCode);
$sagepay->setDescription('Test Payment');
$sagepay->setCustomerEmail('daryll@digitalwebmedia.co.uk');

// Set up addresses
$BillingAddress = new dwmsw\sagepay\Address();
$BillingAddress->setName('Daryll', 'Doyle');
$BillingAddress->setPhone('46554789658');
$BillingAddress->setAddress('88', 'Test Address', 'Town', 'GB', '412');

// Set Addresses into the class
$sagepay->setBillingAddress($BillingAddress);
// Delivery Address can be a different instance of address if needed
$sagepay->setDeliveryAddress($BillingAddress);

// New card instance
$card = new dwmsw\sagepay\Card();
// Card details 
$card->setCardHolder('Mr D Doyle');
$card->setCardType('VISA');
$card->setCardNumber('4929000000006');
$card->setStartDate(false);
$card->setExpiryDate('1216');
$card->setCV2('123');

$sagepay->setCard($card);

$output = $sagepay->register('PAYMENT');

// Do whatever you want with $output

To use tokens with payments, you'll initially need to pass the following in your setup:

$sagepay->setCreateToken(1);

This will tell sagepay to return you a token when payments are successful. You can then use this token, to make further payments.

To make a payment using a token, you don't enter the card details, but instead use the following:

// New card instance
$card = new dwmsw\sagepay\Card();
$card->setToken('TOKEN');
$card->setCV2('CV2');

$sagepay->setCard($card);

When using a token, the class automatically sets StoreToken to 1 to persist the token. Although I will probably add an option to change this at some point!

DEFERRED

// As above, but use the following line
$output = $sagepay->register('DEFERRED');

RELEASE

// Create instance of Direct
$sagepay = new dwmsw\sagepay\Direct();
// Make the release
$output = $sagepay->release('VPSTxId', 'SecurityKey', 'vendorTxCode', 'TxAuthNo', 'AMOUNT TO RELEASE');
    
// Do whatever with $output

REFUND

// Create instance of Direct
$sagepay = new dwmsw\sagepay\Direct();
// Make the refund
$output = $sagepay->refund('NEWvendorTxCode', 'AMOUNT TO REFUND', 'OLDVPSTxId', 'OLDvendorTxCode', 'OLDSecurityKey', 'OLDTxAuthNo', 'Refund Message');
    
// Do whatever with $output

3D Secure

// Create instance of Direct
$sagepay = new dwmsw\sagepay\Direct();
$sagepay->setConnectionMode('test');

// Send the output back to Sagepay
$output = $sagepay->threeDResponse($_POST['MD'], $_POST['PaRes']);

To Do

  • Paypal Integration

Getting Involved

  • Open an issue with a feature you'd like
  • Make a PR
  • Write any tests that may be missing!