shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

g2apay.php (7069B)


      1 <?php
      2 class ControllerExtensionPaymentG2APay extends Controller {
      3 	public function index() {
      4 		$this->load->language('extension/payment/g2apay');
      5 
      6 		$data['action'] = $this->url->link('extension/payment/g2apay/checkout', '', true);
      7 
      8 		return $this->load->view('extension/payment/g2apay', $data);
      9 	}
     10 
     11 	public function checkout() {
     12 		$this->load->model('checkout/order');
     13 		$this->load->model('account/order');
     14 		$this->load->model('extension/payment/g2apay');
     15 
     16 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     17 
     18 		$order_data = array();
     19 
     20 		$this->load->model('setting/extension');
     21 
     22 		$totals = array();
     23 		$taxes = $this->cart->getTaxes();
     24 		$total = 0;
     25 
     26 		// Because __call can not keep var references so we put them into an array.
     27 		$total_data = array(
     28 			'totals' => &$totals,
     29 			'taxes'  => &$taxes,
     30 			'total'  => &$total
     31 		);
     32 
     33 		$i = 0;
     34 
     35 		$results = $this->model_setting_extension->getExtensions('total');
     36 
     37 		foreach ($results as $result) {
     38 			if ($this->config->get('total_' . $result['code'] . '_status')) {
     39 				$this->load->model('extension/total/' . $result['code']);
     40 
     41 				// We have to put the totals in an array so that they pass by reference.
     42 				$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
     43 
     44 				if (isset($order_data['totals'][$i])) {
     45 					if (strstr(strtolower($order_data['totals'][$i]['code']), 'total') === false) {
     46 						$item = new stdClass();
     47 						$item->sku = $order_data['totals'][$i]['code'];
     48 						$item->name = $order_data['totals'][$i]['title'];
     49 						$item->amount = number_format($order_data['totals'][$i]['value'], 2);
     50 						$item->qty = 1;
     51 						$item->id = $order_data['totals'][$i]['code'];
     52 						$item->price = $order_data['totals'][$i]['value'];
     53 						$item->url = $this->url->link('common/home', '', true);
     54 						$items[] = $item;
     55 					}
     56 
     57 					$i++;
     58 				}
     59 			}
     60 		}
     61 
     62 		$ordered_products = $this->model_account_order->getOrderProducts($this->session->data['order_id']);
     63 
     64 		foreach ($ordered_products as $product) {
     65 			$item = new stdClass();
     66 			$item->sku = $product['product_id'];
     67 			$item->name = $product['name'];
     68 			$item->amount = $product['price'] * $product['quantity'];
     69 			$item->qty = $product['quantity'];
     70 			$item->id = $product['product_id'];
     71 			$item->price = $product['price'];
     72 			$item->url = $this->url->link('product/product', 'product_id=' . $product['product_id'], true);
     73 			$items[] = $item;
     74 		}
     75 
     76 		if ($this->config->get('payment_g2apay_environment') == 1) {
     77 			$url = 'https://checkout.pay.g2a.com/index/createQuote';
     78 		} else {
     79 			$url = 'https://checkout.test.pay.g2a.com/index/createQuote';
     80 		}
     81 
     82 		$order_total = number_format($order_info['total'], 2);
     83 
     84 		$string = $this->session->data['order_id'] . $order_total . $order_info['currency_code'] . html_entity_decode($this->config->get('payment_g2apay_secret'));
     85 
     86 		$fields = array(
     87 			'api_hash' => $this->config->get('payment_g2apay_api_hash'),
     88 			'hash' => hash('sha256', $string),
     89 			'order_id' => $this->session->data['order_id'],
     90 			'amount' => $order_total,
     91 			'currency' => $order_info['currency_code'],
     92 			'email' => $order_info['email'],
     93 			'url_failure' => $this->url->link('checkout/failure'),
     94 			'url_ok' => $this->url->link('extension/payment/g2apay/success'),
     95 			'items' => json_encode($items)
     96 		);
     97 
     98 		$response_data = $this->model_extension_payment_g2apay->sendCurl($url, $fields);
     99 
    100 		$this->model_extension_payment_g2apay->logger($order_total);
    101 		$this->model_extension_payment_g2apay->logger($items);
    102 		$this->model_extension_payment_g2apay->logger($fields);
    103 
    104 		if ($response_data === false) {
    105 			$this->response->redirect($this->url->link('extension/payment/failure', '', true));
    106 		}
    107 
    108 		if (strtolower($response_data->status) != 'ok') {
    109 			$this->response->redirect($this->url->link('extension/payment/failure', '', true));
    110 		}
    111 
    112 		$this->model_extension_payment_g2apay->addG2aOrder($order_info);
    113 
    114 		if ($this->config->get('payment_g2apay_environment') == 1) {
    115 			$this->response->redirect('https://checkout.pay.g2a.com/index/gateway?token=' . $response_data->token);
    116 		} else {
    117 			$this->response->redirect('https://checkout.test.pay.g2a.com/index/gateway?token=' . $response_data->token);
    118 		}
    119 	}
    120 
    121 	public function success() {
    122 		$order_id = $this->session->data['order_id'];
    123 
    124 		if (isset($this->request->post['transaction_id'])) {
    125 			$g2apay_transaction_id = $this->request->post['transaction_id'];
    126 		} elseif (isset($this->request->get['transaction_id'])) {
    127 			$g2apay_transaction_id = $this->request->get['transaction_id'];
    128 		} else {
    129 			$g2apay_transaction_id = '';
    130 		}
    131 
    132 		$this->load->model('checkout/order');
    133 
    134 		$order_info = $this->model_checkout_order->getOrder($order_id);
    135 
    136 		if ($order_info) {
    137 			$this->load->model('extension/payment/g2apay');
    138 
    139 			$g2apay_order_info = $this->model_extension_payment_g2apay->getG2aOrder($order_id);
    140 
    141 			$this->model_extension_payment_g2apay->updateOrder($g2apay_order_info['g2apay_order_id'], $g2apay_transaction_id, 'payment', $order_info);
    142 
    143 			$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_g2apay_order_status_id'));
    144 		}
    145 
    146 		$this->response->redirect($this->url->link('checkout/success'));
    147 	}
    148 
    149 	public function ipn() {
    150 		$this->load->model('extension/payment/g2apay');
    151 		$this->model_extension_payment_g2apay->logger('ipn');
    152 
    153 		if (isset($this->request->get['token']) && hash_equals($this->config->get('payment_g2apay_secret_token'), $this->request->get['token'])) {
    154 			$this->model_extension_payment_g2apay->logger('token success');
    155 
    156 			if (isset($this->request->post['userOrderId'])) {
    157 				$g2apay_order = $this->model_extension_payment_g2apay->getG2aOrder($this->request->post['userOrderId']);
    158 
    159 				$string = $g2apay_order['g2apay_transaction_id'] . $g2apay_order['order_id'] . round($g2apay_order['total'], 2) . html_entity_decode($this->config->get('payment_g2apay_secret'));
    160 				$hash = hash('sha256', $string);
    161 				if($hash != $this->request->post['hash']){
    162 					$this->model_extension_payment_g2apay->logger('Hashes do not match, possible tampering!');
    163 					return;
    164 				}
    165 
    166 				switch ($this->request->post['status']) {
    167 					case 'complete':
    168 						$order_status_id = $this->config->get('payment_g2apay_complete_status_id');
    169 						break;
    170 					case 'rejected':
    171 						$order_status_id = $this->config->get('payment_g2apay_rejected_status_id');
    172 						break;
    173 					case 'canceled':
    174 						$order_status_id = $this->config->get('payment_g2apay_cancelled_status_id');
    175 						break;
    176 					case 'partial_refunded':
    177 						$order_status_id = $this->config->get('payment_g2apay_partially_refunded_status_id');
    178 						break;
    179 					case 'refunded':
    180 						$order_status_id = $this->config->get('payment_g2apay_refunded_status_id');
    181 						break;
    182 				}
    183 
    184 				$this->load->model('checkout/order');
    185 				$this->model_checkout_order->addOrderHistory($this->request->post['userOrderId'], $order_status_id);
    186 			}
    187 		}
    188 	}
    189 }