AuthorizeNet.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ qWJyUgLGiW9PBPnpNR5oPvdvIclZlfujxSRIR/AFhGU=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.4
  11. */
  12. /**
  13. * authorize.net payment gateway model class
  14. */
  15. namespace Ppb\Model\PaymentGateway;
  16. use Cube\Controller\Request\AbstractRequest;
  17. class AuthorizeNet extends AbstractPaymentGateway
  18. {
  19. /**
  20. * payment gateway name
  21. */
  22. const NAME = 'AuthorizeNet';
  23. /**
  24. * required settings
  25. */
  26. const MERCHANT_ID = 'x_login';
  27. const TRANSACTION_KEY = 'authnet_transaction_key';
  28. const MD5_HASH = 'x_MD5_Hash';
  29. const SANDBOX_MODE = 'sandbox_mode';
  30. /**
  31. * form post url
  32. */
  33. const POST_URL = 'https://secure.authorize.net/gateway/transact.dll';
  34. /**
  35. * form post url (sandbox)
  36. */
  37. const SANDBOX_POST_URL = 'https://test.authorize.net/gateway/transact.dll';
  38. /**
  39. * 2checkout description
  40. */
  41. protected $_description = 'Click to pay through Authorize.net.';
  42. protected $_ipnCodes = array(
  43. 1 => 'Approved',
  44. 2 => 'Declined',
  45. 3 => 'Error',
  46. 4 => 'Held for Review',
  47. );
  48. public function __construct($userId = null)
  49. {
  50. parent::__construct(self::NAME, $userId);
  51. }
  52. /**
  53. *
  54. * check if the gateway is enabled
  55. *
  56. * @return bool
  57. */
  58. public function enabled()
  59. {
  60. if (!empty($this->_data[self::MERCHANT_ID]) && !empty($this->_data[self::TRANSACTION_KEY])) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. *
  67. * get setup form elements
  68. *
  69. * @return array
  70. */
  71. public function getElements()
  72. {
  73. $translate = $this->getTranslate();
  74. return array(
  75. array(
  76. 'form_id' => 'AuthorizeNet',
  77. 'id' => self::MERCHANT_ID,
  78. 'element' => 'text',
  79. 'label' => $this->_('Authorize.net Merchant ID'),
  80. 'description' => $this->_('Enter your merchant ID'),
  81. 'attributes' => array(
  82. 'class' => 'form-control input-medium',
  83. ),
  84. ),
  85. array(
  86. 'form_id' => 'AuthorizeNet',
  87. 'id' => self::TRANSACTION_KEY,
  88. 'element' => 'text',
  89. 'label' => $this->_('Authorize.net Transaction Key'),
  90. 'description' => $this->_('Enter your assigned transaction key'),
  91. 'attributes' => array(
  92. 'class' => 'form-control input-medium',
  93. ),
  94. ),
  95. array(
  96. 'form_id' => 'AuthorizeNet',
  97. 'id' => self::MD5_HASH,
  98. 'element' => 'text',
  99. 'label' => $this->_('Authorize.net MD5 Hash'),
  100. 'description' => $this->_('(recommended) enter your set md5 hash value if you wish for the ipn requests to be encrypted <br>'
  101. . 'Authorize.net Relay Response URL: <br>') . $this->getIpnUrl(),
  102. 'attributes' => array(
  103. 'class' => 'form-control input-medium',
  104. ),
  105. ),
  106. array(
  107. 'form_id' => 'AuthorizeNet',
  108. 'id' => self::SANDBOX_MODE,
  109. 'element' => 'checkbox',
  110. 'label' => $this->_('Sandbox Mode'),
  111. 'description' => $this->_('Check the above checkbox to activate the sandbox mode.'),
  112. 'multiOptions' => array(
  113. 1 => null,
  114. ),
  115. ),
  116. );
  117. }
  118. public function formElements()
  119. {
  120. $timestamp = time();
  121. return array(
  122. array(
  123. 'id' => 'x_version',
  124. 'value' => '3.1',
  125. 'element' => 'hidden',
  126. ),
  127. array(
  128. 'id' => self::MERCHANT_ID,
  129. 'value' => $this->_data[self::MERCHANT_ID],
  130. 'element' => 'hidden',
  131. ),
  132. array(
  133. 'id' => 'x_type',
  134. 'value' => 'AUTH_CAPTURE',
  135. 'element' => 'hidden',
  136. ),
  137. array(
  138. 'id' => 'x_method',
  139. 'value' => 'CC',
  140. 'element' => 'hidden',
  141. ),
  142. array(
  143. 'id' => 'x_amount',
  144. 'value' => $this->getAmount(),
  145. 'element' => 'hidden',
  146. ),
  147. array(
  148. 'id' => 'x_show_form',
  149. 'value' => 'PAYMENT_FORM',
  150. 'element' => 'hidden',
  151. ),
  152. array(
  153. 'id' => 'x_relay_response',
  154. 'value' => 'TRUE',
  155. 'element' => 'hidden',
  156. ),
  157. array(
  158. 'id' => 'x_test_request',
  159. 'value' => ($this->_isSandboxMode()) ? 'TRUE' : 'false',
  160. 'element' => 'hidden',
  161. ),
  162. array(
  163. 'id' => 'x_description',
  164. 'value' => $this->_shortenString($this->getName(), 255),
  165. 'element' => 'hidden',
  166. ),
  167. array(
  168. 'id' => 'x_invoice_num',
  169. 'value' => $this->getTransactionId(),
  170. 'element' => 'hidden',
  171. ),
  172. array(
  173. 'id' => 'x_currency_code',
  174. 'value' => $this->getCurrency(),
  175. 'element' => 'hidden',
  176. ),
  177. array(
  178. 'id' => 'x_fp_hash',
  179. 'value' => $this->_createHash($timestamp),
  180. 'element' => 'hidden',
  181. ),
  182. array(
  183. 'id' => 'x_fp_sequence',
  184. 'value' => $this->getTransactionId(),
  185. 'element' => 'hidden',
  186. ),
  187. array(
  188. 'id' => 'x_fp_timestamp',
  189. 'value' => $timestamp,
  190. 'element' => 'hidden',
  191. ),
  192. array(
  193. 'id' => 'x_cancel_url',
  194. 'value' => $this->getFailureUrl(),
  195. 'element' => 'hidden',
  196. ),
  197. );
  198. }
  199. /**
  200. *
  201. * get the form post url (live or sandbox)
  202. *
  203. * @return string
  204. */
  205. public function getPostUrl()
  206. {
  207. return ($this->_isSandboxMode()) ?
  208. self::SANDBOX_POST_URL : self::POST_URL;
  209. }
  210. /**
  211. *
  212. * process ipn
  213. *
  214. * @param \Cube\Controller\Request\AbstractRequest $request
  215. *
  216. * @return bool
  217. */
  218. public function processIpn(AbstractRequest $request)
  219. {
  220. $response = false;
  221. if ($request->isPost()) {
  222. $paymentStatus = $request->getParam('x_response_code');
  223. $this->setTransactionId($request->getParam('x_invoice_num'))
  224. ->setAmount($request->getParam('x_amount'))
  225. ->setCurrency($request->getParam('x_currency_code'))
  226. ->setGatewayPaymentStatus($this->_ipnCodes[$paymentStatus])
  227. ->setGatewayTransactionCode($request->getParam('x_trans_id'));
  228. if (!$this->_validateMd5Sig($request)) {
  229. $this->setGatewayPaymentStatus('Invalid MD5 Hash');
  230. }
  231. else if ($paymentStatus == 1) {
  232. $response = true;
  233. }
  234. }
  235. return $response;
  236. }
  237. /**
  238. *
  239. * method that checks if the amount and currency submitted through an ipn is the
  240. * coincides with the row in the transactions table
  241. *
  242. * @param float $amount
  243. * @param string $currency
  244. *
  245. * @return bool
  246. */
  247. public function checkIpnAmount($amount, $currency)
  248. {
  249. if ($this->_amount == $amount && in_array($currency, array('USD', 'CAD', 'GBP'))) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. /**
  255. *
  256. * generates the required x_fp_hash variable, based on merchant id, transaction id (x_fp_sequence), timestamp and payment amount
  257. * and hashed using the merchant's transaction key
  258. *
  259. * @param int $timestamp
  260. *
  261. * @return string
  262. */
  263. private function _createHash($timestamp)
  264. {
  265. return $this->_hmac($this->_data[self::TRANSACTION_KEY],
  266. $this->_data[self::MERCHANT_ID] . '^' . $this->getTransactionId() . '^' . $timestamp . '^' . $this->getAmount() . '^' . $this->getCurrency());
  267. }
  268. /**
  269. *
  270. * RFC 2104 HMAC implementation for php.
  271. * Creates an md5 HMAC.
  272. * Eliminates the need to install mhash to compute a HMAC
  273. * Hacked by Lance Rushing
  274. *
  275. * @param string $key
  276. * @param string $data
  277. *
  278. * @return string
  279. */
  280. private function _hmac($key, $data)
  281. {
  282. $b = 64; // byte length for md5
  283. if (strlen($key) > $b) {
  284. $key = pack("H*", md5($key));
  285. }
  286. $key = str_pad($key, $b, chr(0x00));
  287. $iPad = str_pad('', $b, chr(0x36));
  288. $oPad = str_pad('', $b, chr(0x5c));
  289. $kIPad = $key ^ $iPad;
  290. $kOPad = $key ^ $oPad;
  291. return md5($kOPad . pack("H*", md5($kIPad . $data)));
  292. }
  293. /**
  294. *
  295. * validate ipn md5 hash
  296. *
  297. * @param AbstractRequest $request
  298. *
  299. * @return bool
  300. */
  301. private function _validateMd5Sig(AbstractRequest $request)
  302. {
  303. if (empty($this->_data[self::MD5_HASH])) {
  304. return true;
  305. }
  306. $string = $this->_data[self::MD5_HASH]
  307. . $this->_data[self::MERCHANT_ID]
  308. . $request->getParam('x_trans_id')
  309. . $request->getParam('x_amount');
  310. if (strcasecmp(md5($string), $request->getParam(self::MD5_HASH)) === 0) {
  311. return true;
  312. }
  313. return false;
  314. }
  315. /**
  316. *
  317. * check if sandbox mode is enabled
  318. *
  319. * @return bool
  320. */
  321. protected function _isSandboxMode()
  322. {
  323. $sandbox = (isset($this->_data[self::SANDBOX_MODE])) ? $this->_data[self::SANDBOX_MODE] : false;
  324. return (bool)$sandbox;
  325. }
  326. }