Skip to content

Commit

Permalink
Merge branch 'master' into autogenerated_codeql_file
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-asher-cko authored Nov 15, 2023
2 parents 48ab7c1 + 1a3658a commit 8195abd
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 46 deletions.
9 changes: 9 additions & 0 deletions assets/css/checkoutcom-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ ul.wc_payment_methods label[for*="payment_method_wc_checkout_com_"] img {
#sepa-iban {
height:35px;
font-size: 17px;
padding-left: 30px;
background-image: url( '../images/bank.svg' );
background-repeat: no-repeat;
background-position: left 5px center;
}

.sepa-continue-btn {
Expand All @@ -728,4 +732,9 @@ ul.wc_payment_methods label[for*="payment_method_wc_checkout_com_"] img {
padding-bottom: 0px;
padding-top: 0px;
font-size: 13px;
}

.sepa-example {
font-size: 14px;
font-weight: 800;
}
10 changes: 10 additions & 0 deletions assets/images/bank.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ jQuery( function ( $ ) {
} );
},

coreSettings: function () {
let enable_fallback_ac = $( '#woocommerce_wc_checkout_com_cards_enable_fallback_ac' );
let fallback_ckocom_sk = $( '#woocommerce_wc_checkout_com_cards_fallback_ckocom_sk' );
let fallback_ckocom_pk = $( '#woocommerce_wc_checkout_com_cards_fallback_ckocom_pk' );

if ( enable_fallback_ac.length <= 0 ) {
return;
}

enable_fallback_ac.on( 'change', function () {
if ( this.checked ) {
fallback_ckocom_sk.closest( 'tr' ).show();
fallback_ckocom_pk.closest( 'tr' ).show();
} else {
fallback_ckocom_sk.closest( 'tr' ).hide();
fallback_ckocom_pk.closest( 'tr' ).hide();
}
} )

enable_fallback_ac.trigger( 'change' );
},

