shop.balmet.com

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

pilibaba.php (17210B)


      1 <?php
      2 class ControllerExtensionPaymentPilibaba extends Controller {
      3 	public function index() {
      4 		$this->load->language('extension/payment/pilibaba');
      5 
      6 		$this->load->model('checkout/order');
      7 		$this->load->model('extension/payment/pilibaba');
      8 
      9 		$this->model_extension_payment_pilibaba->log('Regular called');
     10 
     11 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     12 
     13 		$data['version']      = 'V2.0.01';
     14 		$data['merchantNo']   = $this->config->get('payment_pilibaba_merchant_number');
     15 		$data['currencyType'] = $order_info['currency_code'];
     16 		$data['orderNo']      = $order_info['order_id'];
     17 		$data['orderAmount']  = intval(round($order_info['total'], 2) * 100);
     18 		$data['orderTime']    = date('Y-m-d H:i:s');
     19 		$data['pageUrl']      = $this->url->link('checkout/checkout', '', true);
     20 		$data['serverUrl']    = $this->url->link('extension/payment/pilibaba/callback', '', true);
     21 		$data['redirectUrl']  = $this->url->link('checkout/success', '', true);
     22 		$data['notifyType']   = 'json';
     23 		$data['shipper']      = 0;
     24 		$data['tax']          = ($this->config->get('config_tax')) ? 0 : $this->model_extension_payment_pilibaba->getOrderTaxAmount($order_info['order_id']);
     25 		$data['signType']     = 'MD5';
     26 		$data['signMsg']      = strtoupper(md5($data['version'] . $data['merchantNo'] . $data['currencyType'] . $data['orderNo'] . $data['orderAmount'] . $data['orderTime'] . $data['pageUrl'] . $data['serverUrl'] . $data['redirectUrl'] . $data['notifyType'] . $data['shipper'] . $data['tax'] . $data['signType'] . $this->config->get('payment_pilibaba_secret_key')));
     27 
     28 		$products = array();
     29 
     30 		foreach ($this->cart->getProducts() as $product) {
     31 			// kilograms
     32 			if ($product['weight_class_id'] == '1') {
     33 				$weight = intval(round($product['weight'], 2) * 1000);
     34 			} else {
     35 				$weight = intval($product['weight']);
     36 			}
     37 
     38 			$products[] = array_map('strval', array(
     39 				'name'       => $product['name'],
     40 				'pictureUrl' => $this->config->get('config_url') . 'image/' . $product['image'],
     41 				'price'      => intval(round($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'), $this->session->data['currency']), 2) * 100),
     42 				'productUrl' => str_replace('&amp;', '&', $this->url->link('product/product', 'product_id=' . $product['product_id'])),
     43 				'productId'  => $product['product_id'],
     44 				'quantity'   => $product['quantity'],
     45 				'weight'     => $weight
     46 			));
     47 		}
     48 
     49 		$data['products'] = $products;
     50 
     51 		$data['goodsList'] = urlencode(json_encode($products));
     52 
     53 		if ($this->config->get('payment_pilibaba_environment') == 'live') {
     54 			$data['url'] = 'https://www.pilibaba.com/pilipay/payreq';
     55 		} else {
     56 			$data['url'] = 'http://pre.pilibaba.com/pilipay/payreq';
     57 		}
     58 
     59 		$data['auto_submit'] = false;
     60 
     61 		$this->model_extension_payment_pilibaba->log('Request: ' . print_r($data, true));
     62 
     63 		return $this->load->view('extension/payment/pilibaba', $data);
     64 	}
     65 
     66 	public function express() {
     67 		$this->load->language('extension/shipping/pilibaba');
     68 
     69 		$this->load->language('extension/payment/pilibaba');
     70 
     71 		$this->load->model('extension/payment/pilibaba');
     72 
     73 		$this->model_extension_payment_pilibaba->log('Express called');
     74 
     75 		if ($this->config->get('payment_pilibaba_status')) {
     76 			if (!$this->cart->hasProducts() || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
     77 				$this->model_extension_payment_pilibaba->log('No physical products. Redirecting to checkout/cart');
     78 
     79 				$this->response->redirect($this->url->link('checkout/cart'));
     80 			} else {
     81 				$order_data = array();
     82 
     83 				$totals = array();
     84 				$taxes = $this->cart->getTaxes();
     85 				$total = 0;
     86 
     87 				// Because __call can not keep var references so we put them into an array.
     88 				$total_data = array(
     89 					'totals' => &$totals,
     90 					'taxes'  => &$taxes,
     91 					'total'  => &$total
     92 				);
     93 
     94 				$this->load->model('setting/extension');
     95 
     96 				$sort_order = array();
     97 
     98 				$results = $this->model_setting_extension->getExtensions('total');
     99 
    100 				foreach ($results as $key => $value) {
    101 					$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
    102 				}
    103 
    104 				array_multisort($sort_order, SORT_ASC, $results);
    105 
    106 				foreach ($results as $result) {
    107 					if ($this->config->get('total_' . $result['code'] . '_status')) {
    108 						$this->load->model('extension/total/' . $result['code']);
    109 
    110 						// We have to put the totals in an array so that they pass by reference.
    111 						$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
    112 					}
    113 				}
    114 
    115 				$sort_order = array();
    116 
    117 				foreach ($totals as $key => $value) {
    118 					$sort_order[$key] = $value['sort_order'];
    119 				}
    120 
    121 				array_multisort($sort_order, SORT_ASC, $totals);
    122 
    123 				$order_data['totals'] = $totals;
    124 
    125 				$this->load->language('checkout/checkout');
    126 
    127 				$order_data['invoice_prefix'] = $this->config->get('config_invoice_prefix');
    128 				$order_data['store_id'] = $this->config->get('config_store_id');
    129 				$order_data['store_name'] = $this->config->get('config_name');
    130 
    131 				if ($order_data['store_id']) {
    132 					$order_data['store_url'] = $this->config->get('config_url');
    133 				} else {
    134 					$order_data['store_url'] = HTTP_SERVER;
    135 				}
    136 
    137 				$order_data['customer_id'] = 0;
    138 				$order_data['customer_group_id'] = $this->config->get('config_customer_group_id');
    139 				$order_data['firstname'] = '';
    140 				$order_data['lastname'] = '';
    141 				$order_data['email'] = '';
    142 				$order_data['telephone'] = '';
    143 				$order_data['custom_field'] = null;
    144 
    145 				$order_data['payment_firstname'] = '';
    146 				$order_data['payment_lastname'] = '';
    147 				$order_data['payment_company'] = '';
    148 				$order_data['payment_address_1'] = '';
    149 				$order_data['payment_address_2'] = '';
    150 				$order_data['payment_city'] = '';
    151 				$order_data['payment_postcode'] = '';
    152 				$order_data['payment_zone'] = '';
    153 				$order_data['payment_zone_id'] = 0;
    154 				$order_data['payment_country'] = '';
    155 				$order_data['payment_country_id'] = 0;
    156 				$order_data['payment_address_format'] = '';
    157 				$order_data['payment_custom_field'] = array();
    158 				$order_data['payment_method'] = $this->language->get('text_title');
    159 				$order_data['payment_code'] = 'pilibaba';
    160 
    161 				$order_data['shipping_firstname'] = '';
    162 				$order_data['shipping_lastname'] = '';
    163 				$order_data['shipping_company'] = '';
    164 				$order_data['shipping_address_1'] = '';
    165 				$order_data['shipping_address_2'] = '';
    166 				$order_data['shipping_city'] = '';
    167 				$order_data['shipping_postcode'] = '';
    168 				$order_data['shipping_zone'] = '';
    169 				$order_data['shipping_zone_id'] = 0;
    170 				$order_data['shipping_country'] = '';
    171 				$order_data['shipping_country_id'] = 0;
    172 				$order_data['shipping_address_format'] = '';
    173 				$order_data['shipping_custom_field'] = array();
    174 				$order_data['shipping_method'] = $this->language->get('text_description');
    175 				$order_data['shipping_code'] = 'pilibaba.pilibaba';
    176 
    177 				$this->session->data['guest']['customer_group_id'] = $this->config->get('config_customer_group_id');
    178 				$this->session->data['guest']['firstname'] = '';
    179 				$this->session->data['guest']['lastname'] = '';
    180 				$this->session->data['guest']['email'] = '';
    181 				$this->session->data['guest']['telephone'] = '';
    182 				$this->session->data['guest']['custom_field'] = array();
    183 
    184 				$order_data['products'] = array();
    185 
    186 				foreach ($this->cart->getProducts() as $product) {
    187 					$option_data = array();
    188 
    189 					foreach ($product['option'] as $option) {
    190 						$option_data[] = array(
    191 							'product_option_id'       => $option['product_option_id'],
    192 							'product_option_value_id' => $option['product_option_value_id'],
    193 							'option_id'               => $option['option_id'],
    194 							'option_value_id'         => $option['option_value_id'],
    195 							'name'                    => $option['name'],
    196 							'value'                   => $option['value'],
    197 							'type'                    => $option['type']
    198 						);
    199 					}
    200 
    201 					$order_data['products'][] = array(
    202 						'product_id' => $product['product_id'],
    203 						'name'       => $product['name'],
    204 						'model'      => $product['model'],
    205 						'option'     => $option_data,
    206 						'download'   => $product['download'],
    207 						'quantity'   => $product['quantity'],
    208 						'subtract'   => $product['subtract'],
    209 						'price'      => $product['price'],
    210 						'total'      => $product['total'],
    211 						'tax'        => $this->tax->getTax($product['price'], $product['tax_class_id']),
    212 						'reward'     => $product['reward']
    213 					);
    214 				}
    215 
    216 				// Gift Voucher
    217 				$order_data['vouchers'] = array();
    218 
    219 				if (!empty($this->session->data['vouchers'])) {
    220 					foreach ($this->session->data['vouchers'] as $voucher) {
    221 						$order_data['vouchers'][] = array(
    222 							'description'      => $voucher['description'],
    223 							'code'             => token(10),
    224 							'to_name'          => $voucher['to_name'],
    225 							'to_email'         => $voucher['to_email'],
    226 							'from_name'        => $voucher['from_name'],
    227 							'from_email'       => $voucher['from_email'],
    228 							'voucher_theme_id' => $voucher['voucher_theme_id'],
    229 							'message'          => $voucher['message'],
    230 							'amount'           => $voucher['amount']
    231 						);
    232 					}
    233 				}
    234 
    235 				$order_data['comment'] = '';
    236 				$order_data['total'] = $total_data['total'];
    237 
    238 				if (isset($this->request->cookie['tracking'])) {
    239 					$order_data['tracking'] = $this->request->cookie['tracking'];
    240 
    241 					$subtotal = $this->cart->getSubTotal();
    242 
    243 					// Affiliate
    244 					$this->load->model('affiliate/affiliate');
    245 
    246 					$affiliate_info = $this->model_affiliate_affiliate->getAffiliateByCode($this->request->cookie['tracking']);
    247 
    248 					if ($affiliate_info) {
    249 						$order_data['affiliate_id'] = $affiliate_info['affiliate_id'];
    250 						$order_data['commission'] = ($subtotal / 100) * $affiliate_info['commission'];
    251 					} else {
    252 						$order_data['affiliate_id'] = 0;
    253 						$order_data['commission'] = 0;
    254 					}
    255 
    256 					// Marketing
    257 					$this->load->model('checkout/marketing');
    258 
    259 					$marketing_info = $this->model_checkout_marketing->getMarketingByCode($this->request->cookie['tracking']);
    260 
    261 					if ($marketing_info) {
    262 						$order_data['marketing_id'] = $marketing_info['marketing_id'];
    263 					} else {
    264 						$order_data['marketing_id'] = 0;
    265 					}
    266 				} else {
    267 					$order_data['affiliate_id'] = 0;
    268 					$order_data['commission'] = 0;
    269 					$order_data['marketing_id'] = 0;
    270 					$order_data['tracking'] = '';
    271 				}
    272 
    273 				$order_data['language_id'] = $this->config->get('config_language_id');
    274 				$order_data['currency_id'] = $this->currency->getId($this->session->data['currency']);
    275 				$order_data['currency_code'] = $this->session->data['currency'];
    276 				$order_data['currency_value'] = $this->currency->getValue($this->session->data['currency']);
    277 				$order_data['ip'] = $this->request->server['REMOTE_ADDR'];
    278 
    279 				if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) {
    280 					$order_data['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR'];
    281 				} elseif (!empty($this->request->server['HTTP_CLIENT_IP'])) {
    282 					$order_data['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP'];
    283 				} else {
    284 					$order_data['forwarded_ip'] = '';
    285 				}
    286 
    287 				if (isset($this->request->server['HTTP_USER_AGENT'])) {
    288 					$order_data['user_agent'] = $this->request->server['HTTP_USER_AGENT'];
    289 				} else {
    290 					$order_data['user_agent'] = '';
    291 				}
    292 
    293 				if (isset($this->request->server['HTTP_ACCEPT_LANGUAGE'])) {
    294 					$order_data['accept_language'] = $this->request->server['HTTP_ACCEPT_LANGUAGE'];
    295 				} else {
    296 					$order_data['accept_language'] = '';
    297 				}
    298 
    299 				$this->load->model('checkout/order');
    300 
    301 				$this->session->data['order_id'] = $this->model_checkout_order->addOrder($order_data);
    302 
    303 				$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
    304 
    305 				$data['version']      = 'V2.0.01';
    306 				$data['merchantNo']   = $this->config->get('payment_pilibaba_merchant_number');
    307 				$data['currencyType'] = $order_info['currency_code'];
    308 				$data['orderNo']      = $order_info['order_id'];
    309 				$data['orderAmount']  = intval(round($order_info['total'], 2) * 100);
    310 				$data['orderTime']    = date('Y-m-d H:i:s');
    311 				$data['pageUrl']      = $this->url->link('checkout/checkout', '', true);
    312 				$data['serverUrl']    = $this->url->link('extension/payment/pilibaba/callback', '', true);
    313 				$data['redirectUrl']  = $this->url->link('checkout/success', '', true);
    314 				$data['notifyType']   = 'json';
    315 				$data['shipper']      = intval(round($this->config->get('payment_pilibaba_shipping_fee'), 2) * 100);
    316 				$data['tax']          = ($this->config->get('config_tax')) ? 0 : $this->model_extension_payment_pilibaba->getOrderTaxAmount($order_info['order_id']);
    317 				$data['signType']     = 'MD5';
    318 				$data['signMsg']      = strtoupper(md5($data['version'] . $data['merchantNo'] . $data['currencyType'] . $data['orderNo'] . $data['orderAmount'] . $data['orderTime'] . $data['pageUrl'] . $data['serverUrl'] . $data['redirectUrl'] . $data['notifyType'] . $data['shipper'] . $data['tax'] . $data['signType'] . $this->config->get('payment_pilibaba_secret_key')));
    319 
    320 				$products = array();
    321 
    322 				foreach ($this->cart->getProducts() as $product) {
    323 					// kilograms
    324 					if ($product['weight_class_id'] == '1') {
    325 						$weight = intval(round($product['weight'], 2) * 1000);
    326 					} else {
    327 						$weight = intval($product['weight']);
    328 					}
    329 
    330 					$products[] = array_map('strval', array(
    331 						'name'       => $product['name'],
    332 						'pictureUrl' => $this->config->get('config_url') . 'image/' . $product['image'],
    333 						'price'      => intval(round($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'), $this->session->data['currency']), 2) * 100),
    334 						'productUrl' => str_replace('&amp;', '&', $this->url->link('product/product', 'product_id=' . $product['product_id'])),
    335 						'productId'  => $product['product_id'],
    336 						'quantity'   => $product['quantity'],
    337 						'weight'     => $weight
    338 					));
    339 				}
    340 
    341 				$data['products'] = $products;
    342 
    343 				$data['goodsList'] = urlencode(json_encode($products));
    344 
    345 				if ($this->config->get('payment_pilibaba_environment') == 'live') {
    346 					$data['url'] = 'https://www.pilibaba.com/pilipay/payreq';
    347 				} else {
    348 					$data['url'] = 'http://pre.pilibaba.com/pilipay/payreq';
    349 				}
    350 
    351 				$data['text_redirecting'] = $this->language->get('text_redirecting');
    352 
    353 				$data['auto_submit'] = true;
    354 
    355 				$this->model_extension_payment_pilibaba->log('Request: ' . print_r($data, true));
    356 
    357 				$this->response->setOutput($this->load->view('extension/payment/pilibaba', $data));
    358 			}
    359 		} else {
    360 		   $this->model_extension_payment_pilibaba->log('Module disabled');
    361 		}
    362 	}
    363 
    364 	public function callback() {
    365 		$this->load->language('extension/payment/pilibaba');
    366 
    367 		$this->load->model('checkout/order');
    368 		$this->load->model('extension/payment/pilibaba');
    369 
    370 		$this->model_extension_payment_pilibaba->log('Receiving callback');
    371 
    372 		$response_data = $this->request->get;
    373 
    374 		$this->model_extension_payment_pilibaba->log('Response: ' . print_r($response_data, true));
    375 
    376 		$sign_msg = strtoupper(md5($this->config->get('payment_pilibaba_merchant_number') . $response_data['orderNo'] . $response_data['orderAmount'] . 'MD5' . $response_data['fee'] . $response_data['orderTime'] . $response_data['customerMail'] . $this->config->get('payment_pilibaba_secret_key')));
    377 
    378 		$this->model_extension_payment_pilibaba->log('signMsg: ' . $sign_msg);
    379 
    380 		if (hash_equals($sign_msg, $response_data['signMsg'])) {
    381 			$this->model_extension_payment_pilibaba->log('Adding Pilibaba order');
    382 
    383 			$this->model_extension_payment_pilibaba->addPilibabaOrder($response_data);
    384 
    385 			$this->model_extension_payment_pilibaba->log('Pilibaba order added');
    386 
    387 			$this->model_extension_payment_pilibaba->log('Getting consumer info');
    388 
    389 			$consumer_info = $this->model_extension_payment_pilibaba->getConsumerInfo($response_data['orderNo']);
    390 
    391 			if (isset($consumer_info['message']) && $consumer_info['message'] == 'success') {
    392 				$this->model_extension_payment_pilibaba->log('Updating order info');
    393 
    394 				$this->model_extension_payment_pilibaba->updateOrderInfo($consumer_info, $response_data['orderNo']);
    395 
    396 				$this->model_extension_payment_pilibaba->log('Order info updated');
    397 
    398 				$this->model_extension_payment_pilibaba->log('Adding order history');
    399 
    400 				$this->model_checkout_order->addOrderHistory($response_data['orderNo'], $this->config->get('payment_pilibaba_order_status_id'));
    401 
    402 				$this->model_extension_payment_pilibaba->log('Order history added');
    403 			} else {
    404 				$this->model_extension_payment_pilibaba->log('Invalid consumer info response');
    405 			}
    406 
    407 			$this->model_extension_payment_pilibaba->log('Outputting "OK"');
    408 
    409 			echo 'OK';
    410 
    411 			$this->model_extension_payment_pilibaba->log('"OK" outputted');
    412 		} else {
    413 			$this->model_extension_payment_pilibaba->log('Invalid callback response');
    414 		}
    415 	}
    416 }