Skrill.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ YbobH0jNXbel9Exw0UKMBfHG3ZMWJ33CVgg3wZh7rmQ=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.2
  11. */
  12. /**
  13. * skrill gateway model class
  14. *
  15. */
  16. namespace Ppb\Model\PaymentGateway;
  17. use Cube\Controller\Request\AbstractRequest;
  18. class Skrill extends AbstractPaymentGateway
  19. {
  20. /**
  21. * payment gateway name
  22. */
  23. const NAME = 'Skrill';
  24. /**
  25. * required settings
  26. */
  27. const PAY_TO_EMAIL = 'pay_to_email';
  28. const SECRET_WORD = 'secret_word';
  29. const TRANSACTION_FIELD = 'TransactionID';
  30. /**
  31. * form post url
  32. */
  33. const POST_URL = 'https://www.moneybookers.com/app/payment.pl';
  34. /**
  35. * form post url (sandbox)
  36. */
  37. const SANDBOX_POST_URL = 'http://www.moneybookers.com/app/test_payment.pl';
  38. /**
  39. * skrill description
  40. */
  41. protected $_description = 'Click to pay through Skrill.';
  42. protected $_ipnCodes = array(
  43. 2 => 'Processed',
  44. 0 => 'Pending',
  45. -1 => 'Cancelled',
  46. -2 => 'Failed',
  47. -3 => 'Chargeback'
  48. );
  49. public function __construct($userId = null)
  50. {
  51. parent::__construct(self::NAME, $userId);
  52. }
  53. /**
  54. *
  55. * check if the gateway is enabled
  56. *
  57. * @return bool
  58. */
  59. public function enabled()
  60. {
  61. if (!empty($this->_data[self::PAY_TO_EMAIL])) {
  62. return true;
  63. }
  64. return false;
  65. }
  66. /**
  67. *
  68. * get setup form elements
  69. *
  70. * @return array
  71. */
  72. public function getElements()
  73. {
  74. $translate = $this->getTranslate();
  75. return array(
  76. array(
  77. 'form_id' => 'Skrill',
  78. 'id' => self::PAY_TO_EMAIL,
  79. 'element' => 'text',
  80. 'label' => $this->_('Skrill Email Address'),
  81. 'description' => $this->_('Enter your registered email address'),
  82. 'attributes' => array(
  83. 'class' => 'form-control input-medium',
  84. ),
  85. ),
  86. array(
  87. 'form_id' => 'Skrill',
  88. 'id' => self::SECRET_WORD,
  89. 'element' => 'text',
  90. 'label' => $this->_('Skrill Secret Word'),
  91. 'description' => $translate->_('(recommended) The secret word submitted in the "Merchant Tools" section of the Merchant\'s online Skrill account. <br>'
  92. . 'Skrill IPN URL: <br>') . $this->getIpnUrl(),
  93. 'attributes' => array(
  94. 'class' => 'form-control input-medium',
  95. ),
  96. ),
  97. );
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function formElements()
  103. {
  104. return array(
  105. array(
  106. 'id' => self::PAY_TO_EMAIL,
  107. 'value' => $this->_data[self::PAY_TO_EMAIL],
  108. 'element' => 'hidden',
  109. ),
  110. array(
  111. 'id' => 'language',
  112. 'value' => 'EN',
  113. 'element' => 'hidden',
  114. ),
  115. array(
  116. 'id' => 'amount',
  117. 'value' => $this->getAmount(),
  118. 'element' => 'hidden',
  119. ),
  120. array(
  121. 'id' => 'currency',
  122. 'value' => $this->getCurrency(),
  123. 'element' => 'hidden',
  124. ),
  125. array(
  126. 'id' => 'merchant_fields',
  127. 'value' => self::TRANSACTION_FIELD,
  128. 'element' => 'hidden',
  129. ),
  130. array(
  131. 'id' => self::TRANSACTION_FIELD,
  132. 'value' => $this->getTransactionId(),
  133. 'element' => 'hidden',
  134. ),
  135. array(
  136. 'id' => 'detail1_description',
  137. 'value' => $this->getName(),
  138. 'element' => 'hidden',
  139. ),
  140. array(
  141. 'id' => 'status_url',
  142. 'value' => $this->getIpnUrl(),
  143. 'element' => 'hidden',
  144. ),
  145. array(
  146. 'id' => 'return_url',
  147. 'value' => $this->getSuccessUrl(),
  148. 'element' => 'hidden',
  149. ),
  150. array(
  151. 'id' => 'cancel_url',
  152. 'value' => $this->getFailureUrl(),
  153. 'element' => 'hidden',
  154. ),
  155. );
  156. }
  157. public function getPostUrl()
  158. {
  159. return self::POST_URL;
  160. }
  161. /**
  162. *
  163. * process ipn
  164. *
  165. * @param \Cube\Controller\Request\AbstractRequest $request
  166. *
  167. * @return bool return true if ipn returns a valid transaction
  168. */
  169. public function processIpn(AbstractRequest $request)
  170. {
  171. $errno = null;
  172. $errstr = null;
  173. $response = false;
  174. if ($request->isPost()) {
  175. $paymentStatus = $request->getParam('status');
  176. $this->setTransactionId($request->getParam(self::TRANSACTION_FIELD))
  177. ->setAmount($request->getParam('amount'))
  178. ->setCurrency($request->getParam('currency'))
  179. ->setGatewayPaymentStatus($this->_ipnCodes[$paymentStatus])
  180. ->setGatewayTransactionCode($request->getParam('mb_transaction_id'));
  181. if (!$this->_validateMd5Sig($request)) {
  182. $this->setGatewayPaymentStatus('Invalid MD5 Signature');
  183. }
  184. else if ($paymentStatus == 2) {
  185. $response = true;
  186. }
  187. }
  188. return $response;
  189. }
  190. /**
  191. *
  192. * validate md5 signature field
  193. *
  194. * @param AbstractRequest $request
  195. *
  196. * @return bool
  197. */
  198. private function _validateMd5Sig(AbstractRequest $request)
  199. {
  200. if (empty($this->_data[self::SECRET_WORD])) {
  201. return true;
  202. }
  203. $string = $request->getParam('merchant_id')
  204. . $request->getParam('transaction_id')
  205. . strtoupper(md5($this->_data[self::SECRET_WORD]))
  206. . $request->getParam('mb_amount')
  207. . $request->getParam('mb_currency')
  208. . $request->getParam('status');
  209. if (strcmp(strtoupper(md5($string)), $request->getParam('md5sig')) === 0) {
  210. return true;
  211. }
  212. return false;
  213. }
  214. }