Skip to content

Commit

Permalink
Merge pull request #413 from krokedil/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MichaelBengtsson authored Aug 25, 2021
2 parents 91f5637 + 98bd1bb commit 807eadb
Show file tree
Hide file tree
Showing 78 changed files with 10,703 additions and 4,598 deletions.
32 changes: 23 additions & 9 deletions .github/workflows/e2e_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ on:
branches:
- develop
jobs:
build:
name: Run tests
runs-on: ubuntu-latest
test-plugin:
name: Run E2E Tests
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
env:
API_KEY: ${{secrets.E2E_API_KEY}}
API_SECRET: ${{secrets.E2E_API_SECRET}}
steps:
- uses: convictional/trigger-workflow-and-wait@v1.3.0
- name: Create dirs.
run: |
mkdir -p kco
- name: Setup KCO
uses: actions/checkout@v2
with:
owner: krokedil
repo: e2e-tests
github_token: ${{ secrets.GH_ACCESS_TOKEN }}
workflow_file_name: e2e-tests.yml
ref: master
path: kco
- name: Setup Yarn
run: |
cd ./kco/tests/e2e/
yarn install
- name: Run Tests
run: |
cd ./kco/tests/e2e/
yarn e2e:test
44 changes: 19 additions & 25 deletions assets/js/klarna-checkout-for-woocommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ jQuery( function( $ ) {
$( '#ship-to-different-address-checkbox' ).prop( 'checked', true);
}

kco_wc.moveExtraCheckoutFields();
kco_wc.updateShipping( false );
if( ! kco_params.pay_for_order ) {
kco_wc.moveExtraCheckoutFields();
kco_wc.updateShipping( false );
}
},

