Overview

Classes

  • Syspay_Merchant_AstroPayBanksRequest
  • Syspay_Merchant_BillingAgreementCancellationRequest
  • Syspay_Merchant_BillingAgreementInfoRequest
  • Syspay_Merchant_BillingAgreementListRequest
  • Syspay_Merchant_BillingAgreementRequest
  • Syspay_Merchant_ChargebackInfoRequest
  • Syspay_Merchant_ChargebackListRequest
  • Syspay_Merchant_Client
  • Syspay_Merchant_ConfirmRequest
  • Syspay_Merchant_EMS
  • Syspay_Merchant_Entity
  • Syspay_Merchant_Entity_AstroPayBank
  • Syspay_Merchant_Entity_BillingAgreement
  • Syspay_Merchant_Entity_Chargeback
  • Syspay_Merchant_Entity_Creditcard
  • Syspay_Merchant_Entity_Customer
  • Syspay_Merchant_Entity_Eterminal
  • Syspay_Merchant_Entity_Payment
  • Syspay_Merchant_Entity_PaymentMethod
  • Syspay_Merchant_Entity_PaymentRecipient
  • Syspay_Merchant_Entity_Plan
  • Syspay_Merchant_Entity_Refund
  • Syspay_Merchant_Entity_Subscription
  • Syspay_Merchant_Entity_SubscriptionEvent
  • Syspay_Merchant_EterminalRequest
  • Syspay_Merchant_IpAddressesRequest
  • Syspay_Merchant_PaymentInfoRequest
  • Syspay_Merchant_PaymentListRequest
  • Syspay_Merchant_PaymentRequest
  • Syspay_Merchant_PlanInfoRequest
  • Syspay_Merchant_PlanRequest
  • Syspay_Merchant_PlanUpdateRequest
  • Syspay_Merchant_RebillRequest
  • Syspay_Merchant_Redirect
  • Syspay_Merchant_RefundInfoRequest
  • Syspay_Merchant_RefundListRequest
  • Syspay_Merchant_RefundRequest
  • Syspay_Merchant_Request
  • Syspay_Merchant_SubscriptionCancellationRequest
  • Syspay_Merchant_SubscriptionInfoRequest
  • Syspay_Merchant_SubscriptionRebillRequest
  • Syspay_Merchant_SubscriptionRequest
  • Syspay_Merchant_Utils
  • Syspay_Merchant_VoidRequest

Interfaces

  • Syspay_Merchant_Entity_ReturnedEntityInterface

