123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace Ppb\Model\PaymentGateway;
- use Cube\Controller\Request\AbstractRequest,
- Ppb\Service\Table\Currencies as CurrenciesService;
- class Nochex extends AbstractPaymentGateway
- {
-
- const NAME = 'Nochex';
-
- const MERCHANT_ID = 'merchant_id';
-
- const ACCEPTED_CURRENCY = 'GBP';
-
- const POST_URL = 'https://secure.nochex.com/';
-
- protected $_description = 'Click to pay through Nochex.';
- public function __construct($userId = null)
- {
- parent::__construct(self::NAME, $userId);
- }
-
- public function enabled()
- {
- if (!empty($this->_data[self::MERCHANT_ID])) {
- return true;
- }
- return false;
- }
-
- public function getElements()
- {
- $translate = $this->getTranslate();
- return array(
- array(
- 'form_id' => 'Nochex',
- 'id' => self::MERCHANT_ID,
- 'element' => 'text',
- 'label' => $this->_('Nochex Email Address'),
- 'description' => $translate->_('Enter your registered email address (merchant id) <br>'
- . 'Nochex IPN URL: <br>') . $this->getIpnUrl(),
- 'attributes' => array(
- 'class' => 'form-control input-medium',
- ),
- ),
- );
- }
-
- public function setAmount($amount)
- {
- $currency = $this->getCurrency();
- if (empty($currency)) {
- $translate = $this->getTranslate();
- throw new \RuntimeException($translate->_("Please set the currency before setting the amount."));
- }
- if ($currency != self::ACCEPTED_CURRENCY) {
- $currenciesService = new CurrenciesService();
- $amount = $currenciesService->convertAmount($amount, $currency, self::ACCEPTED_CURRENCY);
- $this->setCurrency(self::ACCEPTED_CURRENCY);
- }
- parent::setAmount($amount);
- return $this;
- }
-
- public function formElements()
- {
- return array(
- array(
- 'id' => self::MERCHANT_ID,
- 'value' => $this->_data[self::MERCHANT_ID],
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'order_id',
- 'value' => $this->getTransactionId(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'amount',
- 'value' => $this->getAmount(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'description',
- 'value' => $this->getName(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'callback_url',
- 'value' => $this->getIpnUrl(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'success_url',
- 'value' => $this->getSuccessUrl(),
- 'element' => 'hidden',
- ),
- array(
- 'id' => 'cancel_url',
- 'value' => $this->getFailureUrl(),
- 'element' => 'hidden',
- ),
- );
- }
- public function getPostUrl()
- {
- return self::POST_URL;
- }
-
- public function processIpn(AbstractRequest $request)
- {
- $errno = null;
- $errstr = null;
- $response = false;
- if ($request->isPost()) {
- $params = array();
- foreach ($request->getParams() as $key => $value) {
- $params[] = $key . '=' . urlencode($value);
- }
- $content = implode('&', $params);
- $header = "POST /nochex.dll/apc/apc HTTP/1.0\r\n" .
- "Content-Type: application/x-www-form-urlencoded\r\n" .
- "Content-Length: " . strlen($content) . "\r\n\r\n";
- $fp = fsockopen("www.nochex.com", 80, $errno, $errstr, 10);
- fputs($fp, $header . $content);
- $paymentStatus = $_POST['payment_status'];
- $this->setTransactionId($_POST['custom'])
- ->setAmount($_POST['amount'])
- ->setCurrency('GBP')
- ->setGatewayPaymentStatus($paymentStatus)
- ->setGatewayTransactionCode($_POST['transaction_id']);
- while (!feof($fp)) {
- $result = trim(fgets($fp, 1024));
- if (strstr($result, 'AUTHORISED') && trim($paymentStatus) == 'live') {
- $response = true;
- }
- else {
- $this->setGatewayPaymentStatus($result);
- }
- }
- fclose($fp);
- }
- return $response;
- }
- }
|