shop.balmet.com

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

squareup.php (12239B)


      1 <?php
      2 
      3 class ControllerExtensionPaymentSquareup extends Controller {
      4     public function index() {
      5         $this->load->language('extension/payment/squareup');
      6 
      7         $this->load->library('squareup');
      8 
      9         $data['action'] = $this->url->link('extension/payment/squareup/checkout', '', true);
     10         $data['squareup_js_api'] = Squareup::PAYMENT_FORM_URL;
     11         
     12         if (!empty($this->session->data['payment_address']['postcode'])) {
     13             $data['payment_zip'] = $this->session->data['payment_address']['postcode'];
     14         } else {
     15             $data['payment_zip'] = '';
     16         }
     17 
     18         if ($this->config->get('payment_squareup_enable_sandbox')) {
     19             $data['app_id'] = $this->config->get('payment_squareup_sandbox_client_id');
     20             $data['sandbox_message'] = $this->language->get('warning_test_mode');
     21         } else {
     22             $data['app_id'] = $this->config->get('payment_squareup_client_id');
     23             $data['sandbox_message'] = '';
     24         }
     25 
     26         $data['cards'] = array();
     27 
     28         if ($this->customer->isLogged()) {
     29             $data['is_logged'] = true;
     30 
     31             $this->load->model('extension/credit_card/squareup');
     32 
     33             $cards = $this->model_extension_credit_card_squareup->getCards($this->customer->getId(), $this->config->get('payment_squareup_enable_sandbox'));
     34 
     35             foreach ($cards as $card) {
     36                 $data['cards'][] = array(
     37                     'id' => $card['squareup_token_id'],
     38                     'text' => sprintf($this->language->get('text_card_ends_in'), $card['brand'], $card['ends_in'])
     39                 );
     40             }
     41         } else {
     42             $data['is_logged'] = false;
     43         }
     44 
     45         return $this->load->view('extension/payment/squareup', $data);
     46     }
     47 
     48     public function checkout() {
     49         $this->load->language('extension/payment/squareup');
     50 
     51         $this->load->model('extension/payment/squareup');
     52         $this->load->model('extension/credit_card/squareup');
     53         $this->load->model('checkout/order');
     54         $this->load->model('localisation/country');
     55 
     56         $this->load->library('squareup');
     57 
     58         $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     59 
     60         $shipping_country_info = $this->model_localisation_country->getCountry($order_info['shipping_country_id']);
     61 
     62         $billing_country_info = $this->model_localisation_country->getCountry($order_info['payment_country_id']);
     63 
     64         if (!empty($billing_country_info)) {
     65             $billing_address = array(
     66                 'first_name' => $order_info['payment_firstname'],
     67                 'last_name' => $order_info['payment_lastname'],
     68                 'address_line_1' => $order_info['payment_address_1'],
     69                 'address_line_2' => $order_info['payment_address_2'],
     70                 'locality' => $order_info['payment_city'],
     71                 'sublocality' => $order_info['payment_zone'],
     72                 'postal_code' => $order_info['payment_postcode'],
     73                 'country' => $billing_country_info['iso_code_2'],
     74                 'organization' => $order_info['payment_company']
     75             );
     76         } else {
     77             $billing_address = array();
     78         }
     79 
     80         if (!empty($shipping_country_info)) {
     81             $shipping_address = array(
     82                 'first_name' => $order_info['shipping_firstname'],
     83                 'last_name' => $order_info['shipping_lastname'],
     84                 'address_line_1' => $order_info['shipping_address_1'],
     85                 'address_line_2' => $order_info['shipping_address_2'],
     86                 'locality' => $order_info['shipping_city'],
     87                 'sublocality' => $order_info['shipping_zone'],
     88                 'postal_code' => $order_info['shipping_postcode'],
     89                 'country' => $shipping_country_info['iso_code_2'],
     90                 'organization' => $order_info['shipping_company']
     91             );
     92         } else {
     93             $shipping_address = array();
     94         }
     95 
     96         $json = array();
     97 
     98         try {
     99             // Ensure we have registered the customer with Square
    100             $square_customer = $this->model_extension_credit_card_squareup->getCustomer($this->customer->getId(), $this->config->get('payment_squareup_enable_sandbox'));
    101 
    102             if (!$square_customer && $this->customer->isLogged()) {
    103                 $square_customer = $this->squareup->addLoggedInCustomer();
    104 
    105                 $this->model_extension_credit_card_squareup->addCustomer($square_customer);
    106             }
    107 
    108             $use_saved = false;
    109             $square_card_id = null;
    110 
    111             // check if user is logged in and wanted to save this card
    112             if ($this->customer->isLogged() && !empty($this->request->post['squareup_select_card'])) {
    113                 $card_verified = $this->model_extension_credit_card_squareup->verifyCardCustomer($this->request->post['squareup_select_card'], $this->customer->getId());
    114 
    115                 if (!$card_verified) {
    116                     throw new \Squareup\Exception($this->registry, $this->language->get('error_card_invalid'));
    117                 }
    118 
    119                 $card = $this->model_extension_credit_card_squareup->getCard($this->request->post['squareup_select_card']);
    120 
    121                 $use_saved = true;
    122                 $square_card_id = $card['token'];
    123             } else if ($this->customer->isLogged() && isset($this->request->post['squareup_save_card'])) {
    124                 // Save the card
    125                 $card_data = array(
    126                     'card_nonce' => $this->request->post['squareup_nonce'],
    127                     'billing_address' => $billing_address,
    128                     'cardholder_name' => $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname']
    129                 );
    130 
    131                 $square_card = $this->squareup->addCard($square_customer['square_customer_id'], $card_data);
    132 
    133                 if (!$this->model_extension_credit_card_squareup->cardExists($this->customer->getId(), $square_card)) {
    134                     $this->model_extension_credit_card_squareup->addCard($this->customer->getId(), $this->config->get('payment_squareup_enable_sandbox'), $square_card);
    135                 }
    136                 
    137                 $use_saved = true;
    138                 $square_card_id = $square_card['id'];
    139             }
    140 
    141             // Prepare Transaction
    142             $transaction_data = array(
    143                 'idempotency_key' => uniqid(),
    144                 'amount_money' => array(
    145                     'amount' => $this->squareup->lowestDenomination($order_info['total'], $order_info['currency_code']),
    146                     'currency' => $order_info['currency_code']
    147                 ),
    148                 'billing_address' => $billing_address,
    149                 'buyer_email_address' => $order_info['email'],
    150                 'delay_capture' => !$this->cart->hasRecurringProducts() && $this->config->get('payment_squareup_delay_capture'),
    151                 'integration_id' => Squareup::SQUARE_INTEGRATION_ID
    152             );
    153             
    154             if (!empty($shipping_address)) {
    155                 $transaction_data['shipping_address'] = $shipping_address;
    156             }
    157 
    158             if ($use_saved) {
    159                 $transaction_data['customer_card_id'] = $square_card_id;
    160                 $transaction_data['customer_id'] = $square_customer['square_customer_id'];
    161             } else {
    162                 $transaction_data['card_nonce'] = $this->request->post['squareup_nonce'];
    163             }
    164 
    165             $transaction = $this->squareup->addTransaction($transaction_data);
    166 
    167             if (isset($this->request->server['HTTP_USER_AGENT'])) {
    168                 $user_agent = $this->request->server['HTTP_USER_AGENT'];
    169             } else {
    170                 $user_agent = '';
    171             }
    172 
    173             if (isset($this->request->server['REMOTE_ADDR'])) {
    174                 $ip = $this->request->server['REMOTE_ADDR'];
    175             } else {
    176                 $ip = '';
    177             }
    178 
    179             $this->model_extension_payment_squareup->addTransaction($transaction, $this->config->get('payment_squareup_merchant_id'), $billing_address, $this->session->data['order_id'], $user_agent, $ip);
    180 
    181             if (!empty($transaction['tenders'][0]['card_details']['status'])) {
    182                 $transaction_status = strtolower($transaction['tenders'][0]['card_details']['status']);
    183             } else {
    184                 $transaction_status = '';
    185             }
    186 
    187             $order_status_id = $this->config->get('payment_squareup_status_' . $transaction_status);
    188 
    189             if ($order_status_id) {
    190                 if ($this->cart->hasRecurringProducts() && $transaction_status == 'captured') {
    191                     foreach ($this->cart->getRecurringProducts() as $item) {
    192                         if ($item['recurring']['trial']) {
    193                             $trial_price = $this->tax->calculate($item['recurring']['trial_price'] * $item['quantity'], $item['tax_class_id']);
    194                             $trial_amt = $this->currency->format($trial_price, $this->session->data['currency']);
    195                             $trial_text =  sprintf($this->language->get('text_trial'), $trial_amt, $item['recurring']['trial_cycle'], $item['recurring']['trial_frequency'], $item['recurring']['trial_duration']);
    196 
    197                             $item['recurring']['trial_price'] = $trial_price;
    198                         } else {
    199                             $trial_text = '';
    200                         }
    201 
    202                         $recurring_price = $this->tax->calculate($item['recurring']['price'] * $item['quantity'], $item['tax_class_id']);
    203                         $recurring_amt = $this->currency->format($recurring_price, $this->session->data['currency']);
    204                         $recurring_description = $trial_text . sprintf($this->language->get('text_recurring'), $recurring_amt, $item['recurring']['cycle'], $item['recurring']['frequency']);
    205 
    206                         $item['recurring']['price'] = $recurring_price;
    207 
    208                         if ($item['recurring']['duration'] > 0) {
    209                             $recurring_description .= sprintf($this->language->get('text_length'), $item['recurring']['duration']);
    210                         }
    211 
    212                         if (!$item['recurring']['trial']) {
    213                             // We need to override this value for the proper calculation in updateRecurringExpired
    214                             $item['recurring']['trial_duration'] = 0;
    215                         }
    216 
    217 
    218                         $this->model_extension_payment_squareup->createRecurring($item, $this->session->data['order_id'], $recurring_description, $transaction['id']);
    219                     }
    220                 }
    221 
    222                 $order_status_comment = $this->language->get('squareup_status_comment_' . $transaction_status);
    223 
    224                 $this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $order_status_id, $order_status_comment, true);
    225             }
    226 
    227             $json['redirect'] = $this->url->link('checkout/success', '', true);
    228         } catch (\Squareup\Exception $e) {
    229             if ($e->isCurlError()) {
    230                 $json['error'] = $this->language->get('text_token_issue_customer_error');
    231             } else if ($e->isAccessTokenRevoked()) {
    232                 // Send reminder e-mail to store admin to refresh the token
    233                 $this->model_extension_payment_squareup->tokenRevokedEmail();
    234 
    235                 $json['error'] = $this->language->get('text_token_issue_customer_error');
    236             } else if ($e->isAccessTokenExpired()) {
    237                 // Send reminder e-mail to store admin to refresh the token
    238                 $this->model_extension_payment_squareup->tokenExpiredEmail();
    239 
    240                 $json['error'] = $this->language->get('text_token_issue_customer_error');
    241             } else {
    242                 $json['error'] = $e->getMessage();
    243             }
    244         }
    245 
    246         $this->response->addHeader('Content-Type: application/json');
    247         $this->response->setOutput(json_encode($json));
    248     }
    249 
    250     
    251 }