Plugin Directory

Changeset 2684604


Ignore:
Timestamp:
02/24/2022 09:00:21 PM (4 years ago)
Author:
usedrip
Message:

add resilience on checkout when be is offline

Location:
drip-payments/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • drip-payments/trunk/drip-payments.php

    r2682627 r2684604  
    44 * Description: Forneça a Drip como opção de pagamento para pedidos do WooCommerce.
    55 * Author: Drip
    6  * Version: 0.0.18
     6 * Version: 0.0.19
    77 */
    88
     
    6262            $this->enabled = $this->check_is_enabled();
    6363
     64            //$this->cnpj = $this->get_cnpj();
     65
    6466            add_action('woocommerce_update_options_payment_gateways_drip', array($this, 'process_admin_options'));
    6567            add_action('woocommerce_api_resolve_checkout', array($this, 'resolve_checkout'));
     
    6870        public function check_is_enabled()
    6971        {
     72            if(!$this->get_option('enabled')) {
     73                return 'no';
     74            }
     75
     76            $server_status = (array) json_decode(get_option('drip_payments_server_status'));
     77            $now = new DateTime();
     78            $expiration_time = new Datetime($server_status['expiration']->date);
     79
     80            if(count($server_status) > 1 && $server_status['offline'] && $expiration_time > $now) {
     81                return 'no';
     82            }
    7083            if ($this->checkoutRequest->isDisabled()) {
     84                $server_status = json_encode([
     85                    'offline' => true,
     86                    'expiration' => $now->add(new DateInterval('PT5M'))
     87                ]);
     88                update_option('drip_payments_server_status', $server_status);
    7189                return "no";
    7290            }
    73             if ($this->get_option('enabled') && strlen($this->api_key) > 10) {
     91           
     92            if (strlen($this->api_key) > 10) {
    7493                return "yes";
    7594            }
    7695            return "no";
     96        }
     97
     98        public function get_cnpj()
     99        {
     100            if ($this->enabled) {
     101                $cnpj = get_option('drip_payments_merchant_cnpj_from_api_key', null);
     102
     103                if ($cnpj != null) {
     104                    return $cnpj;
     105                }
     106
     107                $new_cnpj = $this->checkoutRequest->getCnpj($this->api_key);
     108                if (is_string($new_cnpj) && strlen($new_cnpj) > 7) {
     109                    update_option('drip_payments_merchant_cnpj_from_api_key', $new_cnpj);
     110                }
     111                return $cnpj;
     112            }
    77113        }
    78114
  • drip-payments/trunk/readme.txt

    r2682612 r2684604  
    33Tags: payments, drip, drip payments, woocommerce, bnpl
    44Requires at least: 4.7
    5 Tested up to: 5.8
     5Tested up to: 5.9.1
    66Requires PHP: 7.0
    7 Stable tag: 0.0.18
     7Stable tag: 0.0.19
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • drip-payments/trunk/src/DripPaymentsCheckoutRequest.php

    r2682612 r2684604  
    1111    const IS_DISABLED_PATH = self::CHECKOUTS_PATH . '/disabled';
    1212    const SIMULATOR_PATH = 'v1/instalments_simulator';
     13    const MERCHANT_CNPJ = 'v1/merchant_cnpj';
    1314
    1415
     
    1920            'base_uri' => $testMode
    2021                ? self::BASE_URI_SANDBOX
    21                 : self::BASE_URI_PRODUCTION
     22                : self::BASE_URI_PRODUCTION,
     23            'connect_timeout' => 5,
     24            'read_timeout' => 5,
     25            'timeout' => 5
    2226        ];
    2327    }
     
    3539        try {
    3640            $response = $this->client->get(self::IS_DISABLED_PATH);
    37 
     41           
    3842            return ($response->getStatusCode() === 200) && json_decode($response->getBody())->isDisabled;
    3943        } catch (RuntimeException $e) {
    40             return false;
     44            return true;
    4145        }
    4246    }
     
    8387        }
    8488    }
     89
     90    public function getCnpj()
     91    {
     92        try {
     93            $response = $this->client->get(self::MERCHANT_CNPJ, ['headers' => ['X-API-Key' => $this->merchantKey]]);
     94            if ($response->getStatusCode() !== 200)
     95                return '2';
     96
     97            $resp_body = (array) json_decode($response->getBody());
     98            return $resp_body['cnpj'];
     99        } catch (RuntimeException $e) {
     100            return null;
     101        }
     102    }
    85103}
Note: See TracChangeset for help on using the changeset viewer.