Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to customize statuses #875

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions controllers/class-wc-ebanx-api-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
exit;
}

use EBANX\Plugin\Services\WC_EBANX_Constants;
use EBANX\Plugin\Services\WC_EBANX_Helper;
use EBANX\Plugin\Services\WC_EBANX_Api;
use EBANX\Plugin\Services\WC_EBANX_Capture_Payment;
use EBANX\Plugin\Services\WC_EBANX_Constants;
use EBANX\Plugin\Services\WC_EBANX_Helper;

/**
* Class WC_EBANX_Api_Controller
Expand Down Expand Up @@ -122,7 +122,10 @@ public function cancel_order( $order_id, $user_id ) {
);

if ( 'SUCCESS' === $response['status'] ) {
$order->update_status( 'cancelled', __( 'EBANX: Cancelled by customer', 'woocommerce-gateway-ebanx' ) );
$order->update_status(
$this->config->get_mapped_status( 'cancelled' ),
__( 'EBANX: Cancelled by customer', 'woocommerce-gateway-ebanx' )
);
}

wp_redirect( $order->get_view_order_url() );
Expand Down
6 changes: 3 additions & 3 deletions gateways/class-wc-ebanx-credit-card-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use Ebanx\Benjamin\Models\Configs\CreditCardConfig;
use Ebanx\Benjamin\Models\Country;
use EBANX\Plugin\Services\WC_EBANX_Api;
use EBANX\Plugin\Services\WC_EBANX_Capture_Payment;
use EBANX\Plugin\Services\WC_EBANX_Constants;
use EBANX\Plugin\Services\WC_EBANX_Helper;
use EBANX\Plugin\Services\WC_EBANX_Api;
use EBANX\Plugin\Services\WC_EBANX_Payment_Adapter;
use EBANX\Plugin\Services\WC_EBANX_Capture_Payment;

/**
* Class WC_EBANX_Credit_Card_Gateway
Expand Down Expand Up @@ -114,7 +114,7 @@ public function scheduled_subscription_payment( $subscription_id ) {

if ( 'ERROR' == $response['status'] ) {
$order->payment_complete();
$order->update_status( 'failed' );
$order->update_status( $this->get_mapped_status( 'failed' ) );
WC_EBANX::log( $response['status_message'] );
} elseif ( 'SUCCESS' == $response['status'] ) {
switch ( $response['payment']['status'] ) {
Expand Down
17 changes: 17 additions & 0 deletions gateways/class-wc-ebanx-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ public function __construct() {
$this->merchant_currency = strtoupper( get_woocommerce_currency() );
}

/**
* Translate one status to the mapped one.
*
* @param string $status The status that will be mapped.
*
* @return mixed|string
*/
public function get_mapped_status( $status ) {
$mapped_status = sprintf( '%s_status', str_replace( 'wc-', '', $status ) );

if ( isset( $this->configs->settings[ $mapped_status ] ) ) {
$status = $this->configs->settings[ $mapped_status ];
}

return $status;
}

/**
* Check if the method is available to show to the users
*
Expand Down
131 changes: 130 additions & 1 deletion gateways/class-wc-ebanx-global-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
}

use EBANX\Plugin\Services\WC_EBANX_Constants;
use EBANX\Plugin\Services\WC_EBANX_Notice;
use EBANX\Plugin\Services\WC_EBANX_Helper;
use EBANX\Plugin\Services\WC_EBANX_Notice;

/**
* Class WC_EBANX_Global_Gateway
Expand Down Expand Up @@ -151,6 +151,25 @@ public function validate_due_date_days_field() {
return $_POST['woocommerce_ebanx-global_due_date_days'];
}

/**
* Translate one status to the mapped one.
*
* @param $status The status that will be mapped.
*
* @return mixed|string
*/
public function get_mapped_status( $status ) {
$mapped_status = $this->get_option(
sprintf( '%s_status', str_replace( 'wc-', '', $status ) )
);

if ( ! empty( $mapped_status ) ) {
$status = $mapped_status;
}

return $status;
}

