Skip to content

Commit

Permalink
fix: basic order management and notifications handling (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Larsson <[email protected]>
  • Loading branch information
perjo927 and plarsson authored Dec 14, 2022
1 parent 6240968 commit 30de21e
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 190 deletions.
9 changes: 8 additions & 1 deletion Dockerfile-wpdebug-php7
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
FROM wordpress:6.0.2-php7.4-apache

RUN apt-get update && apt-get -qq --force-yes install cron
COPY wpcron.txt /etc/cron.d/wpcron
RUN chmod 0744 /etc/cron.d/wpcron
RUN crontab /etc/cron.d/wpcron

ENV XDEBUG_PORT 8999
ENV XDEBUG_IDEKEY docker

Expand All @@ -9,4 +14,6 @@ RUN pecl install "xdebug" \
RUN echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.client_port=${XDEBUG_PORT}" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.idekey=${XDEBUG_IDEKEY}" >> /usr/local/etc/php/conf.d/xdebug.ini
echo "xdebug.idekey=${XDEBUG_IDEKEY}" >> /usr/local/etc/php/conf.d/xdebug.ini

CMD cron && apache2-foreground
9 changes: 8 additions & 1 deletion Dockerfile-wpdebug-php8
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
FROM wordpress:6.0.2-php8.1-apache

RUN apt-get update && apt-get -qq --force-yes install cron
COPY wpcron.txt /etc/cron.d/wpcron
RUN chmod 0744 /etc/cron.d/wpcron
RUN crontab /etc/cron.d/wpcron

ENV XDEBUG_PORT 8999
ENV XDEBUG_IDEKEY docker

Expand All @@ -9,4 +14,6 @@ RUN pecl install "xdebug" \
RUN echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.client_port=${XDEBUG_PORT}" >> /usr/local/etc/php/conf.d/xdebug.ini && \
echo "xdebug.idekey=${XDEBUG_IDEKEY}" >> /usr/local/etc/php/conf.d/xdebug.ini
echo "xdebug.idekey=${XDEBUG_IDEKEY}" >> /usr/local/etc/php/conf.d/xdebug.ini

CMD cron && apache2-foreground
35 changes: 4 additions & 31 deletions assets/js/ledyer-checkout-for-woocommerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jQuery(function ($) {
// Form fields.
shippingUpdated: false,
blocked: false,
isValidating: false,

preventPaymentMethodChange: false,

Expand All @@ -31,8 +30,6 @@ jQuery(function ($) {
shippingEmailExists: false,
shippingPhoneExists: false,

redirectUrl: null,

/**
* Triggers on document ready.
*/
Expand Down Expand Up @@ -326,8 +323,6 @@ jQuery(function ($) {
}
})

lco_wc.isValidating = false;

const className = lco_params.pay_for_order ? 'div.woocommerce-notices-wrapper' : 'form.checkout';

// Update the checkout and reenable the form.
Expand All @@ -349,10 +344,9 @@ jQuery(function ($) {

/**
* Places the Ledyer order
* @param {string} order_in_sessions
* @param {string} should_validate
*/
placeLedyerOrder: function (order_in_sessions = false, should_validate = false) {
placeLedyerOrder: function (should_validate = false) {
lco_wc.blocked = true;
lco_wc.getLedyerOrder().done(function (response) {
if (response.success) {
Expand All @@ -377,23 +371,14 @@ jQuery(function ($) {
lco_wc.logToFile('Successfully validated order in WooCommerce.');
const url = new URL(data.redirect);

if (order_in_sessions) {
url.searchParams.append('lco_pending', 'yes');
} else {
url.searchParams.append('lco_pending', 'no');
}

if (should_validate) {
window.ledyer.api.clientValidation({
shouldProceed: true
})
// 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 @@ -442,39 +427,27 @@ jQuery(function ($) {

$(document).on('ledyerCheckoutOrderComplete', function (event) {
lco_wc.logToFile('ledyerCheckoutOrderComplete from Ledyer triggered');

if (lco_wc.redirectUrl !== null) {
lco_wc.logToFile('Successfully placed order in Ledyer.');
// 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);
lco_wc.placeLedyerOrder();
}
});

$(document).on('ledyerCheckoutOrderPending', function (event) {
lco_wc.logToFile('ledyerCheckoutOrderPending from Ledyer triggered');
if (!lco_params.pay_for_order) {
lco_wc.placeLedyerOrder(true);
lco_wc.placeLedyerOrder();
}
});

$(document).on('ledyerCheckoutWaitingForClientValidation', function (event) {
lco_wc.logToFile('ledyerCheckoutWaitingForClientValidation from Ledyer triggered');

lco_wc.isValidating = true;

if (lco_params.pay_for_order) {
window.ledyer.api.clientValidation({
shouldProceed: true
})
} else {
lco_wc.placeLedyerOrder(false, lco_wc.isValidating);
lco_wc.placeLedyerOrder(true);
}
});
},
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.2.0
* Fix: Notifications handling and basic order management

## 1.1.3
* Fix: Sanitize response url query parameters (wp review)
* Fix: Escape variables that we print in html markup
Expand Down
6 changes: 5 additions & 1 deletion classes/admin/class-ledyer-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ public function print_standard_content( $ledyer_order ) {
?>
<strong><?php esc_html_e( 'Ledyer Environment: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong><?php echo esc_html( $environment ); ?><br/>
<?php } ?>
<strong><?php esc_html_e( 'Customer Organization number: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong> <?php echo esc_html( $ledyer_order['customer']['companyId'] ); ?><br/>
<strong><?php esc_html_e( 'Company id: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong> <?php echo esc_html( $ledyer_order['customer']['companyId'] ); ?><br/>
<strong><?php esc_html_e( 'Order id: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong> <?php echo esc_html( $ledyer_order['id'] ); ?><br/>
<strong><?php esc_html_e( 'Ledyer id: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong> <?php echo esc_html( $ledyer_order['orderReference'] ); ?><br/>
<strong><?php esc_html_e( 'Payment method: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong> <?php echo esc_html( $ledyer_order['paymentMethod']['type'] ); ?><br/>
<strong><?php esc_html_e( 'Ledyer status: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong> <?php echo esc_html( implode(', ', $ledyer_order['status']) ); ?><br/>


<?php if( ! empty( $ledyer_order['customer']['firstName'] ) || ! empty( $ledyer_order['customer']['lastName'] ) ) : ?>
<strong><?php esc_html_e( 'Order setter: ', 'ledyer-checkout-for-woocommerce' ); ?> </strong> <?php echo esc_html( $ledyer_order['customer']['firstName'] . ' ' . $ledyer_order['customer']['lastName'] ); ?><br/>
<?php endif; ?>
Expand Down
9 changes: 9 additions & 0 deletions classes/class-ledyer-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,13 @@ public function update_order_reference( $order_id, $data ) {
public function acknowledge_order( $order_id ) {
return ( new \Ledyer\Requests\Order\Management\Acknowledge_Order( array( 'orderId' => $order_id, 'data' => array() ) ) )->request();
}

/**
* @param $order_id
*
* @return mixed|\WP_Error
*/
public function get_payment_status( $order_id ) {
return ( new \Ledyer\Requests\Order\Management\Get_Payment_Status( array( 'orderId' => $order_id ) ) )->request();
}
}
22 changes: 7 additions & 15 deletions classes/class-ledyer-confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,22 @@ public function actions() {


/**
* Confirm the order in Woo before redirecting the customer to thank you page.
* Confirm the order in Woo
*/
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 ) || empty( $ledyer_purchase_complete) ) {
if ( empty( $ledyer_confirm ) || empty( $order_key ) ) {
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;
if ( empty( $order_id ) ) {
return;
}

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();
}
wc_ledyer_confirm_ledyer_order( $order_id );
lco_unset_sessions();
}
}
Loading

0 comments on commit 30de21e

Please sign in to comment.