Plugin Directory

Changeset 2703451


Ignore:
Timestamp:
04/01/2022 05:10:57 PM (4 years ago)
Author:
usedrip
Message:

update plugin version to 0.1.1
fix some issues
fix payment values

Location:
drip-payments/trunk
Files:
3 edited

Legend:

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

    r2694390 r2703451  
    44 * Description: Forneça a Drip como opção de pagamento para pedidos do WooCommerce.
    55 * Author: Drip
    6  * Version: 0.1.0
     6 * Version: 0.1.1
    77 */
     8
     9function drip_payments_show_plugin_version() {
     10    return '0.1.1';
     11}
    812
    913add_filter('woocommerce_payment_gateways', 'add_drip_payments_gateway');
     
    2630}
    2731
     32function drip_payments_get_orders_list_per_gateway_date_and_page($gateway, $date, $page) {
     33    $date = wc_string_to_datetime($date);
     34    $start_date = $date->getTimestamp();
     35    $end_date = $date->modify('+1 day')->getTimestamp();
     36
     37    return wc_get_orders(array(
     38        'status'       => array('wc-processing', 'wc-completed'),
     39        'paginate'      => true,
     40        'limit'         => 10,
     41        'paged'         => is_int($page) ? $page : 1,
     42        'payment_method' => $gateway,
     43        'orderby'      => 'date',
     44        'order'        => 'ASC',
     45        'date_created' => "$start_date...$end_date",
     46    ));
     47}
     48
     49function drip_payments_sum_total_value_from_orders($orders) {
     50    $total_orders = [];
     51    foreach($orders as $order) {
     52        $total_itens = [];
     53        foreach ($order->get_items() as $item) {
     54            $item_data = $item->get_data();
     55            $total_itens[] = [
     56                'name' => $item_data['name'],
     57                'quantity' => $item_data['quantity'],
     58                'total' => $item_data['total'],
     59            ];
     60        }
     61
     62        $total_orders[$order->get_id()] = [
     63            'discount' => $order->get_discount_total(),
     64            'total' => $order->get_total(),
     65            'products' => $total_itens
     66        ];
     67    }
     68    return $total_orders;
     69}
     70
     71add_action('rest_api_init', function() {
     72    register_rest_route('drip/v1', '/status', array(
     73      'methods' => 'GET',
     74      'callback' => function (WP_REST_Request $request) {
     75        $date = sanitize_text_field($request->get_param('date'));
     76        if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
     77            return false;
     78        }
     79
     80        $key = sanitize_text_field($request->get_header('X-CNPJ-Key'));
     81        if (strlen($key)  != 14) {
     82            return false;
     83        }
     84
     85        $cnpj = get_option('drip_payments_merchant_cnpj_from_api_key', null);
     86        if ($key != $cnpj) {
     87            return false;
     88        }
     89
     90        $gateways = WC()->payment_gateways->get_available_payment_gateways();
     91        $total_value_from_gateway = [];
     92       
     93        if($gateways) {
     94            foreach($gateways as $gateway) {
     95                if( $gateway->enabled == 'yes' ) {
     96                    $total_orders = drip_payments_get_orders_list_per_gateway_date_and_page($gateway->id, $date, 1);
     97                    $total_value_from_gateway[$gateway->id] = drip_payments_sum_total_value_from_orders($total_orders->orders);
     98                    $total_pages = $total_orders->max_num_pages;
     99                    for ($page = 2; $page <= $total_pages ; $page++) {
     100                        $total_orders = drip_payments_get_orders_list_per_gateway_date_and_page($gateway->id, $date, $page);
     101                        $total_value_from_gateway[$gateway->id] += drip_payments_sum_total_value_from_orders($total_orders->orders);
     102                    }
     103                }
     104            }
     105        }
     106        return $total_value_from_gateway;
     107    }));
     108});
     109
    28110add_action( 'wp_footer', 'drip_payments_add_hidden_version_to_footer', 9999 );
    29  
    30111function drip_payments_add_hidden_version_to_footer() {
    31112   if (is_checkout()) {
    32       echo '<p style="display:none;">drip_version=0.1.0</p>';
     113      echo '<p style="display:none;">drip_version=' . drip_payments_show_plugin_version() . '</p>';
    33114   }
    34115}
     
    62143
    63144            // Instance request object to communicate with server
    64             $this->checkoutRequest = new DripPaymentsCheckoutRequest($this->api_key, $this->testmode);
     145            $this->checkoutRequest = new DripPaymentsCheckoutRequest($this->api_key, $this->testmode, drip_payments_show_plugin_version(), null);
    65146
    66147            $this->enabled = $this->check_is_enabled();
     
    110191                    'offline' => false,
    111192                    'online' => true,
    112                     'expiration' => $now->add(new DateInterval('PT1M'))
     193                    'expiration' => $now->add(new DateInterval('PT5M'))
    113194                ]);
    114195                update_option('drip_payments_server_status', $server_status);
     
    127208            $actual_cashback = json_encode([
    128209                'value' => $this->checkoutRequest->getCashback(),
    129                 'expiration' => $now->add(new DateInterval('PT1M'))
     210                'expiration' => $now->add(new DateInterval('PT5M'))
    130211                ]);
    131212            update_option('drip_payments_actual_cashback', $actual_cashback);
  • drip-payments/trunk/readme.txt

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

    r2694390 r2703451  
    1111    const IS_DISABLED_PATH = self::CHECKOUTS_PATH . '/disabled';
    1212    const SIMULATOR_PATH = 'v1/instalments_simulator';
    13     const MERCHANT_CNPJ = 'v1/merchant_cnpj';
    14 
     13    const MERCHANT_CNPJ = 'v1/merchants/get_cnpj';
     14    const ERROR_LOGGER = 'v1/merchants/log_plugin_error';
    1515
    1616    private static function options($testMode): array
     
    2727    }
    2828
    29     public function __construct($merchantKey, $testMode, GuzzleHttp\Client $client = null)
     29    public function __construct($merchantKey, $testMode, $plugin_version, GuzzleHttp\Client $client = null)
    3030    {
    3131        $this->merchantKey = $merchantKey;
     
    3333            ? new GuzzleHttp\Client(array_merge($client->getConfig(), self::options($testMode)))
    3434            : new GuzzleHttp\Client(self::options($testMode));
     35        $this->plugin_version = $plugin_version;
    3536    }
    3637
     
    4243            return json_decode($response->getBody())->isDisabled == true;
    4344        } catch (RuntimeException $e) {
    44             $now = new DateTime();
    45             $server_status = json_encode([
    46                 'offline' => true,
    47                 'online' => false,
    48                 'expiration' => $now->add(new DateInterval('PT5M'))
    49             ]);
    50             update_option('drip_payments_server_status', $server_status);
     45            $this->deactivatePlugin();
     46            $this->logError($e->getMessage());
    5147            return true;
    5248        }
     49       
    5350    }
    5451
     
    6158            ]);
    6259        } catch (RequestException $e) {
     60            $this->logError($e->getResponse());
    6361            return $e->getResponse();
    6462        } catch (RuntimeException $e) {
     63            $this->logError($e->getMessage());
    6564            return NULL;
    6665        }
     
    7877            return json_decode($response->getBody());
    7978        } catch (RuntimeException $e) {
     79            $this->logError($e->getMessage());
    8080            return false;
    8181        }
     
    9393            return $resp_body['cashbackRate'] * 100;
    9494        } catch (RuntimeException $e) {
    95             $now = new DateTime();
    96             $server_status = json_encode([
    97                 'offline' => true,
    98                 'online' => false,
    99                 'expiration' => $now->add(new DateInterval('PT5M'))
    100             ]);
    101             update_option('drip_payments_server_status', $server_status);
     95            $this->deactivatePlugin();
     96            $this->logError($e->getMessage());
    10297            return '2';
    10398        }
     
    115110            return $resp_body['cnpj'];
    116111        } catch (RuntimeException $e) {
     112            $this->logError($e->getMessage());
     113            return null;
     114        }
     115    }
     116
     117    private function deactivatePlugin() {
     118        $now = new DateTime();
     119        $server_status = json_encode([
     120            'offline' => true,
     121            'online' => false,
     122            'expiration' => $now->add(new DateInterval('PT5M'))
     123        ]);
     124        if(function_exists('update_option')) {
     125            update_option('drip_payments_server_status', $server_status);
     126        }
     127    }
     128
     129    private function logError($error) {
     130        try {
     131            $this->client->post(self::ERROR_LOGGER, [
     132                'json' => [
     133                    'website' => get_bloginfo('wpurl'),
     134                    'ecommerceType' => 'wordpress',
     135                    'pluginVersion' => $this->plugin_version,
     136                    'error' => $error
     137                ],
     138                'headers' => ['X-API-Key' => $this->merchantKey]
     139            ]);
     140            sleep(3);
     141        } catch (RuntimeException $e) {
    117142            return null;
    118143        }
Note: See TracChangeset for help on using the changeset viewer.