Nochex.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ nGMTbByj5NeUjCMlKwl6U9CIVZbZ66G6LFxzLpMVJjg=
  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. * nochex gateway model class
  14. * accepts only payments in GBP
  15. */
  16. namespace Ppb\Model\PaymentGateway;
  17. use Cube\Controller\Request\AbstractRequest,
  18. Ppb\Service\Table\Currencies as CurrenciesService;
  19. class Nochex extends AbstractPaymentGateway
  20. {
  21. /**
  22. * payment gateway name
  23. */
  24. const NAME = 'Nochex';
  25. /**
  26. * required settings
  27. */
  28. const MERCHANT_ID = 'merchant_id';
  29. /**
  30. * accepted currency
  31. */
  32. const ACCEPTED_CURRENCY = 'GBP';
  33. /**
  34. * form post url
  35. */
  36. const POST_URL = 'https://secure.nochex.com/';
  37. /**
  38. * nochex description
  39. */
  40. protected $_description = 'Click to pay through Nochex.';
  41. public function __construct($userId = null)
  42. {
  43. parent::__construct(self::NAME, $userId);
  44. }
  45. /**
  46. *
  47. * check if the gateway is enabled
  48. *
  49. * @return bool
  50. */
  51. public function enabled()
  52. {
  53. if (!empty($this->_data[self::MERCHANT_ID])) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. /**
  59. *
  60. * get setup form elements
  61. *
  62. * @return array
  63. */
  64. public function getElements()
  65. {
  66. $translate = $this->getTranslate();
  67. return array(
  68. array(
  69. 'form_id' => 'Nochex',
  70. 'id' => self::MERCHANT_ID,
  71. 'element' => 'text',
  72. 'label' => $this->_('Nochex Email Address'),
  73. 'description' => $translate->_('Enter your registered email address (merchant id) <br>'
  74. . 'Nochex IPN URL: <br>') . $this->getIpnUrl(),
  75. 'attributes' => array(
  76. 'class' => 'form-control input-medium',
  77. ),
  78. ),
  79. );
  80. }
  81. /**
  82. *
  83. * set transaction amount
  84. * convert all amounts to a standard format (eg: 12000.00)
  85. *
  86. * @param string $amount
  87. *
  88. * @throws \RuntimeException
  89. * @return $this
  90. */
  91. public function setAmount($amount)
  92. {
  93. $currency = $this->getCurrency();
  94. if (empty($currency)) {
  95. $translate = $this->getTranslate();
  96. throw new \RuntimeException($translate->_("Please set the currency before setting the amount."));
  97. }
  98. if ($currency != self::ACCEPTED_CURRENCY) {
  99. $currenciesService = new CurrenciesService();
  100. $amount = $currenciesService->convertAmount($amount, $currency, self::ACCEPTED_CURRENCY);
  101. $this->setCurrency(self::ACCEPTED_CURRENCY);
  102. }
  103. parent::setAmount($amount);
  104. return $this;
  105. }
  106. /**
  107. * @return array
  108. */
  109. public function formElements()
  110. {
  111. return array(
  112. array(
  113. 'id' => self::MERCHANT_ID,
  114. 'value' => $this->_data[self::MERCHANT_ID],
  115. 'element' => 'hidden',
  116. ),
  117. array(
  118. 'id' => 'order_id',
  119. 'value' => $this->getTransactionId(),
  120. 'element' => 'hidden',
  121. ),
  122. array(
  123. 'id' => 'amount',
  124. 'value' => $this->getAmount(),
  125. 'element' => 'hidden',
  126. ),
  127. array(
  128. 'id' => 'description',
  129. 'value' => $this->getName(),
  130. 'element' => 'hidden',
  131. ),
  132. array(
  133. 'id' => 'callback_url',
  134. 'value' => $this->getIpnUrl(),
  135. 'element' => 'hidden',
  136. ),
  137. array(
  138. 'id' => 'success_url',
  139. 'value' => $this->getSuccessUrl(),
  140. 'element' => 'hidden',
  141. ),
  142. array(
  143. 'id' => 'cancel_url',
  144. 'value' => $this->getFailureUrl(),
  145. 'element' => 'hidden',
  146. ),
  147. );
  148. }
  149. public function getPostUrl()
  150. {
  151. return self::POST_URL;
  152. }
  153. /**
  154. *
  155. * process ipn
  156. *
  157. * @param \Cube\Controller\Request\AbstractRequest $request
  158. *
  159. * @return bool return true if ipn returns a valid transaction
  160. */
  161. public function processIpn(AbstractRequest $request)
  162. {
  163. $errno = null;
  164. $errstr = null;
  165. $response = false;
  166. if ($request->isPost()) {
  167. $params = array();
  168. foreach ($request->getParams() as $key => $value) {
  169. $params[] = $key . '=' . urlencode($value);
  170. }
  171. $content = implode('&', $params);
  172. $header = "POST /nochex.dll/apc/apc HTTP/1.0\r\n" .
  173. "Content-Type: application/x-www-form-urlencoded\r\n" .
  174. "Content-Length: " . strlen($content) . "\r\n\r\n";
  175. $fp = fsockopen("www.nochex.com", 80, $errno, $errstr, 10);
  176. fputs($fp, $header . $content);
  177. $paymentStatus = $_POST['payment_status'];
  178. $this->setTransactionId($_POST['custom'])
  179. ->setAmount($_POST['amount'])
  180. ->setCurrency('GBP')
  181. ->setGatewayPaymentStatus($paymentStatus)
  182. ->setGatewayTransactionCode($_POST['transaction_id']);
  183. while (!feof($fp)) {
  184. $result = trim(fgets($fp, 1024));
  185. if (strstr($result, 'AUTHORISED') && trim($paymentStatus) == 'live') {
  186. $response = true;
  187. }
  188. else {
  189. $this->setGatewayPaymentStatus($result);
  190. }
  191. }
  192. fclose($fp);
  193. }
  194. return $response;
  195. }
  196. }