PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/system/sparks/payments/0.0.5/libraries/payments/bluepay.php

https://gitlab.com/Riky_Lesmana/SugarSweetCakes
PHP | 310 lines | 130 code | 47 blank | 133 comment | 7 complexity | 8cac50598159634e7579bb6d7a29c69f MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * BluePay Payment Module
  4. *
  5. * @package CodeIgniter
  6. * @subpackage Sparks
  7. * @category Payments
  8. * @author Joel Kallman (www.eclarian.com)
  9. * @email jkallman@eclarian.com
  10. * @created 08/24/2011
  11. * @license http://www.opensource.org/licenses/mit-license.php
  12. * @link https://github.com/calvinfroedge/codeigniter-payments
  13. */
  14. class Bluepay {
  15. /**
  16. * The API method currently being utilized
  17. */
  18. private $_api_endpoint;
  19. /**
  20. * The API method currently being utilized
  21. */
  22. private $_api_method;
  23. /**
  24. * An array for storing all settings
  25. */
  26. private $_api_settings;
  27. /**
  28. * The version of the API to use
  29. */
  30. private $_api_version;
  31. /**
  32. * The final string to be sent in the http query
  33. */
  34. private $_http_query;
  35. /**
  36. * An array for storing all request data
  37. */
  38. private $_request = array();
  39. /**
  40. * Maps CI Payments key names to Bluepay's API key name
  41. */
  42. private $_payment_to_gateway_key_map;
  43. // -------------------------------------------------------------------------
  44. /**
  45. * Constructor method
  46. */
  47. public function __construct($payments)
  48. {
  49. $this->payments = $payments;
  50. $this->_api_endpoint = $this->payments->ci->config->item('api_endpoint');
  51. $this->_api_version = $this->payments->ci->config->item('api_version');
  52. $this->_payment_to_gateway_key_map = $this->payments->ci->config->item('payment_to_gateway_key_map');
  53. $this->_api_settings = (object) array(
  54. 'login' => (isset($payments->gateway_credentials)) ? $payments->gateway_credentials['api_account_id'] : $this->payments->ci->config->item('api_account_id'),
  55. 'user_id' => (isset($payments->gateway_credentials)) ? $payments->gateway_credentials['api_user_id'] : $this->payments->ci->config->item('api_user_id'),
  56. 'secret_key' => (isset($payments->gateway_credentials)) ? $payments->gateway_credentials['api_secret_key'] : $this->payments->ci->config->item('api_secret_key'),
  57. 'email_customer'=> $this->payments->ci->config->item('email_customer'),
  58. 'test_mode' => $this->payments->ci->config->item('test_mode')
  59. );
  60. }
  61. // -------------------------------------------------------------------------
  62. /**
  63. * Authorize a oneoff payment
  64. * @param array An array of payment params, sent from your controller / library
  65. * @return object The response from the payment gateway
  66. */
  67. public function bluepay_authorize_payment($params)
  68. {
  69. $this->_api_method = 'AUTH';
  70. $this->_request = $this->_build_request($params);
  71. return $this->_handle_query();
  72. }
  73. // -------------------------------------------------------------------------
  74. /**
  75. * Capture a oneoff payment
  76. * @param array An array of payment params, sent from your controller / library
  77. * @return object The response from the payment gateway
  78. */
  79. public function bluepay_capture_payment($params)
  80. {
  81. $this->_api_method = 'CAPTURE';
  82. $this->_request = $this->_build_request($params);
  83. return $this->_handle_query();
  84. }
  85. // -------------------------------------------------------------------------
  86. /**
  87. * Make a oneoff payment
  88. * @param array An array of payment params, sent from your controller / library
  89. * @return object The response from the payment gateway
  90. */
  91. public function bluepay_oneoff_payment($params)
  92. {
  93. $this->_api_method = 'SALE';
  94. $this->_request = $this->_build_request($params);
  95. return $this->_handle_query();
  96. }
  97. // -------------------------------------------------------------------------
  98. /**
  99. * Refund a transaction
  100. * @param array An array that contains your identifier
  101. * @return object The response from the payment gateway
  102. */
  103. public function bluepay_refund_payment($params)
  104. {
  105. $this->_api_method = 'REFUND';
  106. $this->_request = $this->_build_request($params);
  107. return $this->_handle_query();
  108. }
  109. // -------------------------------------------------------------------------
  110. /**
  111. * Void a oneoff payment
  112. * @param array An array of params, sent from your controller / library
  113. * @return object The response from the payment gateway
  114. * NOTE: This transaction type can be used to cancel either an original transaction that is not yet settled, or an entire order composed of more than one transaction. A void prevents the transaction or order from being sent for settlement. A Void can be submitted against any other transaction type.
  115. * NOTE: This will ONLY work for unsettled transactions.
  116. */
  117. public function bluepay_void_payment($params)
  118. {
  119. $this->_api_method = 'VOID';
  120. $this->_request = $this->_build_request($params);
  121. return $this->_handle_query();
  122. }
  123. // -------------------------------------------------------------------------
  124. /**
  125. * Add Config to Request
  126. *
  127. * @param array
  128. * @return array
  129. */
  130. protected function _add_config_to_request($params)
  131. {
  132. $params['MODE'] = ($this->_api_settings->test_mode) ? 'TEST': 'LIVE';
  133. $params['ACCOUNT_ID'] = $this->_api_settings->login;
  134. $params['TRANS_TYPE'] = $this->_api_method;
  135. $params['PAYMENT_TYPE'] = 'CREDIT';
  136. if( ! empty($this->_api_settings->user_id) )
  137. {
  138. $params['USER_ID'] = $this->_api_settings->user_id;
  139. }
  140. $params['TAMPER_PROOF_SEAL'] = $this->_build_tamper_proof_seal($params);
  141. return $params;
  142. }
  143. // -------------------------------------------------------------------------
  144. /**
  145. * Builds a request
  146. *
  147. * Builds as an HTTP POST Request
  148. *
  149. * @param array array of params
  150. * @param string the api call to use
  151. * @return array Array of transaction settings
  152. */
  153. protected function _build_request($params)
  154. {
  155. $request = array();
  156. // Map CI Payments Keys to Gateway Keys
  157. foreach($this->_payment_to_gateway_key_map as $map => $val)
  158. {
  159. // Key not being used or Parameter not included or empty
  160. if($val === FALSE OR ! isset($params[$map]) OR empty($params[$map]) ) continue;
  161. $request[$val] = $params[$map];
  162. }
  163. // Setup Configured Values for Request
  164. $request = $this->_add_config_to_request($request);
  165. // Build HTTP Query Because we are using POST rather than XML
  166. return http_build_query($request);
  167. }
  168. // -------------------------------------------------------------------------
  169. /**
  170. * Build Tamper Proof Seal
  171. *
  172. * This function creates a md5 checksum to validate the integrity of the request
  173. * The secret key is never passed directly and is used as a salt to provide a check
  174. * on the gateway servers.
  175. *
  176. * FORMAT:
  177. * md5(SECRET KEY + ACCOUNT_ID + TRANS_TYPE + AMOUNT + MASTER_ID + NAME1 + PAYMENT_ACCOUNT)
  178. *
  179. * @param array Current Requests Parameters
  180. * @return string Checksum for Tamper Proof Seal
  181. */
  182. protected final function _build_tamper_proof_seal($params)
  183. {
  184. $hash = '';
  185. $params['SECRET_KEY'] = $this->_api_settings->secret_key;
  186. $tps_contents = array('SECRET_KEY', 'ACCOUNT_ID', 'TRANS_TYPE', 'AMOUNT', 'MASTER_ID', 'NAME1', 'PAYMENT_ACCOUNT');
  187. foreach($tps_contents as $key) $hash .= (isset($params[$key])) ? $params[$key]: '';
  188. return bin2hex( md5($hash, TRUE) );
  189. }
  190. // -------------------------------------------------------------------------
  191. /**
  192. * Build the query for the response and call the request function
  193. *
  194. * @param array
  195. * @param array
  196. * @param string
  197. * @return array
  198. */
  199. protected function _handle_query()
  200. {
  201. $this->_http_query = $this->_request;
  202. $response_object = $this->payments->gateway_request($this->_api_endpoint, $this->_http_query, 'application/x-www-form-urlencoded');
  203. return $this->_parse_response($response_object);
  204. }
  205. // -------------------------------------------------------------------------
  206. /**
  207. * Parse the response from the server
  208. *
  209. * @param object Always includes timestamp, gateway_response, reason
  210. * @return object
  211. */
  212. protected function _parse_response($data)
  213. {
  214. // Since this module currently uses POST to make the gateway request
  215. // We know our current object can be simply typecasted back to an array.
  216. // IF THIS EVER CHANGES, USE $this->payments->arrayize_object($data);
  217. $results = explode('&',urldecode($data));
  218. foreach($results as $result)
  219. {
  220. list($key, $value) = explode('=', $result);
  221. $gateway_response[$key]=$value;
  222. }
  223. $details = (object) array();
  224. $details->timestamp = gmdate('c');
  225. $details->gateway_response = $gateway_response; // Full Gateway Response
  226. //Set response types
  227. $response_types = array(
  228. 'E' => $this->payments->payment_type.'_gateway_failure',
  229. '1' => $this->payments->payment_type.'_success',
  230. '0' => $this->payments->payment_type.'_local_failure'
  231. );
  232. // Default to Failure if data is not what is expected
  233. $status = 'failure';
  234. // Setup Final Response
  235. if(isset($gateway_response['MESSAGE']))
  236. {
  237. $details->reason = $gateway_response['MESSAGE'];
  238. }
  239. if(isset($gateway_response['STATUS']))
  240. {
  241. $details->status = $gateway_response['STATUS']; // The request can be successful, yet have the card be declined
  242. }
  243. // Setup additional properties if successful
  244. if(isset($gateway_response['TRANS_ID']))
  245. {
  246. $details->identifier = $gateway_response['TRANS_ID'];
  247. }
  248. // Return Local Response, because we didn't get an expected response from server
  249. if( ! isset($gateway_response['STATUS'], $gateway_response['MESSAGE']))
  250. {
  251. // @todo - Don't know if this should be a different response than "gateway"
  252. return $this->payments->return_response($status, $response_types['E'], 'gateway_response', $details);
  253. }
  254. // Possible Responses are 1 = Approved, 0 = Decline, 'E' = Error
  255. $is_success = ($data['STATUS'] === '1');
  256. // Setup Response
  257. $status = ($is_success) ? 'success': 'failure';
  258. $response = $response_types[$gateway_response['STATUS']];
  259. // Send it back!
  260. return $this->payments->return_response($status, $response, 'gateway_response', $details);
  261. }
  262. // -------------------------------------------------------------------------
  263. }