/**
* Define the fields on EBANX WooCommerce settings page and set the defaults when the plugin is installed
*
Expand Down Expand Up @@ -193,6 +212,116 @@ public function init_form_fields() {
'type' => 'checkbox',
'desc_tip' => true,
),
'display_statuses_title' => array(
'title' => __( 'Status mapping', 'woocommerce-gateway-ebanx' ),
'type' => 'title',
'description' => __( 'Map the woocommerce statuses.', 'woocommerce-gateway-ebanx' ),
),
'pending_status' => array(
'title' => __( 'Pending payment', 'woocommerce-gateway-ebanx' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'wc-pending',
'options' => array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
)
),
'processing_status' => array(
'title' => __( 'Processing', 'woocommerce-gateway-ebanx' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'wc-processing',
'options' => array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
)
),
'on-hold_status' => array(
'title' => __( 'On hold', 'woocommerce-gateway-ebanx' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'wc-on-hold',
'options' => array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
)
),
'completed_status' => array(
'title' => __( 'Completed', 'woocommerce-gateway-ebanx' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'wc-completed',
'options' => array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
)
),
'cancelled_status' => array(
'title' => __( 'Cancelled', 'woocommerce-gateway-ebanx' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'wc-cancelled',
'options' => array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
)
),
'refunded_status' => array(
'title' => __( 'Refunded', 'woocommerce-gateway-ebanx' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'wc-refunded',
'options' => array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
)
),
'failed_status' => array(
'title' => __( 'Failed', 'woocommerce-gateway-ebanx' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'wc-failed',
'options' => array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
)
),
'display_methods_title' => array(
'title' => __( 'Enable Payment Methods', 'woocommerce-gateway-ebanx' ),
'type' => 'title',
Expand Down
12 changes: 8 additions & 4 deletions gateways/class-wc-ebanx-new-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Ebanx\Benjamin\Models\Configs\CreditCardConfig;
use Ebanx\Benjamin\Models\Country;
use Ebanx\Benjamin\Models\Currency;
use EBANX\Plugin\Services\WC_EBANX_Api;
use EBANX\Plugin\Services\WC_EBANX_Constants;
use EBANX\Plugin\Services\WC_EBANX_Errors;
use EBANX\Plugin\Services\WC_EBANX_Helper;
use EBANX\Plugin\Services\WC_EBANX_Api;
use EBANX\Plugin\Services\WC_EBANX_Payment_Adapter;

/**
Expand Down Expand Up @@ -320,7 +320,11 @@ protected function process_response( $response, $order ) {
if ( $response['payment']['pre_approved'] && 'CO' === $payment_status ) {
$order->payment_complete( $response['payment']['hash'] );
} else {
$order->update_status( $this->get_order_status_from_payment_status( $payment_status ) );
$order->update_status(
$this->get_mapped_status(
$this->get_order_status_from_payment_status( $payment_status )
)
);
}

$order->add_order_note( $this->get_order_note_from_payment_status( $payment_status ) );
Expand Down Expand Up @@ -363,7 +367,7 @@ protected function process_response_error( $response, $order ) {
// translators: placeholders contain bp-dr code and corresponding message.
$error_message = sprintf( __( 'EBANX: An error occurred: %1$s - %2$s', 'woocommerce-gateway-ebanx' ), $code, $status_message );

$order->update_status( 'failed', $error_message );
$order->update_status( $this->get_mapped_status( 'failed' ), $error_message );
$order->add_order_note( $error_message );

do_action( 'ebanx_process_response_error', $order, $code );
Expand Down Expand Up @@ -707,7 +711,7 @@ final public function update_payment( $order, $data ) {
if ( isset( $new_status ) && $new_status !== $old_status ) {
$payment_status = $status[ $data['payment']['status'] ];
$order->add_order_note( sprintf( __( 'EBANX: The payment has been updated to: %s.', 'woocommerce-gateway-ebanx' ), $payment_status ) );
$order->update_status( $new_status );
$order->update_status( $this->get_mapped_status( $new_status ) );
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Plugin/Services/WC_EBANX_Capture_Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ public static function capture_payment( $order_id ) {
WC_EBANX_Flash::add_message( sprintf( __( 'Payment %s was captured successfully.', 'woocommerce-gateway-ebanx' ), $order_id ), 'warning', true );
}
} elseif ( 'CA' === $response['payment']['status'] ) {
$order->update_status( 'failed' );
$order->update_status( $configs->get_mapped_status( 'failed' ) );
$order->add_order_note( __( 'EBANX: Transaction Failed', 'woocommerce-gateway-ebanx' ) );
} elseif ( 'OP' === $response['payment']['status'] ) {
$order->update_status( 'pending' );
$order->update_status( $configs->get_mapped_status( 'pending' ) );
$order->add_order_note( __( 'EBANX: Transaction Pending', 'woocommerce-gateway-ebanx' ) );
}
}
Expand Down