Skip to content

Commit

Permalink
Added refund method and additional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mediabeastnz committed Feb 9, 2019
1 parent 3028fe9 commit d064f5a
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 56 deletions.
4 changes: 4 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mediabeastnz/omnipay-afterpay",
"type": "library",
"version": "2.0.0",
"version": "2.1.0",
"description": "AfterPay gateway for Omnipay payment processing library",
"keywords": [
"after pay",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
bootstrap="bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
31 changes: 31 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class Gateway extends AbstractGateway
{

public $countryCode = 'NZ';

/**
* @return string
*/
Expand Down Expand Up @@ -60,6 +63,23 @@ public function setMerchantSecret($value)
return $this->setParameter('merchantSecret', $value);
}

/**
* @return mixed
*/
public function getCountryCode()
{
return $this->getParameter('countryCode');
}

/**
* @param mixed $value
* @return $this
*/
public function setCountryCode($value)
{
return $this->setParameter('countryCode', $value);
}

/**
* Configuration Request.
*
Expand Down Expand Up @@ -97,4 +117,15 @@ public function completePurchase(array $options = array())
{
return $this->createRequest('\Omnipay\AfterPay\Message\CompletePurchaseRequest', $options);
}

/**
* Handle partial/full refunds
*
* @param array $options
* @return \Omnipay\Common\Message\ResponseInterface
*/
public function refund(array $options = array())
{
return $this->createRequest('\Omnipay\AfterPay\Message\RefundRequest', $options);
}
}
106 changes: 67 additions & 39 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
protected $liveEndpoint = 'https://api.secure-afterpay.com.au/v1';
protected $testEndpoint = 'https://api-sandbox.secure-afterpay.com.au/v1';
protected $liveEndpoint = 'https://api.afterpay.com/v1';
protected $testEndpoint = 'https://api-sandbox.afterpay.com/v1';


/**
Expand Down Expand Up @@ -53,29 +53,6 @@ public function getHeaders()
return $headers;
}


/**
* @param mixed $data
* @return \Omnipay\AfterPay\Message\Response
* @throws \Guzzle\Http\Exception\RequestException
*/
public function sendData($data)
{
$endpoint = $this->getEndpoint();
$httpMethod = $this->getHttpMethod();
$httpRequest = $this->httpClient->createRequest($httpMethod, $endpoint);
$httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2
$httpRequest->addHeader('Authorization', $this->buildAuthorizationHeader());
$httpRequest->addHeader('Content-type', 'application/json');
$httpRequest->addHeader('Accept', 'application/json');
$httpRequest->setBody(json_encode($data));
$httpResponse = $httpRequest->send();
$this->response = $this->createResponse(
$this->parseResponseData($httpResponse)
);
return $this->response;
}

/**
* @return string
*/
Expand All @@ -92,32 +69,83 @@ protected function getEndpoint()
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
}

/**
* @param \Guzzle\Http\Message\Response $httpResponse
* @return array
*/
protected function parseResponseData(GuzzleResponse $httpResponse)
public function sendData($data)
{
return $httpResponse->json();
// don't throw exceptions for 4xx errors
$this->httpClient->getEventDispatcher()->addListener(
'request.error',
function ($event) {
if ($event['response']->isClientError()) {
$event->stopPropagation();
}
}
);

// Guzzle HTTP Client createRequest does funny things when a GET request
// has attached data, so don't send the data if the method is GET.
if ($this->getHttpMethod() == 'GET') {
$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint() . '?' . http_build_query($data),
array(
'User-Agent' => $this->getUserAgent(),
'Accept' => 'application/json',
'Authorization' => $this->buildAuthorizationHeader(),
'Content-type' => 'application/json',
)
);
} else {
$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint(),
array(
'User-Agent' => $this->getUserAgent(),
'Accept' => 'application/json',
'Authorization' => $this->buildAuthorizationHeader(),
'Content-type' => 'application/json',
),
$this->toJSON($data)
);
}

try {
$httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35
$httpResponse = $httpRequest->send();
$responseBody = (string) $httpResponse->getBody();
$response = json_decode($responseBody, true) ?? [];

return $this->response = $this->createResponse($response);
} catch (\Exception $e) {
throw new InvalidResponseException(
'Error communicating with payment gateway: ' . $e->getMessage(),
$e->getCode()
);
}

}

