Skip to content

Commit

Permalink
Merge pull request #509 from ebanx/bugfix/notification
Browse files Browse the repository at this point in the history
Notification conflicts
  • Loading branch information
Cezar Luiz committed May 26, 2017
2 parents a949145 + 93abd81 commit e9740d3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
3 changes: 2 additions & 1 deletion docker-compose.yml
Expand Up @@ -9,7 +9,7 @@ services:
ports:
- 3306:3306
volumes:
- ./.data:/var/lib/mysql
- ./.data/db:/var/lib/mysql

woo_plugin:
image: ebanx/ebanx-woocommerce
Expand All @@ -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
113 changes: 74 additions & 39 deletions woocommerce-gateway-ebanx/gateways/class-wc-ebanx-gateway.php
Expand Up @@ -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;
};

Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions woocommerce-gateway-ebanx/services/class-wc-ebanx-hooks.php
Expand Up @@ -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')
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e9740d3

Please sign in to comment.