/**
Expand Down Expand Up @@ -241,6 +243,7 @@ jQuery( function( $ ) {
opacity: 0.6
}
});

var ajax = $.ajax({
type: 'POST',
url: kco_params.get_klarna_order_url,
Expand Down Expand Up @@ -337,6 +340,7 @@ jQuery( function( $ ) {
failOrder: function( event, error_message, callback ) {
callback({ should_proceed: false });
kco_wc.blocked = false;
var className = kco_params.pay_for_order ? 'div.woocommerce-notices-wrapper' : 'form.checkout';
// Renable the form.
$( 'body' ).trigger( 'updated_checkout' );
$( kco_wc.checkoutFormSelector ).removeClass( 'processing' );
Expand All @@ -345,12 +349,12 @@ jQuery( function( $ ) {

// Print error messages, and trigger checkout_error, and scroll to notices.
$( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ).remove();
$( 'form.checkout' ).prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>' ); // eslint-disable-line max-len
$( 'form.checkout' ).removeClass( 'processing' ).unblock();
$( 'form.checkout' ).find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();
$( className ).prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>' ); // eslint-disable-line max-len
$( className ).removeClass( 'processing' ).unblock();
$( className ).find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();
$( document.body ).trigger( 'checkout_error' , [ error_message ] );
$( 'html, body' ).animate( {
scrollTop: ( $( 'form.checkout' ).offset().top - 100 )
scrollTop: ( $( className ).offset().top - 100 )
}, 1000 );
},

Expand Down Expand Up @@ -393,10 +397,10 @@ jQuery( function( $ ) {
return Object.keys(kco_params.countries).find(key => kco_params.countries[key] === country);
},

placeKlarnaOrder: function(callback) {
placeKlarnaOrder: function(callback) {
kco_wc.blocked = true;
kco_wc.getKlarnaOrder().done( function(response) {
if(response.success) {
if(response.success ) {
$( '.woocommerce-checkout-review-order-table' ).block({
message: null,
overlayCSS: {
Expand All @@ -418,27 +422,13 @@ jQuery( function( $ ) {
throw 'Result failed';
}
} catch ( err ) {

if ( data.messages ) {
var message = data.messages;
kco_wc.logToFile( 'Checkout error | ' + data.messages );
kco_wc.failOrder( 'submission', data.messages, callback );
} else {
var message = '<div class="woocommerce-error">Checkout error</div>';
kco_wc.logToFile( 'Checkout error | No message' );
kco_wc.failOrder( 'submission', '<div class="woocommerce-error">Checkout error</div>', callback );
}

// Reload page.
if ( true === data.reload ) {
window.location.reload();
return;
}

// Trigger update in case we need a fresh nonce.
if ( true === data.refresh ) {
$( 'body' ).trigger( 'update_checkout' );
}

kco_wc.failOrder( 'submission', message, callback );
}
},
error: function( data ) {
Expand Down Expand Up @@ -516,7 +506,11 @@ jQuery( function( $ ) {
},
'validation_callback': function( data, callback ) {
kco_wc.logToFile( 'validation_callback from Klarna triggered' );
kco_wc.placeKlarnaOrder(callback);
if( kco_params.pay_for_order ) {
callback({ should_proceed: true });
} else {
kco_wc.placeKlarnaOrder(callback);
}
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion assets/js/klarna-checkout-for-woocommerce.min.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions classes/class-kco-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class KCO_API {
/**
* Creates a Klarna Checkout order.
*
* @param int $order_id The WooCommerce order id.
* @return mixed
*/
public function create_klarna_order() {
public function create_klarna_order( $order_id = false ) {
$request = new KCO_Request_Create();
$response = $request->request();
$response = $request->request( $order_id );

return $this->check_for_api_error( $response );
}
Expand Down
2 changes: 1 addition & 1 deletion classes/class-kco-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function update_klarna_order() {

$klarna_order = KCO_WC()->api->get_klarna_order( $klarna_order_id );

if ( 'checkout_incomplete' === $klarna_order['status'] ) {
if ( $klarna_order && 'checkout_incomplete' === $klarna_order['status'] ) {
// If it is, update order.
$klarna_order = KCO_WC()->api->update_klarna_order( $klarna_order_id );
}
Expand Down
9 changes: 7 additions & 2 deletions classes/class-kco-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ public function enqueue_scripts() {
return;
}

$pay_for_order = false;
if ( is_wc_endpoint_url( 'order-pay' ) ) {
return;
$pay_for_order = true;
}

if ( ! kco_wc_prefill_allowed() ) {
Expand Down Expand Up @@ -283,6 +284,7 @@ public function enqueue_scripts() {
'timeout_message' => __( 'Please try again, something went wrong with processing your order.', 'klarna-checkout-for-woocommerce' ),
'timeout_time' => apply_filters( 'kco_checkout_timeout_duration', 20 ),
'countries' => kco_get_country_codes(),
'pay_for_order' => $pay_for_order,
);

if ( version_compare( WC_VERSION, '3.9', '>=' ) ) {
Expand All @@ -291,7 +293,10 @@ public function enqueue_scripts() {
wp_localize_script( 'kco', 'kco_params', $checkout_localize_params );

wp_enqueue_script( 'kco' );
wp_enqueue_style( 'kco' );

if ( ! $pay_for_order ) {
wp_enqueue_style( 'kco' );
}
}


Expand Down
23 changes: 23 additions & 0 deletions classes/class-kco-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ class KCO_Logger {
*/
public static function log( $data ) {
$settings = get_option( 'woocommerce_kco_settings' );

if ( 'yes' === $settings['logging'] ) {
$message = self::format_data( $data );
if ( empty( self::$log ) ) {
self::$log = new WC_Logger();
}
self::$log->add( 'klarna-checkout-for-woocommerce', wp_json_encode( $message ) );
}

if ( isset( $data['response']['code'] ) && ( $data['response']['code'] < 200 || $data['response']['code'] > 299 ) ) {
self::log_to_db( $data );
}
}

/**
Expand Down Expand Up @@ -112,4 +117,22 @@ public static function get_stack() {
return $stack;
}

/**
* Logs an event in the WP DB.
*
* @param array $data The data to be logged.
*/
public static function log_to_db( $data ) {
$logs = get_option( 'krokedil_debuglog_kco', array() );

if ( ! empty( $logs ) ) {
$logs = json_decode( $logs );
}

$logs = array_slice( $logs, -14 );
$logs[] = $data;
$logs = wp_json_encode( $logs );
update_option( 'krokedil_debuglog_kco', $logs );
}

}
3 changes: 2 additions & 1 deletion classes/class-kco-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct() {
* @return void
*/
public function add_status_page_box() {
include_once KCO_WC_PLUGIN_PATH . '/includes/admin/views/status-report.php';
}
}
$wc_collector_checkout_status = new KCO_Status();
$kco_status = new KCO_Status();
4 changes: 2 additions & 2 deletions classes/class-kco-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function set_recurring( $request_args ) {
* @param array $klarna_order The Klarna order.
* @return void
*/
public function set_recurring_token_for_order( $order_id = null, $klarna_order ) {
public function set_recurring_token_for_order( $order_id = null, $klarna_order = null ) {
$wc_order = wc_get_order( $order_id );
if ( class_exists( 'WC_Subscription' ) && ( wcs_order_contains_subscription( $wc_order, array( 'parent', 'renewal', 'resubscribe', 'switch' ) ) || wcs_is_subscription( $wc_order ) ) ) {
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
Expand Down Expand Up @@ -235,7 +235,7 @@ public function set_recurring_token_for_order( $order_id = null, $klarna_order )
* @param array $klarna_order The Klarna order.
* @return void
*/
public function set_recurring_token_for_subscription( $subscription_id = null, $klarna_order ) {
public function set_recurring_token_for_subscription( $subscription_id = null, $klarna_order = null ) {
if ( isset( $klarna_order['recurring_token'] ) ) {
$recurring_token = $klarna_order['recurring_token'];
update_post_meta( $subscription_id, '_kco_recurring_token', $recurring_token );
Expand Down
34 changes: 33 additions & 1 deletion classes/class-kco-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function override_template( $template, $template_name ) {
if ( is_checkout() ) {
$confirm = filter_input( INPUT_GET, 'confirm', FILTER_SANITIZE_STRING );
// Don't display KCO template if we have a cart that doesn't needs payment.
if ( apply_filters( 'kco_check_if_needs_payment', true ) ) {
if ( apply_filters( 'kco_check_if_needs_payment', true ) && ! is_wc_endpoint_url( 'order-pay' ) ) {
if ( ! WC()->cart->needs_payment() ) {
return $template;
}
Expand Down Expand Up @@ -114,6 +114,38 @@ public function override_template( $template, $template_name ) {
}
}
}

// Klarna Checkout Pay for order.
if ( 'checkout/form-pay.php' === $template_name ) {
global $wp;
$order_id = $wp->query_vars['order-pay'];
$order = wc_get_order( $order_id );
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
if ( array_key_exists( 'kco', $available_gateways ) ) {
if ( locate_template( 'woocommerce/klarna-checkout-pay.php' ) ) {
$klarna_checkout_template = locate_template( 'woocommerce/paysoncheckout-pay.php' );
} else {
$klarna_checkout_template = KCO_WC_PLUGIN_PATH . '/templates/klarna-checkout-pay.php';
}

if ( 'kco' === $order->get_payment_method() ) {
$confirm = filter_input( INPUT_GET, 'confirm', FILTER_SANITIZE_STRING );
if ( empty( $confirm ) ) {
$template = $klarna_checkout_template;
}
}

// If chosen payment method does not exist and PCO is the first gateway.
if ( empty( $order->get_payment_method() ) ) {
reset( $available_gateways );
if ( 'kco' === key( $available_gateways ) ) {
if ( empty( $confirm ) ) {
$template = $klarna_checkout_template;
}
}
}
}
}
}

return $template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class KCO_Request_Create_Recurring extends KCO_Request {
* @param string $recurring_token The Klarna recurring token.
* @return array
*/
public function request( $order_id = null, $recurring_token ) {
public function request( $order_id = null, $recurring_token = null ) {
$request_url = $this->get_api_url_base() . 'customer-token/v1/tokens/' . $recurring_token . '/order';
$request_args = apply_filters( 'kco_wc_create_recurring_order', $this->get_request_args( $order_id ) );
$response = wp_remote_request( $request_url, $request_args );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function get_body( $order_id ) {
$request_body = array(
'purchase_country' => $this->get_purchase_country(),
'locale' => substr( str_replace( '_', '-', get_locale() ), 0, 5 ),
'merchant_urls' => KCO_WC()->merchant_urls->get_urls(),
'merchant_urls' => KCO_WC()->merchant_urls->get_urls( $order_id ),
'billing_countries' => KCO_Request_Countries::get_billing_countries(),
'shipping_countries' => KCO_Request_Countries::get_shipping_countries(),
'merchant_data' => KCO_Request_Merchant_Data::get_merchant_data(),
Expand Down
69 changes: 69 additions & 0 deletions includes/admin/views/status-report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Admin View: Page - Status Report.
*
* @package KCO\Includes\Admin\Views
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

?>
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>
<th colspan="6" data-export-label="Klarna Checkout Request Log">
<h2><?php esc_html_e( 'Klarna Checkout', 'klarna-checkout-for-woocommerce' ); ?><?php echo wc_help_tip( esc_html__( 'Klarna Checkout System Status.', 'klarna-checkout-for-woocommerce' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></h2>
</th>
</tr>

<?php
$db_logs = get_option( 'krokedil_debuglog_kco', array() );
if ( ! empty( $db_logs ) ) {
$db_logs = array_reverse( json_decode( $db_logs, true ) );
?>
<tr>
<td ><strong><?php esc_html_e( 'Time', 'klarna-checkout-for-woocommerce' ); ?></strong></td>
<td class="help"></td>
<td ><strong><?php esc_html_e( 'Request', 'klarna-checkout-for-woocommerce' ); ?></strong></td>
<td ><strong><?php esc_html_e( 'Response Code', 'klarna-checkout-for-woocommerce' ); ?></strong></td>
<td ><strong><?php esc_html_e( 'Response Message', 'klarna-checkout-for-woocommerce' ); ?></strong></td>
<td ><strong><?php esc_html_e( 'Correlation ID', 'klarna-checkout-for-woocommerce' ); ?></strong></td>
</tr>
</thead>
<tbody>
<?php
foreach ( $db_logs as $log ) {

$timestamp = isset( $log['timestamp'] ) ? $log['timestamp'] : '';
$log_title = isset( $log['title'] ) ? $log['title'] : '';
$code = isset( $log['response']['code'] ) ? $log['response']['code'] : '';
$body = isset( $log['response']['body'] ) ? wp_json_encode( $log['response']['body'] ) : '';
$error_code = isset( $log['response']['body']['error_code'] ) ? 'Error code: ' . $log['response']['body']['error_code'] . '.' : '';
$error_messages = isset( $log['response']['body']['error_messages'] ) ? 'Error messages: ' . wp_json_encode( $log['response']['body']['error_messages'] ) : '';
$correlation_id = isset( $log['response']['body']['correlation_id'] ) ? $log['response']['body']['correlation_id'] : '';

?>
<tr>
<td><?php echo esc_html( $timestamp ); ?></td>
<td class="help"></td>
<td><?php echo esc_html( $log_title ); ?><span style="display: none;">, Response code: <?php echo esc_html( $code ); ?>, Response message: <?php echo esc_html( $body ); ?>, Correlation ID: <?php echo esc_html( $correlation_id ); ?></span</td>
<td><?php echo esc_html( $code ); ?></td>
<td><?php echo esc_html( $error_code ) . ' ' . esc_html( $error_messages ); ?></td>
<td><?php echo esc_html( $correlation_id ); ?></td>
</tr>
<?php
}
} else {
?>
</thead>
<tbody>
<tr>
<td colspan="6" data-export-label="No KCO errors"><?php esc_html_e( 'No error logs', 'klarna-checkout-for-woocommerce' ); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Loading

0 comments on commit 807eadb

Please sign in to comment.