public function toJSON($data, $options = 0)
{
if (version_compare(phpversion(), '5.4.0', '>=') === true) {
return json_encode($data, $options | 64);
}
return str_replace('\\/', '/', json_encode($data, $options));
}

/**
* @param mixed $data
* @return \Omnipay\AfterPay\Message\Response
*/
protected function createResponse($data)
{
return new Response($this, $data);
return $this->response = new Response($this, $data);
}

/**
* @return string
*/
protected function buildAuthorizationHeader()
{
$merchantId = $this->getMerchantId();
$merchantSecret = $this->getMerchantSecret();

return 'Basic ' . base64_encode($merchantId . ':' . $merchantSecret);
}

protected function getUserAgent() {
return 'Omnipay (Omnipay-Afterpay/'.PHP_VERSION.' ; '.$this->getMerchantId().')';
}
}
2 changes: 1 addition & 1 deletion src/Message/ConfigurationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ConfigurationRequest extends AbstractRequest
*/
public function getData()
{
return null;
return array();
}

/**
Expand Down
16 changes: 4 additions & 12 deletions src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

class PurchaseResponse extends Response
{
protected $liveScript = 'https://www.secure-afterpay.com.au/afterpay.js';
protected $testScript = 'https://www-sandbox.secure-afterpay.com.au/afterpay.js';
protected $script = 'https://portal.sandbox.afterpay.com/afterpay.js';

public function getRedirectMethod()
{
Expand All @@ -27,7 +26,6 @@ public function isRedirect()
*/
public function getRedirectResponse()
{
echo '<pre>'; print_r($this->data); echo '</pre>'; die();
$output = <<<EOF
<html>
<head>
Expand All @@ -37,15 +35,15 @@ public function getRedirectResponse()
<body>
<script>
window.onload = function() {
AfterPay.init();
AfterPay.init(countryCode: "%s");
AfterPay.redirect({token: "%s"});
};
</script>
</body>
</html>
EOF;

$output = sprintf($output, $this->getScriptUrl(), $this->getToken());
$output = sprintf($output, $this->getScriptUrl(), $this->getCountryCode(), $this->getToken());

return HttpResponse::create($output);
}
Expand All @@ -55,13 +53,7 @@ public function getRedirectResponse()
*/
public function getScriptUrl()
{
$request = $this->getRequest();

if ($request instanceof PurchaseRequest && $request->getTestMode()) {
return $this->testScript;
}

return $this->liveScript;
return $this->script;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Omnipay\Afterpay\Message;

class RefundRequest extends AbstractRequest
{
/**
* Get the raw data array for this message. The format of this varies from gateway to
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
*
* @return mixed
*/
public function getData()
{
return array (
'amount' => array(
'amount' => $this->getAmount(),
'currency' => $this->getCurrency()
)
);
}

/**
* @return string
*/
public function getEndpoint()
{
return parent::getEndpoint() . '/payments/' . $this->getTransactionReference() . '/refund';
}
}
10 changes: 8 additions & 2 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,24 @@ public function configurationRequest()
public function testPurchase()
{
$request = $this->gateway->purchase(array('amount' => '10.00'));

$this->assertInstanceOf('Omnipay\AfterPay\Message\PurchaseRequest', $request);
$this->assertSame('10.00', $request->getAmount());
}

public function testPurchaseReturn()
{
$request = $this->gateway->completePurchase(array('amount' => '10.00'));

$this->assertInstanceOf('Omnipay\AfterPay\Message\CompletePurchaseRequest', $request);
$this->assertSame('10.00', $request->getAmount());
}

public function testRefund()
{
$options = array('amount' => '10.00', 'currency' => 'NZD');
$request = $this->gateway->refund($options);

$this->assertInstanceOf('Omnipay\Afterpay\Message\RefundRequest', $request);
$this->assertSame('10.00', $request->getAmount());
$this->assertSame('NZD', $request->getCurrency());
}
}

0 comments on commit d064f5a

Please sign in to comment.