Exceptions

  • Syspay_Merchant_EMSException
  • Syspay_Merchant_RedirectException
  • Syspay_Merchant_RequestException
  • Syspay_Merchant_UnexpectedResponseException
  • Overview
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Base API client
  5:  *
  6:  * @see https://app.syspay.com/bundles/emiuser/doc/merchant_api.html#emerchant-rest-api
  7:  */
  8: class Syspay_Merchant_Client
  9: {
 10:     const BASE_URL_PROD    = 'https://app.syspay.com';
 11:     const BASE_URL_SANDBOX = 'https://app-sandbox.syspay.com';
 12: 
 13:     protected $username;
 14:     protected $secret;
 15:     protected $baseUrl;
 16: 
 17:     protected $responseBody    = null;
 18:     protected $responseHeaders = array();
 19:     protected $responseData    = null;
 20: 
 21:     protected $requestBody     = null;
 22:     protected $requestHeaders  = array();
 23:     protected $requestParams   = null;
 24: 
 25:     protected $requestId       = null;
 26: 
 27:     /**
 28:      * Creates a new client object
 29:      * @param string $username The Syspay API username
 30:      * @param string $secret   The Syspay API shared secret
 31:      * @param string $baseUrl  The base URL the request should be made to (optional, defaults to prod environment)
 32:      */
 33:     public function __construct($username, $secret, $baseUrl = null)
 34:     {
 35:         $this->username = $username;
 36:         $this->secret   = $secret;
 37:         $this->baseUrl  = (null === $baseUrl)?self::BASE_URL_PROD:$baseUrl;
 38:     }
 39: 
 40:     /**
 41:      * Generates the x-wsse header
 42:      *
 43:      * @param string   $username The Syspay API username
 44:      * @param string   $secret   The Syspay API shared secret
 45:      * @param string   $nonce    A random string (optional, will be generated)
 46:      * @param DateTime $created  The creation date of this header (optional, defaults to now)
 47:      *
 48:      * @return string   The value to give to the x-wsse header
 49:      */
 50:     protected function generateAuthHeader($username, $secret, $nonce = null, DateTime $created = null)
 51:     {
 52:         if (null === $nonce) {
 53:             $nonce = md5(rand(), true);
 54:         }
 55: 
 56:         if (null === $created) {
 57:             $created = new DateTime();
 58:         }
 59: 
 60:         $created = $created->format('U');
 61: 
 62:         $digest = base64_encode(sha1($nonce . $created . $secret, true));
 63:         $b64nonce = base64_encode($nonce);
 64: 
 65:         return sprintf(
 66:             'AuthToken MerchantAPILogin="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
 67:             $username,
 68:             $digest,
 69:             $b64nonce,
 70:             $created
 71:         );
 72:     }
 73: 
 74:     /**
 75:      * Make a request to the Syspay API
 76:      * @param Syspay_Merchant_Request $request The request to send to the API
 77:      *
 78:      * @return mixed The response to the request
 79:      * @throws Syspay_Merchant_RequestException If the request could not be processed by the API
 80:      */
 81:     public function request(Syspay_Merchant_Request $request)
 82:     {
 83:         $this->requestBody = $this->responseBody = $this->responseData = $this->requestId = null;
 84:         $this->responseHeaders = $this->requestHeaders = array();
 85: 
 86:         $headers = array(
 87:             'Accept: application/json',
 88:             'X-Wsse: ' . $this->generateAuthHeader($this->username, $this->secret)
 89:         );
 90: 
 91: 
 92:         $url = rtrim($this->baseUrl, '/') . '/' . ltrim($request->getPath(), '/');
 93: 
 94:         $ch = curl_init();
 95:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 96:         curl_setopt($ch, CURLOPT_HEADER, true);
 97:         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // TODO: verify ssl and provide certificate in package
 98:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 99: 
100:         $method = strtoupper($request->getMethod());
101: 
102:         // Per-method special handling
103:         switch($method) {
104:             case 'PUT':
105:             case 'POST':
106:                 $body = json_encode($request->getData());
107: 
108:                 array_push($headers, 'Content-Type: application/json');
109:                 array_push($headers, 'Content-Length: ' . strlen($body));
110: 
111:                 curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
112:                 $this->requestBody = $body;
113:                 break;
114: 
115:             case 'GET':
116:                 $queryParams = $request->getData();
117:                 if (is_array($queryParams)) {
118:                     $url .= '?' . http_build_query($queryParams);
119:                 }
120:                 $this->requestParams = $queryParams;
121:                 break;
122: 
123:             case 'DELETE':
124:                 break;
125: 
126:             default:
127:                 throw new Exception('Unsupported method given: ' . $method);
128:         }
129: 
130:         curl_setopt($ch, CURLOPT_URL, $url);
131:         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
132:         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
133:         $this->requestHeaders = $headers;
134: 
135:         $response = curl_exec($ch);
136:         if ($response === false) {
137:             throw new Exception(curl_error($ch), curl_errno($ch));
138:         }
139:         $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
140:         list($headers, $body) = explode("\r\n\r\n", $response, 2);
141:         $this->responseHeaders = explode("\r\n", $headers);
142:         $this->responseBody    = $body;
143:         if (preg_match('/\nx-syspay-request-uuid: (.*?)\r?\n/i', $headers, $m)) {
144:             $this->requestId = $m[1];
145:         }
146: 
147:         if (!in_array($httpCode, array(200, 201))) {
148:             throw new Syspay_Merchant_RequestException($httpCode, $headers, $body);
149:         }
150: 
151:         $decoded = json_decode($body);
152: 
153:         if (($decoded instanceof stdClass) && isset($decoded->data) && ($decoded->data instanceof stdClass)) {
154:             $this->responseData = $decoded->data;
155: 
156:             return $request->buildResponse($decoded->data);
157:         } else {
158:             throw new Syspay_Merchant_UnexpectedResponseException('Unable to decode response from json', $body);
159:         }
160: 
161:         return false;
162:     }
163: 
164:     /**
165:      * Get the raw body of the last request.
166:      * @return string The last request's response body, or null if the request failed.
167:      */
168:     public function getResponseBody()
169:     {
170:         return $this->responseBody;
171:     }
172: 
173:     /**
174:      * Get the raw headers of the last request.
175:      * @return array The last request's headers, or an empty array if the request failed
176:      */
177:     public function getResponseHeaders()
178:     {
179:         return $this->responseHeaders;
180:     }
181: 
182:     /**
183:      * Get the username
184:      * @return string Merchant username
185:      */
186:     public function getUsername()
187:     {
188:         return $this->username;
189:     }
190: 
191:     /**
192:      * Get the shared secret
193:      * @return string secret
194:      */
195:     public function getSecret()
196:     {
197:         return $this->secret;
198:     }
199: 
200:     /**
201:      * Get the base URL
202:      * @return string Base URL
203:      */
204:     public function getBaseUrl()
205:     {
206:         return $this->baseUrl;
207:     }
208: 
209:     /**
210:      * Get the (decoded) response data, if available
211:      * @return mixed Response data
212:      */
213:     public function getResponseData()
214:     {
215:         return $this->responseData;
216:     }
217: 
218:     /**
219:      * Get the request headers, if available
220:      * @return array Request headers
221:      */
222:     public function getRequestHeaders()
223:     {
224:         return $this->requestHeaders;
225:     }
226: 
227:     /**
228:      * Get the request body, if available (POST/PUT requests)
229:      * @return array Request body
230:      */
231:     public function getRequestBody()
232:     {
233:         return $this->requestBody;
234:     }
235: 
236:     /**
237:      * Get the request params, if available (GET request)
238:      * @return array Request body
239:      */
240:     public function getRequestParams()
241:     {
242:         return $this->requestParams;
243:     }
244: 
245:     /**
246:      * Get the last request's id
247:      * @return string Request Id or null if it couldn't be extracted
248:      */
249:     public function getRequestId()
250:     {
251:         return $this->requestId;
252:     }
253: }
254: 
Syspay Merchant SDK API documentation generated by ApiGen 2.8.0