Skip to content

Commit

Permalink
fix: duplicate orders (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
perjo927 authored Nov 7, 2022
1 parent be65522 commit 3e88b68
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 78 deletions.
20 changes: 14 additions & 6 deletions assets/js/ledyer-checkout-for-woocommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ jQuery(function ($) {
shippingEmailExists: false,
shippingPhoneExists: false,

redirectUrl: null,

/**
* Triggers on document ready.
*/
documentReady: function () {
lco_wc.log(lco_params);
if (0 < lco_wc.paymentMethodEl.length) {
lco_wc.paymentMethod = lco_wc.paymentMethodEl.filter(':checked').val();
} else {
Expand Down Expand Up @@ -95,7 +96,6 @@ jQuery(function ($) {
success: function (data) { },
error: function (data) { },
complete: function (data) {
lco_wc.log(data.responseJSON);
window.location.href = data.responseJSON.data.redirect;
}
});
Expand All @@ -106,7 +106,6 @@ jQuery(function ($) {
*/
maybeChangeToLco: function () {
if (!lco_wc.preventPaymentMethodChange) {
lco_wc.log($(this).val());

if ('lco' === $(this).val()) {
$('.woocommerce-info').remove();
Expand All @@ -130,7 +129,6 @@ jQuery(function ($) {
success: function (data) { },
error: function (data) { },
complete: function (data) {
lco_wc.log(data.responseJSON);
window.location.href = data.responseJSON.data.redirect;
}
});
Expand Down Expand Up @@ -245,7 +243,6 @@ jQuery(function ($) {
* @param {array} data
*/
setCustomerData: function (data) {
lco_wc.log(data);
if ('billing_address' in data && data.billing_address !== null) {
// Billing fields.
'billing_first_name' in data.billing_address ? $('#billing_first_name').val(data.billing_address.billing_first_name) : '';
Expand Down Expand Up @@ -366,6 +363,7 @@ jQuery(function ($) {
opacity: 0.6
}
});

$.ajax({
type: 'POST',
url: lco_params.submit_order,
Expand All @@ -392,9 +390,11 @@ jQuery(function ($) {
// Ledyer will respond with a new event when order is complete
// So don't redirect just yet
lco_wc.isValidating = false;
lco_wc.redirectUrl = url;
return;
}

lco_wc.redirectUrl.searchParams.append('lco_purchase_complete', 'yes');
window.location.href = url.toString();
} else {
throw 'Result failed';
Expand Down Expand Up @@ -444,6 +444,14 @@ jQuery(function ($) {
$(document).on('ledyerCheckoutOrderComplete', function (event) {
lco_wc.logToFile('ledyerCheckoutOrderComplete from Ledyer triggered');

if (lco_wc.redirectUrl !== null) {
// This means that placeLedyerOrder was called successfully already
// (Due to an earlier call caused by client validation)
lco_wc.redirectUrl.searchParams.append('lco_purchase_complete', 'yes');
window.location.href = lco_wc.redirectUrl.toString();
return;
}

if (!lco_params.pay_for_order) {
lco_wc.placeLedyerOrder(false, lco_wc.isValidating);
}
Expand All @@ -466,7 +474,7 @@ jQuery(function ($) {
shouldProceed: true
})
} else {
lco_wc.placeLedyerOrder(false, true);
lco_wc.placeLedyerOrder(false, lco_wc.isValidating);
}
});
},
Expand Down
3 changes: 3 additions & 0 deletions changelog.readme
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.1.2
* Fix: prevent duplicate orders from being placed

## 1.1.1
* Fix: client side validation bugs

Expand Down
12 changes: 5 additions & 7 deletions classes/class-ledyer-confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,25 @@ public function actions() {
*/
public function confirm_order() {

$ledyer_purchase_complete = filter_input( INPUT_GET, 'lco_purchase_complete', FILTER_SANITIZE_STRING );
$ledyer_confirm = filter_input( INPUT_GET, 'lco_confirm', FILTER_SANITIZE_STRING );
$order_key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_STRING );
$ledyer_pending = filter_input( INPUT_GET, 'lco_pending', FILTER_SANITIZE_STRING );

if ( empty( $ledyer_confirm ) || empty( $order_key ) ) {
if ( empty( $ledyer_confirm ) || empty( $order_key ) || empty( $ledyer_purchase_complete) ) {
return;
}
$order_id = wc_get_order_id_by_order_key( $order_key );
$order = wc_get_order( $order_id );

$order_pending_order = ( 'yes' === $ledyer_pending ) ? true : false;
$should_confirm_order = ( 'yes' === $ledyer_purchase_complete ) ? true : false;

Logger::log( $order_id . ': Confirmation endpoint hit for order.' );

if ( empty( $order->get_date_paid() ) ) {

Logger::log( $order_id . ': Confirm the Ledyer order from the confirmation page.' );
Logger::log( $order_id . ': Confirm the Ledyer order from the confirmation page.' );

if ( $should_confirm_order ) {
// Confirm the order.
wc_ledyer_confirm_ledyer_order( $order_id, $order_pending_order );

lco_unset_sessions();
}
}
Expand Down
8 changes: 0 additions & 8 deletions classes/class-ledyer-lco-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,6 @@ public function add_data_attributes( $tag, $handle ) {
* @param $order
*/
public function wc_order_created( $order_id, $posted_data, $order ) {
$ledyer_order_id = WC()->session->get( 'lco_wc_order_id' );

$ledyer_order = ledyer()->api->update_order_reference( $ledyer_order_id, array( 'reference' => strval( $order_id ) ) );

if ( ! $ledyer_order || ( is_object( $ledyer_order ) && is_wp_error( $ledyer_order ) ) ) {
// If failed then bail.
return;
}
}

/**
Expand Down
10 changes: 6 additions & 4 deletions classes/class-ledyer-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Ledyer_Checkout_For_WooCommerce {
*/
public $checkout;

const VERSION = '1.1.1';
const VERSION = '1.1.2';
const SLUG = 'ledyer-checkout-for-woocommerce';
const SETTINGS = 'ledyer_checkout_for_woocommerce_settings';

Expand Down Expand Up @@ -153,15 +153,17 @@ public function update_order_status( $request ) {
$order = wc_get_order( $order->ID );

if( 'com.ledyer.order.create' === $request_body['eventType'] ) {
// TODO: if order was on hold or pending and notification is received, set to processing (update_status)

$order->payment_complete( $order_id );
$order->update_status('pending');
$response = ledyer()->api->acknowledge_order( $order_id );
if( is_wp_error( $response ) ) {
\Ledyer\Logger::log( 'Couldn\'t acknowledge order ' . $order_id );
}
}

if( 'com.ledyer.order.ready_for_capture' === $request_body['eventType'] ) {
$order->update_status('processing');
}

switch ( $ledyer_order['status'] ) {
case 'fullyCaptured':
$order->update_status( 'completed' );
Expand Down
14 changes: 3 additions & 11 deletions includes/lco-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,14 @@ function wc_ledyer_confirm_ledyer_order( $order_id, $ledyer_pending ) {

do_action( 'ledyer_process_payment', $order_id, $request );

$ledyer_order = ledyer()->api->update_order_reference( $payment_id, array( 'reference' => strval( $order_id ) ) );

update_post_meta( $order_id, 'ledyerpayment_type', $request['paymentMethod']['type'] );
update_post_meta( $order_id, 'ledyer_payment_method', $request['paymentMethod']['provider'] );
update_post_meta( $order_id, '_ledyer_date_paid', gmdate( 'Y-m-d H:i:s' ) );

$isAdvanceInvoice = $request['paymentMethod']['type'] == 'advanceInvoice';

if( ! $ledyer_pending ) {
$order->add_order_note( sprintf( __( 'New payment created in Ledyer with Payment ID %1$s. Payment type - %2$s. Awaiting charge.', 'ledyer-checkout-for-woocommerce' ), $payment_id, $request['paymentMethod']['type'] ) );

if ( ! $isAdvanceInvoice) {
$order->payment_complete( $payment_id );
}
$response = ledyer()->api->acknowledge_order( $payment_id );
if( is_wp_error( $response ) ) {
\Ledyer\Logger::log( 'Couldn\'t acknowledge order ' . $payment_id );
}
$order->add_order_note( sprintf( __( 'New payment created in Ledyer with Payment ID %1$s. Payment type - %2$s. Awaiting capture.', 'ledyer-checkout-for-woocommerce' ), $payment_id, $request['paymentMethod']['type'] ) );
} else {
$order->update_status('on-hold');
$order->add_order_note( sprintf( __( 'New payment created in Ledyer with Payment ID %1$s. Payment type - %2$s. Awaiting signature.', 'ledyer-checkout-for-woocommerce' ), $session_id, $request['paymentMethod']['type'] ) );
Expand Down
2 changes: 1 addition & 1 deletion languages/ledyer-checkout-for-woocommerce.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is distributed under the same license as the Ledyer Checkout for WooCommerce plugin.
msgid ""
msgstr ""
"Project-Id-Version: Ledyer Checkout for WooCommerce 1.1.1\n"
"Project-Id-Version: Ledyer Checkout for WooCommerce 1.1.2\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/ledyer-checkout-for-woocommerce\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
2 changes: 1 addition & 1 deletion ledyer-checkout-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Ledyer Checkout payment gateway for WooCommerce.
* Author: Maksimer/Ledyer
* Author URI: https://www.maksimer.com/
* Version: 1.1.1
* Version: 1.1.2
* Text Domain: ledyer-checkout-for-woocommerce
* Domain Path: /languages
*
Expand Down
Loading

0 comments on commit 3e88b68

Please sign in to comment.