Skip to content

Commit 7cf1f13

Browse files
authored
Merge pull request #116 from academe/issue112
Issue112 - Support Sage Pay Form
2 parents a24bfb3 + 1fa282a commit 7cf1f13

18 files changed

+1222
-93
lines changed

README.md

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ Table of Contents
3232
* [Server Authorize/Purchase](#server-authorizepurchase)
3333
* [Server Create Card](#server-create-card)
3434
* [Server Notification Handler](#server-notification-handler)
35-
* [Sage Pay Shared Methods (for both Direct and Server):](#sage-pay-shared-methods-for-both-direct-and-server)
35+
* [Sage Pay Form Methods](#sage-pay-form-methods)
36+
* [Form Authorize](#form-authorize)
37+
* [Form Purchase](#form-purchase)
38+
* [Sage Pay Shared Methods (Direct and Server)](#sage-pay-shared-methods-for-both-direct-and-server)
3639
* [Repeat Authorize/Purchase](#repeat-authorizepurchase)
3740
* [Capture](#capture)
3841
* [Delete Card](#delete-card)
@@ -69,6 +72,7 @@ The following gateways are provided by this package:
6972

7073
* SagePay_Direct
7174
* SagePay_Server
75+
* SagePay_Form
7276

7377
For general Omnipay usage instructions, please see the main
7478
[Omnipay](https://github.com/thephpleague/omnipay) repository.
@@ -586,7 +590,95 @@ The `$nextUrl` is where you want Sage Pay to send the user to next.
586590
It will often be the same URL whether the transaction was approved or not,
587591
since the result will be safely saved in the database.
588592

589-
## Sage Pay Shared Methods (for both Direct and Server):
593+
## Sage Pay Form Methods
594+
595+
Sage Pay Form requires neither a server-to-server back-channel nor
596+
IP-based security.
597+
The payment details are encrypted on the server before being sent to
598+
the gateway from the user's browser.
599+
The result is returned to the merchant site also through a client-side
600+
encrypted message.
601+
602+
Capturing and voiding `Form` transactions is a manual process performed
603+
in the "My Sage Pay" administration panel.
604+
605+
Supported functions are:
606+
607+
* authorize()
608+
* purchase()
609+
610+
### Form Authorize
611+
612+
The authorization is intialized in a similar way to a `Server` payment,
613+
but with an `encryptionKey`:
614+
615+
```php
616+
$gateway = OmniPay::create('SagePay\Form')->initialize([
617+
'vendor' => 'vendorname',
618+
'testMode' => true,
619+
'encryptionKey' => 'abcdef1212345678',
620+
]);
621+
```
622+
623+
The `encryptionKey` is generated in "My Sage Pay" when logged in as the administrator.
624+
625+
Note that this gateway will assume all inout data (names, addresses etc.)
626+
are UTF-8 encoded.
627+
It will then recode the data to ISO8859-1 before encrypting it for the gateway,
628+
as the gateway strictly accepts ISO8859-1 only, regardless of what encoding is
629+
used to submit the form from the merchant site.
630+
If you do not want this conversion to happen, it can be disabled with this parameter:
631+
632+
'disableUtf8Decode' => true,
633+
634+
The authorize must be given a `returnUrl` (the return URL on success, or on failure
635+
if no separate `failureUrl` is provided).
636+
637+
```php
638+
$response = $gateway->authorize([
639+
...all the normal details...
640+
//
641+
'returnUrl' => 'https://example.com/success',
642+
'failureUrl' => 'https://example.com/failure',
643+
]);
644+
```
645+
646+
The `$response` will be a `POST` redirect, which will take use to the gateway.
647+
At the gateway, the user will authenticate or authorise their credit card,
648+
perform any 3D Secure actions that may be requested, then will return to the
649+
merchant site.
650+
651+
To get the result details, the transaction is "completed":
652+
653+
```php
654+
// The result will be read and decrypted from the return URL (or failure URL)
655+
// query parameters:
656+
657+
$result = $gateway->completeAuthorize()->send();
658+
659+
$result->isSuccessful();
660+
$result->getTransactionReference();
661+
// etc.
662+
```
663+
664+
If you already have the encrypted response string, then it can be optionally
665+
passed in:
666+
667+
$result = $gateway->completeAuthorize(['crypt' => $crypt])->send();
668+
669+
This should normally not be necessary, but is handy for testing or if the
670+
current page query parameters are not available in a particular architecture.
671+
672+
### Form Purchase
673+
674+
This is the same as `authorize()`, but the `purchase()` request is used instead,
675+
and the `completePurchase()` request is used to complete the transaction on return.
676+
677+
## Sage Pay Shared Methods (Direct and Server)
678+
679+
Note: these functions do not work for the `Form` API.
680+
These actions for `Sage Pay Form` must be performed manually through the "My Sage Pay"
681+
admin panel.
590682

591683
* capture()
592684
* refund()

src/AbstractGateway.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Omnipay\SagePay;
4+
5+
use Omnipay\Common\AbstractGateway as OmnipayAbstractGateway;
6+
use Omnipay\SagePay\Traits\GatewayParamsTrait;
7+
8+
abstract class AbstractGateway extends OmnipayAbstractGateway implements ConstantsInterface
9+
{
10+
use GatewayParamsTrait;
11+
12+
/**
13+
* Examples for language: EN, DE and FR.
14+
* Also supports a locale format.
15+
*/
16+
public function getDefaultParameters()
17+
{
18+
return [
19+
'vendor' => null,
20+
'testMode' => false,
21+
'referrerId' => null,
22+
'language' => null,
23+
'useOldBasketFormat' => false,
24+
'exitOnResponse' => false,
25+
'apply3DSecure' => null,
26+
'useAuthenticate' => null,
27+
'accountType' => null,
28+
];
29+
}
30+
}

src/ConstantsInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ interface ConstantsInterface
116116
const SERVICE_TOKEN = 'directtoken';
117117
const SERVICE_DIRECT3D = 'direct3dcallback';
118118

119+
/**
120+
* 0 = Do not send either customer or vendor emails
121+
* 1 = Send customer and vendor emails if addresses are provided
122+
* 2 = Send vendor email but NOT the customer email
123+
* If you do not supply this field, 1 is assumed and emails
124+
* are sent if addresses are provided.
125+
*/
126+
const SEND_EMAIL_NONE = '0';
127+
const SEND_EMAIL_BOTH = '1';
128+
const SEND_EMAIL_VENDOR = '2';
129+
119130
//
120131
// Then the response constants.
121132
//

src/DirectGateway.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Omnipay\SagePay;
44

5-
use Omnipay\Common\AbstractGateway;
6-
use Omnipay\SagePay\Traits\GatewayParamsTrait;
75
use Omnipay\SagePay\Message\DirectAuthorizeRequest;
86
use Omnipay\SagePay\Message\DirectCompleteAuthorizeRequest;
97
use Omnipay\SagePay\Message\DirectPurchaseRequest;
@@ -20,36 +18,15 @@
2018
* Sage Pay Direct Gateway
2119
*/
2220

23-
class DirectGateway extends AbstractGateway implements ConstantsInterface
21+
class DirectGateway extends AbstractGateway
2422
{
25-
use GatewayParamsTrait;
26-
2723
// Gateway identification.
2824

2925
public function getName()
3026
{
3127
return 'Sage Pay Direct';
3228
}
3329

34-
/**
35-
* Examples for language: EN, DE and FR.
36-
* Also supports a locale format.
37-
*/
38-
public function getDefaultParameters()
39-
{
40-
return [
41-
'vendor' => null,
42-
'testMode' => false,
43-
'referrerId' => null,
44-
'language' => null,
45-
'useOldBasketFormat' => false,
46-
'exitOnResponse' => false,
47-
'apply3DSecure' => null,
48-
'useAuthenticate' => null,
49-
'accountType' => null,
50-
];
51-
}
52-
5330
/**
5431
* Direct methods.
5532
*/

src/FormGateway.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Omnipay\SagePay;
4+
5+
use Omnipay\SagePay\Message\Form\AuthorizeRequest;
6+
use Omnipay\SagePay\Message\Form\CompleteAuthorizeRequest;
7+
use Omnipay\SagePay\Message\Form\CompletePurchaseRequest;
8+
use Omnipay\SagePay\Message\Form\PurchaseRequest;
9+
10+
/**
11+
* Sage Pay Server Gateway
12+
*/
13+
class FormGateway extends AbstractGateway
14+
{
15+
public function getName()
16+
{
17+
return 'Sage Pay Form';
18+
}
19+
20+
/**
21+
* Authorize a payment.
22+
*/
23+
public function authorize(array $parameters = array())
24+
{
25+
return $this->createRequest(AuthorizeRequest::class, $parameters);
26+
}
27+
28+
/**
29+
* Authorize and capture a payment.
30+
*/
31+
public function purchase(array $parameters = array())
32+
{
33+
return $this->createRequest(PurchaseRequest::class, $parameters);
34+
}
35+
36+
/**
37+
*
38+
*/
39+
public function completeAuthorize(array $parameters = array())
40+
{
41+
return $this->createRequest(CompleteAuthorizeRequest::class, $parameters);
42+
}
43+
44+
/**
45+
*
46+
*/
47+
public function completePurchase(array $parameters = array())
48+
{
49+
return $this->createRequest(CompletePurchaseRequest::class, $parameters);
50+
}
51+
}

0 commit comments

Comments
 (0)