divido.php (9909B)
1 <?php 2 class ControllerExtensionPaymentDivido extends Controller { 3 const 4 STATUS_ACCEPTED = 'ACCEPTED', 5 STATUS_ACTION_LENDER = 'ACTION-LENDER', 6 STATUS_CANCELED = 'CANCELED', 7 STATUS_COMPLETED = 'COMPLETED', 8 STATUS_DEPOSIT_PAID = 'DEPOSIT-PAID', 9 STATUS_DECLINED = 'DECLINED', 10 STATUS_DEFERRED = 'DEFERRED', 11 STATUS_REFERRED = 'REFERRED', 12 STATUS_FULFILLED = 'FULFILLED', 13 STATUS_SIGNED = 'SIGNED'; 14 15 private $status_id = array( 16 self::STATUS_ACCEPTED => 1, 17 self::STATUS_ACTION_LENDER => 2, 18 self::STATUS_CANCELED => 0, 19 self::STATUS_COMPLETED => 2, 20 self::STATUS_DECLINED => 8, 21 self::STATUS_DEFERRED => 1, 22 self::STATUS_REFERRED => 1, 23 self::STATUS_DEPOSIT_PAID => 1, 24 self::STATUS_FULFILLED => 1, 25 self::STATUS_SIGNED => 2, 26 ); 27 28 private $history_messages = array( 29 self::STATUS_ACCEPTED => 'Credit request accepted', 30 self::STATUS_ACTION_LENDER => 'Lender notified', 31 self::STATUS_CANCELED => 'Credit request canceled', 32 self::STATUS_COMPLETED => 'Credit application completed', 33 self::STATUS_DECLINED => 'Credit request declined', 34 self::STATUS_DEFERRED => 'Credit request deferred', 35 self::STATUS_REFERRED => 'Credit request referred', 36 self::STATUS_DEPOSIT_PAID => 'Deposit paid', 37 self::STATUS_FULFILLED => 'Credit request fulfilled', 38 self::STATUS_SIGNED => 'Contract signed', 39 ); 40 41 public function index() { 42 $this->load->language('extension/payment/divido'); 43 $this->load->model('extension/payment/divido'); 44 $this->load->model('checkout/order'); 45 46 $api_key = $this->config->get('payment_divido_api_key'); 47 $key_parts = explode('.', $api_key); 48 $js_key = strtolower(array_shift($key_parts)); 49 50 list($total, $totals) = $this->model_extension_payment_divido->getOrderTotals(); 51 52 $this->model_extension_payment_divido->setMerchant($this->config->get('payment_divido_api_key')); 53 54 $plans = $this->model_extension_payment_divido->getCartPlans($this->cart); 55 foreach ($plans as $key => $plan) { 56 $planMinTotal = $total - ($total * ($plan->min_deposit / 100)); 57 if ($plan->min_amount > $planMinTotal) { 58 unset($plans[$key]); 59 } 60 } 61 62 $plans_ids = array_map(function ($plan) { 63 return $plan->id; 64 }, $plans); 65 $plans_ids = array_unique($plans_ids); 66 $plans_list = implode(',', $plans_ids); 67 68 $data = array( 69 'button_confirm' => $this->language->get('divido_checkout'), 70 'merchant_script' => "//cdn.divido.com/calculator/{$js_key}.js", 71 'grand_total' => $total, 72 'plan_list' => $plans_list, 73 'generic_credit_req_error' => 'Credit request could not be initiated', 74 ); 75 76 return $this->load->view('extension/payment/divido', $data); 77 } 78 79 public function update() { 80 $this->load->language('extension/payment/divido'); 81 $this->load->model('extension/payment/divido'); 82 $this->load->model('checkout/order'); 83 84 $data = json_decode(file_get_contents('php://input')); 85 86 if (!isset($data->status)) { 87 $this->response->setOutput(''); 88 return; 89 } 90 91 $lookup = $this->model_extension_payment_divido->getLookupByOrderId($data->metadata->order_id); 92 if ($lookup->num_rows != 1) { 93 $this->response->setOutput(''); 94 return; 95 } 96 97 $hash = $this->model_extension_payment_divido->hashOrderId($data->metadata->order_id, $lookup->row['salt']); 98 if ($hash !== $data->metadata->order_hash) { 99 $this->response->setOutput(''); 100 return; 101 } 102 103 $order_id = $data->metadata->order_id; 104 $order_info = $this->model_checkout_order->getOrder($order_id); 105 $status_id = $order_info['order_status_id']; 106 $message = "Status: {$data->status}"; 107 if (isset($this->history_messages[$data->status])) { 108 $message = $this->history_messages[$data->status]; 109 } 110 111 if ($data->status == self::STATUS_SIGNED) { 112 $status_override = $this->config->get('payment_divido_order_status_id'); 113 if (!empty($status_override)) { 114 $this->status_id[self::STATUS_SIGNED] = $status_override; 115 } 116 } 117 118 if (isset($this->status_id[$data->status]) && $this->status_id[$data->status] > $status_id) { 119 $status_id = $this->status_id[$data->status]; 120 } 121 122 if ($data->status == self::STATUS_DECLINED && $order_info['order_status_id'] == 0) { 123 $status_id = 0; 124 } 125 126 $this->model_extension_payment_divido->saveLookup($data->metadata->order_id, $lookup->row['salt'], null, $data->application); 127 $this->model_checkout_order->addOrderHistory($order_id, $status_id, $message, false); 128 $this->response->setOutput('ok'); 129 } 130 131 public function confirm() { 132 $this->load->language('extension/payment/divido'); 133 134 $this->load->model('extension/payment/divido'); 135 136 ini_set('html_errors', 0); 137 if (!$this->session->data['payment_method']['code'] == 'divido') { 138 return false; 139 } 140 141 $this->model_extension_payment_divido->setMerchant($this->config->get('payment_divido_api_key')); 142 143 $api_key = $this->config->get('payment_divido_api_key'); 144 145 $deposit = $this->request->post['deposit']; 146 $finance = $this->request->post['finance']; 147 148 $address = $this->session->data['payment_address']; 149 if (isset($this->session->data['shipping_address'])) { 150 $address = $this->session->data['shipping_address']; 151 } 152 153 $country = $address['iso_code_2']; 154 $language = strtoupper($this->language->get('code')); 155 $currency = strtoupper($this->session->data['currency']); 156 $order_id = $this->session->data['order_id']; 157 158 if ($this->customer->isLogged()) { 159 $this->load->model('account/customer'); 160 $customer_info = $this->model_account_customer->getCustomer($this->customer->getId()); 161 162 $firstname = $customer_info['firstname']; 163 $lastname = $customer_info['lastname']; 164 $email = $customer_info['email']; 165 $telephone = $customer_info['telephone']; 166 } elseif (isset($this->session->data['guest'])) { 167 $firstname = $this->session->data['guest']['firstname']; 168 $lastname = $this->session->data['guest']['lastname']; 169 $email = $this->session->data['guest']['email']; 170 $telephone = $this->session->data['guest']['telephone']; 171 } 172 173 $postcode = $address['postcode']; 174 175 $products = array(); 176 foreach ($this->cart->getProducts() as $product) { 177 $products[] = array( 178 'type' => 'product', 179 'text' => $product['name'], 180 'quantity' => $product['quantity'], 181 'value' => $product['price'], 182 ); 183 } 184 185 list($total, $totals) = $this->model_extension_payment_divido->getOrderTotals(); 186 187 $sub_total = $total; 188 $cart_total = $this->cart->getSubTotal(); 189 $shiphandle = $sub_total - $cart_total; 190 191 $products[] = array( 192 'type' => 'product', 193 'text' => 'Shipping & Handling', 194 'quantity' => 1, 195 'value' => $shiphandle, 196 ); 197 198 $deposit_amount = round(($deposit / 100) * $total, 2, PHP_ROUND_HALF_UP); 199 200 $shop_url = $this->config->get('config_url'); 201 if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) { 202 $shop_url = $this->config->get('config_ssl'); 203 } 204 205 $callback_url = $this->url->link('extension/payment/divido/update', '', true); 206 $return_url = $this->url->link('checkout/success', '', true); 207 $checkout_url = $this->url->link('checkout/checkout', '', true); 208 209 $salt = uniqid('', true); 210 $hash = $this->model_extension_payment_divido->hashOrderId($order_id, $salt); 211 212 $request_data = array( 213 'merchant' => $api_key, 214 'deposit' => $deposit_amount, 215 'finance' => $finance, 216 'country' => $country, 217 'language' => $language, 218 'currency' => $currency, 219 'metadata' => array( 220 'order_id' => $order_id, 221 'order_hash' => $hash, 222 ), 223 'customer' => array( 224 'title' => '', 225 'first_name' => $firstname, 226 'middle_name' => '', 227 'last_name' => $lastname, 228 'country' => $country, 229 'postcode' => $postcode, 230 'email' => $email, 231 'mobile_number' => '', 232 'phone_number' => $telephone, 233 ), 234 'products' => $products, 235 'response_url' => $callback_url, 236 'redirect_url' => $return_url, 237 'checkout_url' => $checkout_url, 238 ); 239 240 $response = Divido_CreditRequest::create($request_data); 241 242 if ($response->status == 'ok') { 243 244 $this->model_extension_payment_divido->saveLookup($order_id, $salt, $response->id, null, $deposit_amount); 245 246 $data = array( 247 'status' => 'ok', 248 'url' => $response->url, 249 ); 250 } else { 251 $data = array( 252 'status' => 'error', 253 'message' => $this->language->get($response->error), 254 ); 255 } 256 257 $this->response->setOutput(json_encode($data)); 258 } 259 260 public function calculator($args) { 261 $this->load->language('extension/payment/divido'); 262 263 $this->load->model('extension/payment/divido'); 264 265 if (!$this->model_extension_payment_divido->isEnabled()) { 266 return null; 267 } 268 269 $this->model_extension_payment_divido->setMerchant($this->config->get('payment_divido_api_key')); 270 271 $product_selection = $this->config->get('payment_divido_productselection'); 272 $price_threshold = $this->config->get('payment_divido_price_threshold'); 273 $product_id = $args['product_id']; 274 $product_price = $args['price']; 275 $type = $args['type']; 276 277 if ($product_selection == 'threshold' && $product_price < $price_threshold) { 278 return null; 279 } 280 281 $plans = $this->model_extension_payment_divido->getProductPlans($product_id); 282 if (empty($plans)) { 283 return null; 284 } 285 286 $plans_ids = array_map(function ($plan) { 287 return $plan->id; 288 }, $plans); 289 290 $plan_list = implode(',', $plans_ids); 291 292 $data = array( 293 'planList' => $plan_list, 294 'productPrice' => $product_price 295 ); 296 297 $filename = ($type == 'full') ? 'extension/payment/divido_calculator' : 'extension/payment/divido_widget'; 298 299 return $this->load->view($filename, $data); 300 } 301 }