1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267:
<?php
/**
* Viva Payments Abstract REST Request
*/
namespace Omnipay\VivaPayments\Message;
use Omnipay\Common\Message\RequestInterface;
/**
* Viva Payments Abstract REST Request
*
* This is the parent class for all Viva Payments REST requests.
*
* @link http://www.paystream.com.au/developer-guides/
* @see \Omnipay\VivaPayments\RestGateway
*/
abstract class AbstractRestRequest extends \Omnipay\Common\Message\AbstractRequest
{
/**
* Sandbox Endpoint URL
*
* @var string URL
*/
protected $testEndpoint = 'http://demo.vivapayments.com';
/**
* Live Endpoint URL
*
* @var string URL
*/
protected $liveEndpoint = 'https://www.vivapayments.com';
/**
* Get HTTP Method.
*
* This is nearly always POST but can be over-ridden in sub classes.
*
* @return string
*/
protected function getHttpMethod()
{
return 'POST';
}
/**
* Get base endpoint URL
*
* @return string
*/
protected function getBaseEndpoint()
{
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
}
/**
* Get API endpoint URL
*
* @return string
*/
protected function getEndpoint()
{
$base = $this->getBaseEndpoint();
return $base . '/api';
}
/**
* Get the gateway merchantId
*
* @return string
*/
public function getMerchantId()
{
return $this->getParameter('merchantId');
}
/**
* Set the gateway merchantId
*
* Note that all test merchantIds begin with the word TEST in upper case.
*
* @param string $value
* @return AbstractRestRequest provides a fluent interface.
*/
public function setMerchantId($value)
{
return $this->setParameter('merchantId', $value);
}
/**
* Get the gateway apiKey -- used as the password in HTTP Basic Auth
*
* @return string
*/
public function getApiKey()
{
return $this->getParameter('apiKey');
}
/**
* Set the gateway apiKey -- used as the password in HTTP Basic Auth
*
* @param string $value
* @return AbstractRestRequest provides a fluent interface.
*/
public function setApiKey($value)
{
return $this->setParameter('apiKey', $value);
}
/**
* Get the gateway language
*
* The language (in ISO format) that the payment form is displayed. This
* parameter is also included in the target URL the application uses to
* redirect the user after the successful/unsuccessful completion of a payment.
*
* Note: If no parameter is passed, the system assumes el-GR. The payment form
* is currently displayed in the English language for all values other than el-GR.
*
* @return string
*/
public function getRequestLang()
{
return $this->getParameter('requestLang');
}
/**
* Set the gateway language
*
* The language (in ISO format) that the payment form is displayed. This
* parameter is also included in the target URL the application uses to
* redirect the user after the successful/unsuccessful completion of a payment.
*
* Note: If no parameter is passed, the system assumes el-GR. The payment form
* is currently displayed in the English language for all values other than el-GR.
*
* @param string $value
* @return AbstractRestRequest provides a fluent interface.
*/
public function setRequestLang($value)
{
return $this->setParameter('requestLang', $value);
}
/**
* Get the transaction source code.
*
* Sources can be created and managed from the merchant's profile. A source is
* used for grouping and filtering transactions (ie a merchant may have more
* than one websites, or different product categories).
*
* Note: SourceCode is case-sensitive.
*
* @return string
*/
public function getSourceCode()
{
return $this->getParameter('sourceCode');
}
/**
* Set the transaction source code.
*
* Sources can be created and managed from the merchant's profile. A source is
* used for grouping and filtering transactions (ie a merchant may have more
* than one websites, or different product categories).
*
* Note: SourceCode is case-sensitive.
*
* @param string $value
* @return AbstractRestRequest provides a fluent interface.
*/
public function setSourceCode($value)
{
return $this->setParameter('sourceCode', $value);
}
public function getData()
{
// Optional and common parameters
// https://github.com/VivaPayments/API/wiki/Optional-Parameters
$data = array(
'RequestLang' => $this->getRequestLang(),
'MerchantTrns' => $this->getTransactionId(),
'CustomerTrns' => $this->getDescription(),
'SourceCode' => $this->getSourceCode(),
);
return $data;
}
/**
* Creates the correct type of response for this request.
*
* This is a hook to allow different types of request to create their own response
* types, normally used when there is a redirect request of some kind.
*
* @param RequestInterface $request
* @param $data
* @param int $statusCode
* @return RestResponse
*/
protected function createResponse(RequestInterface $request, $data, $statusCode = 200)
{
return new RestResponse($this, $data, $statusCode);
}
public function sendData($data)
{
// don't throw exceptions for 4xx errors
$this->httpClient->getEventDispatcher()->addListener(
'request.error',
function ($event) {
if ($event['response']->isClientError()) {
$event->stopPropagation();
}
}
);
// POST requests require different parameter sending methods to the other requests.
if ($this->getHttpMethod() == 'POST') {
$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint(),
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
),
json_encode($data)
)->setAuth($this->getMerchantId(), $this->getApiKey());
} else {
$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint() . '?' . http_build_query($data),
array(
'Accept' => 'application/json',
)
)->setAuth($this->getMerchantId(), $this->getApiKey());
}
// Might be useful to have some debug code here. Perhaps hook to whatever
// logging engine is being used.
// echo "Data == " . json_encode($data) . "\n";
$httpResponse = $httpRequest->send();
// HTTP Response raw data used for making mocks
/*
echo "HTTP Raw Headers\n\n";
echo $httpResponse->getRawHeaders();
echo "\n\n";
echo "HTTP Raw Body\n\n";
echo $httpResponse->getBody(true);
echo "\n\n";
*/
// Get body parsed from JSON if it's not empty
$rawBody = $httpResponse->getBody(true);
if (empty($rawBody)) {
return $this->response = $this->createResponse($this, array(), $httpResponse->getStatusCode());
}
return $this->response = $this->createResponse($this, $httpResponse->json(), $httpResponse->getStatusCode());
}
}