Skip to content

Instantly share code, notes, and snippets.

@MarGul
Created November 11, 2019 13:26
Show Gist options
  • Save MarGul/5c74bccfa19a13f4fb61f9007211460e to your computer and use it in GitHub Desktop.
Save MarGul/5c74bccfa19a13f4fb61f9007211460e to your computer and use it in GitHub Desktop.
Lägg till detta i createOrder
/**
* Create a new order.
*
* @return void
*/
public function createOrder()
{
$rcoObj = $this->request->post['rcoObj'];
$reference = $this->request->post['reference'];
$response = array('success' => true, 'redirect' => false);
$customerData = $rcoObj['customerData'];
$orderData = array();
// Log for testing
$rand = mt_rand();
$log = new Log('loh_oc_rc.log');
$log->write(date('Y-m-d h:i:s') . ' - New create order. [' . $rand . ']');
$log->write('Reference: ' . $reference . ' - [' . $rand . ']');
$this->response->addHeader('Content-Type: application/json');
// If the cart is empty, i.e session has expired. Just redirect to cart.
if (!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) {
$response = array(
'success' => true,
'redirect' => true,
'redirect_to' => $this->url->link('checkout/cart')
);
$this->response->setOutput(json_encode($response));
return;
}
try {
// Set order metaData
$orderData['invoice_prefix'] = $this->config->get('config_invoice_prefix');
$orderData['store_id'] = $this->config->get('config_store_id');
$orderData['store_name'] = $this->config->get('config_name');
$orderData['language_id'] = $this->config->get('config_language_id');
$orderData['currency_id'] = $this->currency->getId($this->session->data['currency']);
$orderData['currency_code'] = $this->session->data['currency'];
$orderData['currency_value'] = $this->currency->getValue($this->session->data['currency']);
$orderData['ip'] = $this->request->server['REMOTE_ADDR'];
$orderData['user_agent'] = isset($this->request->server['HTTP_USER_AGENT']) ?
$this->request->server['HTTP_USER_AGENT'] :
'';
$orderData['accept_language'] = isset($this->request->server['HTTP_ACCEPT_LANGUAGE']) ?
$this->request->server['HTTP_ACCEPT_LANGUAGE'] :
'';
$orderData['comment'] = '';
if ($orderData['store_id']) {
$orderData['store_url'] = $this->config->get('config_url');
} else {
$orderData['store_url'] = $this->request->server['HTTPS'] ? HTTPS_SERVER : HTTP_SERVER;
}
if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) {
$orderData['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR'];
} elseif (!empty($this->request->server['HTTP_CLIENT_IP'])) {
$orderData['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP'];
} else {
$orderData['forwarded_ip'] = '';
}
// Add the customer information
$orderData['customer_id'] = $this->customer->isLogged() ? $this->customer->getId() : 0;
$orderData['customer_group_id'] = $this->customer->isLogged() ? $this->customer->getGroupId() : 1;
$orderData['firstname'] = $customerData['address']['firstname'];
$orderData['lastname'] = $customerData['address']['surname'];
$orderData['email'] = $customerData['address']['email'];
$orderData['telephone'] = $customerData['address']['telephone'];
$orderData['fax'] = '';
$orderData['custom_field'] = array();
// Add the payment information
$orderData['payment_firstname'] = $customerData['address']['firstname'];
$orderData['payment_lastname'] = $customerData['address']['surname'];
$orderData['payment_company'] = '';
$orderData['payment_address_1'] = $customerData['address']['address'];
$orderData['payment_address_2'] = $customerData['address']['addressExtra'];
$orderData['payment_city'] = $customerData['address']['city'];
$orderData['payment_postcode'] = $customerData['address']['postal'];
$orderData['payment_zone'] = '';
$orderData['payment_zone_id'] = '';
$orderData['payment_country'] = $customerData['address']['countryCode'];
$orderData['payment_country_id'] = '';
$orderData['payment_address_format'] = '';
$orderData['payment_custom_field'] = array(
'reference' => $reference,
'for_language' => $this->session->data['language']
);
$orderData['payment_method'] = 'Resurs Checkout';
$orderData['payment_code'] = 'resurs_checkout';
// Add the shipping information
$shipKey = !empty($customerData['delivery']) ? 'delivery' : 'address';
$orderData['shipping_firstname'] = $customerData[$shipKey]['firstname'];
$orderData['shipping_lastname'] = $customerData[$shipKey]['surname'];
$orderData['shipping_company'] = '';
$orderData['shipping_address_1'] = $customerData[$shipKey]['address'];
$orderData['shipping_address_2'] = $customerData[$shipKey]['addressExtra'];
$orderData['shipping_postcode'] = $customerData[$shipKey]['postal'];
$orderData['shipping_city'] = $customerData[$shipKey]['city'];
$orderData['shipping_zone_id'] = '';
$orderData['shipping_zone'] = '';
$orderData['shipping_zone_code'] = '';
$orderData['shipping_country_id'] = '';
$orderData['shipping_country'] = $customerData[$shipKey]['countryCode'];
$orderData['shipping_address_format'] = '';
$orderData['shipping_custom_field'] = array();
$orderData['shipping_method'] = !empty($this->session->data['shipping_method']) ?
$this->session->data['shipping_method']['title'] :
'';
$orderData['shipping_code'] = !empty($this->session->data['shipping_method']) ?
$this->session->data['shipping_method']['code'] :
'';
// Collect the products.
$orderData['products'] = array();
foreach ($this->cart->getProducts() as $product) {
$option_data = array();
foreach ($product['option'] as $option) {
$option_data[] = array(
'product_option_id' => $option['product_option_id'],
'product_option_value_id' => $option['product_option_value_id'],
'option_id' => $option['option_id'],
'option_value_id' => $option['option_value_id'],
'name' => $option['name'],
'value' => $option['value'],
'type' => $option['type']
);
}
$orderData['products'][] = array(
'product_id' => $product['product_id'],
'name' => $product['name'],
'model' => $product['model'],
'option' => $option_data,
'download' => $product['download'],
'quantity' => $product['quantity'],
'subtract' => $product['subtract'],
'price' => $product['price'],
'total' => $product['total'],
'tax' => $this->tax->getTax($product['price'], $product['tax_class_id']),
'reward' => $product['reward']
);
}
// Collect the vouchers
$orderData['vouchers'] = array();
if (!empty($this->session->data['vouchers'])) {
foreach ($this->session->data['vouchers'] as $voucher) {
$orderData['vouchers'][] = array(
'description' => $voucher['description'],
'code' => token(10),
'to_name' => $voucher['to_name'],
'to_email' => $voucher['to_email'],
'from_name' => $voucher['from_name'],
'from_email' => $voucher['from_email'],
'voucher_theme_id' => $voucher['voucher_theme_id'],
'message' => $voucher['message'],
'amount' => $voucher['amount']
);
}
}
// Set the totals
$totalsData = $this->getTotals();
$orderData['totals'] = $totalsData['totals'];
$orderData['total'] = $totalsData['total'];
// Set the tracking data if it's present.
if (isset($this->request->cookie['tracking'])) {
$orderData['tracking'] = $this->request->cookie['tracking'];
$subtotal = $this->cart->getSubTotal();
// Affiliate
$this->load->model('affiliate/affiliate');
$affiliate_info = $this->model_affiliate_affiliate->getAffiliateByCode($this->request->cookie['tracking']);
if ($affiliate_info) {
$orderData['affiliate_id'] = $affiliate_info['affiliate_id'];
$orderData['commission'] = ($subtotal / 100) * $affiliate_info['commission'];
} else {
$orderData['affiliate_id'] = 0;
$orderData['commission'] = 0;
}
// Marketing
$this->load->model('checkout/marketing');
$marketing_info = $this->model_checkout_marketing->getMarketingByCode($this->request->cookie['tracking']);
if ($marketing_info) {
$orderData['marketing_id'] = $marketing_info['marketing_id'];
} else {
$orderData['marketing_id'] = 0;
}
} else {
$orderData['affiliate_id'] = 0;
$orderData['commission'] = 0;
$orderData['marketing_id'] = 0;
$orderData['tracking'] = '';
}
// Add the order
$this->load->model('checkout/order');
$orderId = $this->model_checkout_order->addOrder($orderData);
$log->write('Got the orderID: ' . $orderId . ' - [' . $rand . ']');
$this->session->data['order_id'] = $orderId;
// If it's a zero sum order we are just adding the order and redirecting to the success page.
// Otherwise we will add it like normal to Resurs.
if ($this->cartTotal() <= 0) {
$response['redirect'] = true;
$response['redirect_to'] = $this->url->link('checkout/success');
// Set the 0 order to status
$this->load->model('checkout/order');
$this->model_checkout_order->addOrderHistory(
$orderId,
$this->getOrderStatusId('on_hold'),
'0 sum order got handled by Resurs Checkout.'
);
} else {
// Update the payment reference
$account = $this->getAccountByLanguage();
$rcConnector = $this->connectorInstance($account);
if ($rcConnector->updatePaymentReference($reference, $orderId)) {
$log->write('Successfully updated the reference - [' . $rand . ']');
} else {
throw new Exception;
}
}
} catch (Exception $e) {
$log->write('There was an exception thrown - [' . $rand . ']');
$log->write($e->__toString());
$this->load->language('extension/payment/resurs_checkout');
// Set a session flash about the error.
$this->session->data['error_flash_msg'] = $this->language->get('order_fail_critical_alert_message');
$response = array(
'success' => false,
'message' => $this->language->get('order_fail_critical_js_message'),
'exception' => $e
);
}
$this->response->setOutput(json_encode($response));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment