Changeset 2703451
- Timestamp:
- 04/01/2022 05:10:57 PM (4 years ago)
- Location:
- drip-payments/trunk
- Files:
-
- 3 edited
-
drip-payments.php (modified) (5 diffs)
-
readme.txt (modified) (1 diff)
-
src/DripPaymentsCheckoutRequest.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
drip-payments/trunk/drip-payments.php
r2694390 r2703451 4 4 * Description: Forneça a Drip como opção de pagamento para pedidos do WooCommerce. 5 5 * Author: Drip 6 * Version: 0.1. 06 * Version: 0.1.1 7 7 */ 8 9 function drip_payments_show_plugin_version() { 10 return '0.1.1'; 11 } 8 12 9 13 add_filter('woocommerce_payment_gateways', 'add_drip_payments_gateway'); … … 26 30 } 27 31 32 function 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 49 function 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 71 add_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 28 110 add_action( 'wp_footer', 'drip_payments_add_hidden_version_to_footer', 9999 ); 29 30 111 function drip_payments_add_hidden_version_to_footer() { 31 112 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>'; 33 114 } 34 115 } … … 62 143 63 144 // 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); 65 146 66 147 $this->enabled = $this->check_is_enabled(); … … 110 191 'offline' => false, 111 192 'online' => true, 112 'expiration' => $now->add(new DateInterval('PT 1M'))193 'expiration' => $now->add(new DateInterval('PT5M')) 113 194 ]); 114 195 update_option('drip_payments_server_status', $server_status); … … 127 208 $actual_cashback = json_encode([ 128 209 'value' => $this->checkoutRequest->getCashback(), 129 'expiration' => $now->add(new DateInterval('PT 1M'))210 'expiration' => $now->add(new DateInterval('PT5M')) 130 211 ]); 131 212 update_option('drip_payments_actual_cashback', $actual_cashback); -
drip-payments/trunk/readme.txt
r2684604 r2703451 3 3 Tags: payments, drip, drip payments, woocommerce, bnpl 4 4 Requires at least: 4.7 5 Tested up to: 5.9. 15 Tested up to: 5.9.2 6 6 Requires PHP: 7.0 7 Stable tag: 0. 0.197 Stable tag: 0.1.1 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html -
drip-payments/trunk/src/DripPaymentsCheckoutRequest.php
r2694390 r2703451 11 11 const IS_DISABLED_PATH = self::CHECKOUTS_PATH . '/disabled'; 12 12 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'; 15 15 16 16 private static function options($testMode): array … … 27 27 } 28 28 29 public function __construct($merchantKey, $testMode, GuzzleHttp\Client $client = null)29 public function __construct($merchantKey, $testMode, $plugin_version, GuzzleHttp\Client $client = null) 30 30 { 31 31 $this->merchantKey = $merchantKey; … … 33 33 ? new GuzzleHttp\Client(array_merge($client->getConfig(), self::options($testMode))) 34 34 : new GuzzleHttp\Client(self::options($testMode)); 35 $this->plugin_version = $plugin_version; 35 36 } 36 37 … … 42 43 return json_decode($response->getBody())->isDisabled == true; 43 44 } 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()); 51 47 return true; 52 48 } 49 53 50 } 54 51 … … 61 58 ]); 62 59 } catch (RequestException $e) { 60 $this->logError($e->getResponse()); 63 61 return $e->getResponse(); 64 62 } catch (RuntimeException $e) { 63 $this->logError($e->getMessage()); 65 64 return NULL; 66 65 } … … 78 77 return json_decode($response->getBody()); 79 78 } catch (RuntimeException $e) { 79 $this->logError($e->getMessage()); 80 80 return false; 81 81 } … … 93 93 return $resp_body['cashbackRate'] * 100; 94 94 } 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()); 102 97 return '2'; 103 98 } … … 115 110 return $resp_body['cnpj']; 116 111 } 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) { 117 142 return null; 118 143 }
Note: See TracChangeset
for help on using the changeset viewer.