diff --git a/docker-compose.yml b/docker-compose.yml index 1557e55a..493c8e91 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: ports: - 3306:3306 volumes: - - ./.data:/var/lib/mysql + - ./.data/db:/var/lib/mysql woo_plugin: image: ebanx/ebanx-woocommerce @@ -29,6 +29,7 @@ services: ports: - 80:80 volumes: + - /var/www/html - ./woocommerce-gateway-ebanx/:/var/www/html/wp-content/plugins/woocommerce-gateway-ebanx/ links: - woo_mysql:mysql \ No newline at end of file diff --git a/woocommerce-gateway-ebanx/gateways/class-wc-ebanx-gateway.php b/woocommerce-gateway-ebanx/gateways/class-wc-ebanx-gateway.php index 5bee5941..dbd90431 100644 --- a/woocommerce-gateway-ebanx/gateways/class-wc-ebanx-gateway.php +++ b/woocommerce-gateway-ebanx/gateways/class-wc-ebanx-gateway.php @@ -941,49 +941,14 @@ final public function process_hook(array $codes, $notificationType) $order = new WC_Order($order_id); - // TODO: if (empty($order)) {} - // TODO: if ($data->status != "SUCCESS") - switch (strtoupper($notificationType)) { case 'REFUND': - $refunds = current(get_post_meta($order->id, "_ebanx_payment_refunds")); - - foreach ($refunds as $k => $ref) { - foreach ($data->payment->refunds as $refund) { - if ($ref->id == $refund->id) { - if ($refund->status == 'CO' && $refunds[$k]->status != 'CO') { - $order->add_order_note(sprintf(__('EBANX: Your Refund was confirmed to EBANX - Refund ID: %s', 'woocommerce-gateway-ebanx'), $refund->id)); - } - if ($refund->status == 'CA' && $refunds[$k]->status != 'CA') { - $order->add_order_note(sprintf(__('EBANX: Your Refund was canceled to EBANX - Refund ID: %s', 'woocommerce-gateway-ebanx'), $refund->id)); - } - - $refunds[$k]->status = $refund->status; // status == co save note - $refunds[$k]->cancel_date = $refund->cancel_date; - $refunds[$k]->request_date = $refund->request_date; - $refunds[$k]->pending_date = $refund->pending_date; - $refunds[$k]->confirm_date = $refund->confirm_date; - } - } - } + $this->process_refund_hook($order, $data); - update_post_meta($order->id, "_ebanx_payment_refunds", $refunds); break; case 'UPDATE': - switch (strtoupper($data->payment->status)) { - case 'CO': - $order->update_status('processing'); - break; - case 'CA': - $order->update_status('failed'); - break; - case 'PE': - $order->update_status('on-hold'); - break; - case 'OP': - $order->update_status('pending'); - break; - } + $this->update_payment($order, $data); + break; }; @@ -993,7 +958,77 @@ final public function process_hook(array $codes, $notificationType) } /** - * Create the conveter amount on checkout page + * Updates the payment when receive a notification from EBANX + * + * @param WC_Order $order + * @param EBANX_Request $data + * @return void + */ + final public function update_payment($order, $data) { + $requestStatus = strtoupper($data->payment->status); + + $status = array( + 'CO' => 'Confirmed', + 'CA' => 'Canceled', + 'PE' => 'Pending', + 'OP' => 'Opened' + ); + $new_status = null; + switch ($requestStatus) { + case 'CO': + $new_status = 'processing'; + break; + case 'CA': + $new_status = 'failed'; + break; + case 'PE': + $new_status = 'on-hold'; + break; + case 'OP': + $new_status = 'pending'; + break; + } + + if ($new_status !== $order->status) { + $paymentStatus = $status[$data->payment->status]; + $order->add_order_note(sprintf(__('EBANX: The payment has been updated to: %s.', 'woocommerce-gateway-ebanx'), $paymentStatus)); + } + } + + /** + * Updates the refunds when receivers a EBANX refund notification + * + * @param WC_Order $order + * @param EBANX_Request $data + * @return void + */ + final public function process_refund_hook($order, $data) { + $refunds = current(get_post_meta($order->id, "_ebanx_payment_refunds")); + + foreach ($refunds as $k => $ref) { + foreach ($data->payment->refunds as $refund) { + if ($ref->id == $refund->id) { + if ($refund->status == 'CO' && $refunds[$k]->status != 'CO') { + $order->add_order_note(sprintf(__('EBANX: Your Refund was confirmed to EBANX - Refund ID: %s', 'woocommerce-gateway-ebanx'), $refund->id)); + } + if ($refund->status == 'CA' && $refunds[$k]->status != 'CA') { + $order->add_order_note(sprintf(__('EBANX: Your Refund was canceled to EBANX - Refund ID: %s', 'woocommerce-gateway-ebanx'), $refund->id)); + } + + $refunds[$k]->status = $refund->status; // status == co save note + $refunds[$k]->cancel_date = $refund->cancel_date; + $refunds[$k]->request_date = $refund->request_date; + $refunds[$k]->pending_date = $refund->pending_date; + $refunds[$k]->confirm_date = $refund->confirm_date; + } + } + } + + update_post_meta($order->id, "_ebanx_payment_refunds", $refunds); + } + + /** + * Create the converter amount on checkout page * * @param string $currency Possible currencies: BRL, USD, EUR, PEN, CLP, COP, MXN * @return void diff --git a/woocommerce-gateway-ebanx/services/class-wc-ebanx-hooks.php b/woocommerce-gateway-ebanx/services/class-wc-ebanx-hooks.php index 3979aa68..e6602c4f 100644 --- a/woocommerce-gateway-ebanx/services/class-wc-ebanx-hooks.php +++ b/woocommerce-gateway-ebanx/services/class-wc-ebanx-hooks.php @@ -35,9 +35,6 @@ private static function is_url_response() { public static function payment_status_hook_action() { ob_start(); - // $myfile = fopen("/var/www/checkout-woocommerce/test.txt", "a") or die("Unable to open file!"); - // fwrite($myfile, json_encode(array('get' => $_GET, 'post' => $_REQUEST, 'request' => $_REQUEST))); - if ( ( WC_EBANX_Request::has('operation') && WC_EBANX_Request::read('operation') == 'payment_status_change' && WC_EBANX_Request::has('notification_type') @@ -68,8 +65,9 @@ public static function payment_status_hook_action() { if ( self::is_url_response() ) { wp_redirect( $order->get_checkout_order_received_url() ); - exit; } + + exit; } ob_end_clean();