Skip to content

Commit

Permalink
Merge pull request #3 from onlinesid/omnipay2
Browse files Browse the repository at this point in the history
omnipay/common ~2.0 and omnipay/tests ~2.0
  • Loading branch information
onlinesid committed Feb 21, 2016
2 parents 76d6037 + 1f95e5d commit 46b8fe8
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 61 deletions.
18 changes: 13 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,31 @@
"name": "Jeremy Shipman",
"email": "[email protected]",
"homepage": "http://jeremyshipman.com"
},
{
"name": "Sid Bachtiar",
"email": "[email protected]",
"homepage": "http://onlinesid.com"
}
],
"autoload": {
"psr-4": { "Omnipay\\Poli\\" : "src/" }
"psr-4": {
"Omnipay\\Poli\\" : "src/"
}
},
"require": {
"omnipay/omnipay": "~1.1"
"omnipay/common": "~2.0"
},
"require-dev": {
"guzzle/plugin-mock": "~3.1",
"mockery/mockery": "~0.7",
"phpunit/phpunit": "~3.7.16",
"squizlabs/php_codesniffer": "~1.4"
"phpunit/phpunit": "~3.7.0",
"squizlabs/php_codesniffer": "~1.4",
"omnipay/tests": "~2.0"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "2.0.x-dev"
}
}
}
24 changes: 23 additions & 1 deletion src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

use Omnipay\Common\AbstractGateway;

/**
* Class Gateway
*
* @package Omnipay\Poli
*/
class Gateway extends AbstractGateway
{

/**
* @inheritdoc
*/
public function getName()
{
return 'Poli';
}

/**
* @inheritdoc
*/
public function getDefaultParameters()
{
return array(
Expand All @@ -20,11 +30,18 @@ public function getDefaultParameters()
);
}

/**
* @return string
*/
public function getMerchantCode()
{
return $this->getParameter('merchantCode');
}

/**
* @param string $value
* @return $this
*/
public function setMerchantCode($value)
{
return $this->setParameter('merchantCode', $value);
Expand All @@ -49,4 +66,9 @@ public function completePurchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\Poli\Message\CompletePurchaseRequest', $parameters);
}

public function fetchCheckout(array $parameters = array())
{
return $this->createRequest('\Omnipay\Poli\Message\FetchCheckoutRequest', $parameters);
}
}
14 changes: 10 additions & 4 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Omnipay\Poli\Message;

use Omnipay\Common\Exception\InvalidRequestException;
use SimpleXMLElement;
use Omnipay\Common\Exception\InvalidResponseException;

