shop.balmet.com

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

squareup.php (7570B)


      1 <?php
      2 class ControllerExtensionRecurringSquareup extends Controller {
      3     public function index() {
      4         $this->load->language('extension/recurring/squareup');
      5         
      6         $this->load->model('account/recurring');
      7         $this->load->model('extension/payment/squareup');
      8 
      9         if (isset($this->request->get['order_recurring_id'])) {
     10             $order_recurring_id = $this->request->get['order_recurring_id'];
     11         } else {
     12             $order_recurring_id = 0;
     13         }
     14         
     15         $recurring_info = $this->model_account_recurring->getOrderRecurring($order_recurring_id);
     16         
     17         if ($recurring_info) {
     18             $data['cancel_url'] = html_entity_decode($this->url->link('extension/recurring/squareup/cancel', 'order_recurring_id=' . $order_recurring_id, 'SSL'));
     19 
     20             $data['continue'] = $this->url->link('account/recurring', '', true);    
     21             
     22             if ($recurring_info['status'] == ModelExtensionPaymentSquareup::RECURRING_ACTIVE) {
     23                 $data['order_recurring_id'] = $order_recurring_id;
     24             } else {
     25                 $data['order_recurring_id'] = '';
     26             }
     27 
     28             return $this->load->view('extension/recurring/squareup', $data);
     29         }
     30     }
     31     
     32     public function cancel() {
     33         $this->load->language('extension/recurring/squareup');
     34         
     35         $this->load->model('account/recurring');
     36         $this->load->model('extension/payment/squareup');
     37         
     38         if (isset($this->request->get['order_recurring_id'])) {
     39             $order_recurring_id = $this->request->get['order_recurring_id'];
     40         } else {
     41             $order_recurring_id = 0;
     42         }
     43 
     44         $json = array();
     45         
     46         $recurring_info = $this->model_account_recurring->getOrderRecurring($order_recurring_id);
     47 
     48         if ($recurring_info) {
     49             $this->model_account_recurring->editOrderRecurringStatus($order_recurring_id, ModelExtensionPaymentSquareup::RECURRING_CANCELLED);
     50 
     51             $this->load->model('checkout/order');
     52 
     53             $order_info = $this->model_checkout_order->getOrder($recurring_info['order_id']);
     54 
     55             $this->model_checkout_order->addOrderHistory($recurring_info['order_id'], $order_info['order_status_id'], $this->language->get('text_order_history_cancel'), true);
     56 
     57             $json['success'] = $this->language->get('text_canceled');
     58         } else {
     59             $json['error'] = $this->language->get('error_not_found');
     60         }
     61 
     62         $this->response->addHeader('Content-Type: application/json');
     63         $this->response->setOutput(json_encode($json));
     64     }
     65 
     66     public function recurring() {
     67         $this->load->language('extension/payment/squareup');
     68         
     69         $this->load->model('extension/payment/squareup');
     70 
     71         if (!$this->model_extension_payment_squareup->validateCRON()) {
     72             return;
     73         }
     74 
     75         $this->load->library('squareup');
     76 
     77         $result = array(
     78             'transaction_success' => array(),
     79             'transaction_error' => array(),
     80             'transaction_fail' => array(),
     81             'token_update_error' => ''
     82         );
     83 
     84         $result['token_update_error'] = $this->model_extension_payment_squareup->updateToken();
     85 
     86         $this->load->model('checkout/order');
     87 
     88         foreach ($this->model_extension_payment_squareup->nextRecurringPayments() as $payment) {
     89             try {
     90                 if (!$payment['is_free']) {
     91                     $transaction = $this->squareup->addTransaction($payment['transaction']);
     92 
     93                     $transaction_status = !empty($transaction['tenders'][0]['card_details']['status']) ?
     94                         strtolower($transaction['tenders'][0]['card_details']['status']) : '';
     95 
     96                     $target_currency = $transaction['tenders'][0]['amount_money']['currency'];
     97 
     98                     $amount = $this->squareup->standardDenomination($transaction['tenders'][0]['amount_money']['amount'], $target_currency);
     99 
    100                     $this->model_extension_payment_squareup->addTransaction($transaction, $this->config->get('payment_squareup_merchant_id'), $payment['billing_address'], $payment['order_id'], "CRON JOB", "127.0.0.1");
    101 
    102                     $reference = $transaction['id'];
    103                 } else {
    104                     $amount = 0;
    105                     $target_currency = $this->config->get('config_currency');
    106                     $reference = '';
    107                     $transaction_status = 'captured';
    108                 }
    109 
    110                 $success = $transaction_status == 'captured';
    111 
    112                 $this->model_extension_payment_squareup->addRecurringTransaction($payment['order_recurring_id'], $reference, $amount, $success);
    113 
    114                 $trial_expired = false;
    115                 $recurring_expired = false;
    116                 $profile_suspended = false;
    117 
    118                 if ($success) {
    119                     $trial_expired = $this->model_extension_payment_squareup->updateRecurringTrial($payment['order_recurring_id']);
    120 
    121                     $recurring_expired = $this->model_extension_payment_squareup->updateRecurringExpired($payment['order_recurring_id']);
    122 
    123                     $result['transaction_success'][$payment['order_recurring_id']] = $this->currency->format($amount, $target_currency);
    124                 } else {
    125                     // Transaction was not successful. Suspend the recurring profile.
    126                     $profile_suspended = $this->model_extension_payment_squareup->suspendRecurringProfile($payment['order_recurring_id']);
    127 
    128                     $result['transaction_fail'][$payment['order_recurring_id']] = $this->currency->format($amount, $target_currency);
    129                 }
    130 
    131 
    132                 $order_status_id = $this->config->get('payment_squareup_status_' . $transaction_status);
    133 
    134                 if ($order_status_id) {
    135                     if (!$payment['is_free']) {
    136                         $order_status_comment = $this->language->get('squareup_status_comment_' . $transaction_status);
    137                     } else {
    138                         $order_status_comment = '';
    139                     }
    140 
    141                     if ($profile_suspended) {
    142                         $order_status_comment .= $this->language->get('text_squareup_profile_suspended');
    143                     }
    144 
    145                     if ($trial_expired) {
    146                         $order_status_comment .= $this->language->get('text_squareup_trial_expired');
    147                     }
    148 
    149                     if ($recurring_expired) {
    150                         $order_status_comment .= $this->language->get('text_squareup_recurring_expired');
    151                     }
    152 
    153                     if ($success) {
    154                         $notify = (bool)$this->config->get('payment_squareup_notify_recurring_success');
    155                     } else {
    156                         $notify = (bool)$this->config->get('payment_squareup_notify_recurring_fail');
    157                     }
    158 
    159                     $this->model_checkout_order->addOrderHistory($payment['order_id'], $order_status_id, trim($order_status_comment), $notify);
    160                 }
    161             } catch (\Squareup\Exception $e) {
    162                 $result['transaction_error'][] = '[ID: ' . $payment['order_recurring_id'] . '] - ' . $e->getMessage();
    163             }
    164         };
    165 
    166         if ($this->config->get('payment_squareup_cron_email_status')) {
    167             $this->model_extension_payment_squareup->cronEmail($result);
    168         }
    169     }
    170 }