Skip to content

Commit

Permalink
Added WorldPayExtension (#165)
Browse files Browse the repository at this point in the history
* WorldPay Extension
  • Loading branch information
mlewis-everley authored and bummzack committed May 31, 2017
1 parent 60d5856 commit 1ae0c32
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
63 changes: 63 additions & 0 deletions code/extensions/WorldPayResponseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use SilverStripe\Omnipay\Service\ServiceResponse;

/**
* Add specific response overwrites for WorldPay gateway responses.
* This allows us to provide a templated response to a WorldPay
* payment notification and auto redirect to the current site after
* payment.
*
* If you need to accept payments via WorldPay and want to use the
* custom payment response page, then you need to enable this extension
* in your config.yml:
*
* SilverStripe\Omnipay\Service\PaymentService:
* extensions:
* - WorldPayResponseExtension
*
* If you would like to overwrite the default template (for example
* to use your own styling, WPDISPLAY tags, etc) then add a
* "WorldPayCallback.ss" file to the tempaltes directory in your
* theme.
*
* @package silverstripe-omnipay
* @subpackage extensions
* @author Bummzack
* @author Mo <[email protected]>
*/
class WorldPayResponseExtension extends Extension
{
public function updateServiceResponse($response)
{
$payment = $response->getPayment();

// We only want to respond to the notification if we are using WorldPay
if ($payment->Gateway !== 'WorldPay') {
return;
}

// Ignore payments that aren't in the PendingPurchase state
if ($payment->Status !== 'PendingPurchase') {
return;
}

$omnipayResponse = $response->getOmnipayResponse();

if ($omnipayResponse !== null && !$response->isError()) {

if ($omnipayResponse->isSuccessful()) {
$return_url = Director::absoluteURL($payment->SuccessUrl, true);
} else {
$return_url = Director::absoluteURL($payment->FailureUrl, true);
}

$viewer = new SSViewer("WorldPayCallback");
$html = $viewer->process(ArrayData::create(array(
"ReturnURL" => $return_url
)));

$response->setHttpResponse(new SS_HTTPResponse($html, 200));
}
}
}
71 changes: 71 additions & 0 deletions docs/en/WorldPaySetup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Setting Up WorldPay payments

In order to start taking payments with WorldPay you will need to
create an account with [Worldpay](http://www.worldpay.com/).

next you will need to install the Omnipay WorldPay libraries
(ideally via composer):

# composer require "omnipay/worldpay:~2.0"

## Setup WorldPay on your install

Once you have the WorldPay module installed, add the following
to `mysite/_config/payment.yml`

````
---
Name: payment
---
Payment:
allowed_gateways:
- 'WorldPay'
GatewayInfo:
WorldPay:
parameters:
installationId: '1010618'
callbackPassword: 'xyz'
---
Except:
environment: 'live'
---
GatewayInfo:
WorldPay:
parameters:
testMode: true
---
Only:
environment: 'live'
---
GatewayInfo:
WorldPay:
parameters:
callbackPassword: 'abc'
````

The `installationId` will be provided by WorldPay when your account is
setup or can be retrieved from the "Installations" screen of the [WorldPay Admin](https://secure.worldpay.com/sso/public/auth/login.html).

The `callbackPassword` can be set in the WorldPay admin > Installations > Installation Adminiatration (using the 'Payment Response password' field).
**Note** You can add different passwords for test and live environments.

Additional supported settings can be found at the [omnipay-worldpay github page](https://github.com/thephpleague/omnipay-worldpay/blob/master/src/Gateway.php#L21).

## Using a custom callback response

If you want a custom callback response (so the user sees a styled page at
the end of the payment process and is returned back to your site) you
will need to enable the `WorldPayResponseExtension`.

You can add the following to your `config.yml` to achieve this:

````
SilverStripe\Omnipay\Service\PaymentService:
extensions:
- WorldPayResponseExtension
````

If you want to customise the apperance of the response page, just copy
the `WorldPayCallback.ss` template from this module and add it to your
`templates` folder.
23 changes: 23 additions & 0 deletions templates/WorldPayCallback.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<head>
<meta http-equiv="refresh" content="2;url={$ReturnURL}" />
<title><%t WorldPayExtension.Processing "Processing" %></title>
</head>

<body>
<div style="font-family:sans-serif;text-align:center;" class="payment-processing">
<h1><%t WorldPayExtension.Processing "Processing" %></h1>

<p>
<%t WorldPayExtension.RedirectingToStore "We are now redirecting you, if you are not redirected automatically then click the link below." %>
</p>

<p>
<a href="{$ReturnURL}"><%t WorldPayExtension.ReturnToStore "Return To Merchant's Store" %></a>
</p>

<WPDISPLAY ITEM="banner">
</div>
</body>
</html>

0 comments on commit 1ae0c32

Please sign in to comment.