Expand All @@ -12,8 +13,8 @@
*/
class CompletePurchaseRequest extends PurchaseRequest
{

protected $endpoint = "https://publicapi.apac.paywithpoli.com/api/Transaction/GetTransaction";
//protected $endpoint = "https://publicapi.apac.paywithpoli.com/api/Transaction/GetTransaction";
protected $endpoint = 'https://poliapi.apac.paywithpoli.com/api/v2/Transaction/GetTransaction';

public function getData()
{
Expand All @@ -38,10 +39,15 @@ public function getData()
}

public function send()
{
return $this->sendData($this->getData());
}

public function sendData($data)
{
$request = $this->httpClient->get($this->endpoint)
->setAuth($this->getMerchantCode(), $this->getAuthenticationCode());
$request->getQuery()->replace($this->getData());
->setAuth($this->getMerchantCode(), $this->getAuthenticationCode());
$request->getQuery()->replace($data);
$httpResponse = $request->send();

return $this->response = new CompletePurchaseResponse($this, $httpResponse->getBody());
Expand Down
67 changes: 67 additions & 0 deletions src/Message/FetchCheckoutRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Omnipay\Poli\Message;

use Omnipay\Common\Message\AbstractRequest;

class FetchCheckoutRequest extends AbstractRequest
{
protected $endpoint = 'https://poliapi.apac.paywithpoli.com/api/v2/Transaction/GetTransaction';

public function getMerchantCode()
{
return $this->getParameter('merchantCode');
}

public function setMerchantCode($value)
{
return $this->setParameter('merchantCode', $value);
}

public function getAuthenticationCode()
{
return $this->getParameter('authenticationCode');
}

public function setAuthenticationCode($value)
{
return $this->setParameter('authenticationCode', $value);
}

public function getData()
{
$this->validate(
'merchantCode',
'authenticationCode'
);
$data = array();
return $data;
}

public function send()
{
$data = $this->getData();
return $this->sendData($data);
}

public function sendData($data)
{
$token = $this->httpRequest->query->get('token');
$url = $this->endpoint.'?token='.urlencode($token);

$merchantCode = $this->getMerchantCode();
$authenticationCode = $this->getAuthenticationCode();
$auth = base64_encode($merchantCode.":".$authenticationCode); //'S61xxxxx:AuthCode123');

$httpRequest = $this->httpClient->get(
$url,
array(
'Content-Type'=>'application/json',
'Authorization' => 'Basic '.$auth,
)
);
$httpResponse = $httpRequest->send();
return $this->response = new FetchCheckoutResponse($this, $httpResponse->getBody(true));

}
}
100 changes: 100 additions & 0 deletions src/Message/FetchCheckoutResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Omnipay\Poli\Message;

use Guzzle\Http\EntityBodyInterface;
use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RequestInterface;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Message\RedirectResponseInterface;

/**
* Poli Checkout Response
*
*/
class FetchCheckoutResponse extends AbstractResponse implements RedirectResponseInterface
{
/**
*
* @param RequestInterface $request
* @param string $data
* @throws InvalidResponseException
*/
public function __construct(RequestInterface $request, $data)
{
$this->request = $request;
$this->data = json_decode($data, true);
}

/**
* Is the result a success?
*
* @return bool
*/
public function isSuccessful()
{
return !$this->getCode();
}

/**
* Do we need to redirect?
*
* @return bool
*/
public function isRedirect()
{
return isset($this->data['NavigateURL']);
}

/**
* Error message, e.g.: '' = no error
*
* @return string
*/
public function getMessage()
{
return $this->data['ErrorMessage'];
}

/**
* Error code, e.g.: 0 or '' = no error
*
* @return int
*/
public function getCode()
{
return $this->data['ErrorCode'];
}

/**
* Redirection URL
*
* @return string
*/
public function getRedirectUrl()
{
if ($this->isRedirect()) {
return $this->data['NavigateURL'];
}
}

/**
* Redirection method
*
* @return string
*/
public function getRedirectMethod()
{
return 'GET';
}

/**
* Redirection data
*
* @return null
*/
public function getRedirectData()
{
return null;
}
}
35 changes: 23 additions & 12 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
*/
class PurchaseRequest extends AbstractRequest
{

protected $endpoint = 'https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/transaction/initiate';
protected $endpoint = 'https://poliapi.apac.paywithpoli.com/api/v2/Transaction/Initiate';

public function getMerchantCode()
{
Expand Down Expand Up @@ -47,22 +46,20 @@ public function getData()
);

$data = array();
$data['AuthenticationCode'] = $this->getAuthenticationCode();
$data['CurrencyAmount'] = $this->getAmount();
$data['Amount'] = $this->getAmount();
$data['CurrencyCode'] = $this->getCurrency();
$data['MerchantCheckoutURL'] = $this->getCancelUrl();
$data['MerchantCode'] = $this->getMerchantCode();
$data['CancellationURL'] = $this->getCancelUrl();
$data['MerchantData'] = $this->getTransactionId();
$data['MerchantDateTime'] = date('Y-m-d\TH:i:s');
$data['MerchantHomePageURL'] = $this->getCancelUrl();
$data['MerchantRef'] = $this->getCombinedMerchantRef();
$data['MerchantReference'] = $this->getCombinedMerchantRef();
$data['MerchantReferenceFormat'] = 1;
$data['NotificationURL'] = $this->getNotifyUrl();
$data['SuccessfulURL'] = $this->getReturnUrl();
$data['SuccessURL'] = $this->getReturnUrl();
$data['Timeout'] = 0;
$data['UnsuccessfulURL'] = $this->getReturnUrl();
$data['FailureURL'] = $this->getReturnUrl();
$data['UserIPAddress'] = $this->getClientIp();

return $data;
}

Expand Down Expand Up @@ -92,10 +89,24 @@ protected function cleanField($field)

public function send()
{
$postdata = $this->packageData($this->getData());
return $this->sendData($this->getData());
}

public function sendData($data)
{
$merchantCode = $this->getMerchantCode();
$authenticationCode = $this->getAuthenticationCode();
$auth = base64_encode($merchantCode.":".$authenticationCode); //'S61xxxxx:AuthCode123');
unset($data['MerchantCode'], $data['AuthenticationCode']);

//$postdata = $this->packageData($data);
$postdata = json_encode($data);
$httpRequest = $this->httpClient->post(
$this->endpoint,
array('Content-Type'=>'text/xml'),
array(
'Content-Type'=>'application/json',
'Authorization' => 'Basic '.$auth,
),
$postdata
);
$httpResponse = $httpRequest->send();
Expand Down
Loading

0 comments on commit 46b8fe8

Please sign in to comment.