cardSettings: function () {

let ckocom_card_autocap = $( '#ckocom_card_autocap' );
Expand Down Expand Up @@ -301,6 +323,8 @@ jQuery( function ( $ ) {

admin_functions.orderStatusSettings();

admin_functions.coreSettings();

// Script to hide and show fields.
admin_functions.cardSettings();

Expand Down
90 changes: 80 additions & 10 deletions includes/api/class-wc-checkoutcom-api-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ public static function void_payment() {
* @return array|mixed
*/
public static function refund_payment( $order_id, $order ) {
$core_settings = get_option( 'woocommerce_wc_checkout_com_cards_settings' );
$is_fallback_active = ( 'yes' === ( $core_settings['enable_fallback_ac'] ?? 'no' ) );

$cko_payment_id = get_post_meta( $order_id, '_cko_payment_id', true );

// Check if cko_payment_id is empty.
Expand All @@ -846,8 +849,68 @@ public static function refund_payment( $order_id, $order ) {
$checkout = new Checkout_SDK();

try {
// Check if payment is already voided or captured on checkout.com hub.
$details = $checkout->get_builder()->getPaymentsClient()->getPaymentDetails( $cko_payment_id );

try {
// Check if payment is already voided or captured on checkout.com hub.
$details = $checkout->get_builder()->getPaymentsClient()->getPaymentDetails( $cko_payment_id );

} catch ( CheckoutApiException $ex ) {

// Handle above try block exception.
if ( ! $is_fallback_active ) {
$error_message = esc_html__( 'An error has occurred while processing your refund. ', 'checkout-com-unified-payments-api' );

// check if gateway response is enabled from module settings.
if ( $gateway_debug ) {
$error_message .= $ex->getMessage();
}

WC_Checkoutcom_Utility::logger( $error_message, $ex );

return [ 'error' => $error_message ];
}

// Handle Retry with fallback account.
$checkout = new Checkout_SDK( true );
$details = $checkout->get_builder()->getPaymentsClient()->getPaymentDetails( $cko_payment_id );

if ( 'Refunded' === $details['status'] && ! $refund_is_less ) {
$error_message = 'Payment has already been refunded on Checkout.com hub for order Id : ' . $order_id;

return [ 'error' => $error_message ];
}

$refund_request = new RefundRequest();
$refund_request->reference = $order->get_order_number();

// Process partial refund if amount is less than order amount.
if ( $refund_is_less ) {
$refund_request->amount = $refund_amount_cents;

$_SESSION['cko-refund-is-less'] = $refund_is_less;
}

$order->add_order_note( esc_html__( 'Checkout.com Refund : Process via fallback account.', 'checkout-com-unified-payments-api' ) );

$response = $checkout->get_builder()->getPaymentsClient()->refundPayment( $cko_payment_id, $refund_request );

if ( ! WC_Checkoutcom_Utility::is_successful( $response ) ) {
/* translators: 1: Order ID. */
$error_message = sprintf( esc_html__( 'An error has occurred while processing your refund payment on Checkout.com hub. Order Id : %s', 'checkout-com-unified-payments-api' ), $order_id );

// Check if gateway response is enabled from module settings.
if ( $gateway_debug ) {
$error_message .= $response;
}

WC_Checkoutcom_Utility::logger( $error_message, $response );

return [ 'error' => $error_message ];
} else {
return $response;
}

}

if ( 'Refunded' === $details['status'] && ! $refund_is_less ) {
$error_message = 'Payment has already been refunded on Checkout.com hub for order Id : ' . $order_id;
Expand All @@ -862,7 +925,6 @@ public static function refund_payment( $order_id, $order ) {
if ( $refund_is_less ) {
$refund_request->amount = $refund_amount_cents;

// Set is_mada in session.
$_SESSION['cko-refund-is-less'] = $refund_is_less;
}

Expand Down Expand Up @@ -1406,22 +1468,30 @@ public static function mandate_cancel_request( $url, $subscription_id ) {
return false;
}

$core_settings = get_option( 'woocommerce_wc_checkout_com_cards_settings' );
$core_settings = get_option( 'woocommerce_wc_checkout_com_cards_settings' );
$is_fallback_active = ( 'yes' === ( $core_settings['enable_fallback_ac'] ?? 'no' ) );

$core_settings['ckocom_sk'] = cko_is_nas_account() ? 'Bearer ' . $core_settings['ckocom_sk'] : $core_settings['ckocom_sk'];

$wp_request_headers = [
'Authorization' => $core_settings['ckocom_sk'],
];

$wp_response = wp_remote_post(
$url,
[
'headers' => $wp_request_headers,
'headers' => [ 'Authorization' => $core_settings['ckocom_sk'] ],
]
);

if ( 200 !== wp_remote_retrieve_response_code( $wp_response ) ) {
// If unauthorized & fallback ABC setup retry with those cred.
if ( 401 === wp_remote_retrieve_response_code( $wp_response ) && $is_fallback_active ) {

$wp_response = wp_remote_post(
$url,
[
'headers' => [ 'Authorization' => $core_settings['fallback_ckocom_sk'] ],
]
);

} elseif ( 200 !== wp_remote_retrieve_response_code( $wp_response ) ) {

WC_Checkoutcom_Utility::logger(
sprintf(
'An error has occurred while mandate cancel Order # %d request. Response code: %d',
Expand Down
6 changes: 3 additions & 3 deletions includes/api/class-wc-checkoutcom-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function value_to_decimal( $amount, $currency_symbol ) {
*
* @return float|int
*/
public function decimal_to_value( $amount, $currency_symbol ) {
public static function decimal_to_value( $amount, $currency_symbol ) {
$currency = strtoupper( $currency_symbol );
$three_decimal_currency_list = [ 'BHD', 'LYD', 'JOD', 'IQD', 'KWD', 'OMR', 'TND' ];
$zero_decimal_currency_list = [
Expand Down Expand Up @@ -117,13 +117,13 @@ public static function get_delayed_capture_timestamp() {
// If the input of the delay is numeric.
if ( is_numeric( $delay ) ) {
// Get total seconds based on the hour input.
$total_seconds = $delay * 3600;
$total_seconds = round( $delay * 3600 );
// If the delay is 0 manually add a 10 seconds delay.
if ( 0 === $total_seconds ) {
$total_seconds += $default_seconds_delay;
}
$hours = floor( $total_seconds / 3600 );
$minutes = floor( $total_seconds / 60 % 60 );
$minutes = floor( floor( $total_seconds / 60 ) % 60 );
$seconds = floor( $total_seconds % 60 );

// Return date and time in UTC with the delays added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public function payment_fields() {
// check if apm is selected as payment method.
if (jQuery('#payment_method_wc_checkout_com_alternative_payments_sepa').is(':checked')) {

if (0 === jQuery('#sepa-iban').val().length) {
const iban = jQuery('#sepa-iban').val();

if (0 === iban.length) {
alert( '<?php esc_html_e( 'Please enter your bank accounts iban', 'checkout-com-unified-payments-api' ); ?>' );
return false;
}
Expand Down
19 changes: 18 additions & 1 deletion includes/settings/class-wc-checkoutcom-cards-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function core_settings() {
'default' => 'Pay by Card with Checkout.com',
],
'ckocom_account_type' => [
'title' => __( 'Account type', 'checkout-com-unified-payments-api' ),
'title' => __( 'Account Type', 'checkout-com-unified-payments-api' ),
'type' => 'select',
'description' => __( 'Contact support team to know your account type.', 'checkout-com-unified-payments-api' ),
'desc_tip' => true,
Expand All @@ -143,6 +143,23 @@ public static function core_settings() {
'description' => sprintf( __( 'You can %1$s find your public key %2$s in the Checkout.com Hub', 'checkout-com-unified-payments-api' ), '<a class="checkoutcom-key-docs" target="_blank" href="' . esc_url( $docs_link ) . '">', '</a>' ),
'placeholder' => 'pk_xxx',
],
'enable_fallback_ac' => [
'id' => 'enable',
'title' => __( 'Fallback Account', 'checkout-com-unified-payments-api' ),
'type' => 'checkbox',
'label' => __( 'Enable Fallback Account(ABC account) for Refund', 'checkout-com-unified-payments-api' ),
'default' => 'no',
],
'fallback_ckocom_sk' => [
'title' => __( 'Secret Key', 'checkout-com-unified-payments-api' ),
'type' => 'text',
'placeholder' => 'sk_xxx',
],
'fallback_ckocom_pk' => [
'title' => __( 'Public Key', 'checkout-com-unified-payments-api' ),
'type' => 'text',
'placeholder' => 'pk_xxx',
],
];

return apply_filters( 'wc_checkout_com_cards', $settings );
Expand Down
Loading

0 comments on commit 8195abd

Please sign in to comment.