Get payer action from gateway response and use it for opening the modal

This commit is contained in:
dinamiko 2022-07-15 12:13:38 +02:00
parent d248a278be
commit 859c99aa49
3 changed files with 27 additions and 32 deletions

View file

@ -1,15 +1,18 @@
window.addEventListener('load', function() {
const oxxoButton = document.getElementById('ppcp-oxxo-payer-action');
if(oxxoButton) {
oxxoButton.addEventListener('click', (event) => {
event.preventDefault();
window.open(
oxxoButton.href,
'_blank',
'popup'
);
document.addEventListener(
'DOMContentLoaded',
function() {
jQuery('form.checkout').on('checkout_place_order_success', function(type, data) {
if(data.payer_action && data.payer_action !== '') {
const width = screen.width / 2;
const height = screen.height / 2;
const left = (screen.width / 2) - (width / 2);
const top = (screen.height / 2) - (height / 2);
window.open(
data.payer_action,
'_blank',
'popup, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left
);
}
});
window.open(oxxoButton.href);
}
});
);

View file

@ -73,22 +73,6 @@ class OXXO {
}
);
add_filter(
'woocommerce_thankyou_order_received_text',
function( string $message, WC_Order $order ) {
$payer_action = $order->get_meta( 'ppcp_oxxo_payer_action' ) ?? '';
$button = '';
if ( $payer_action ) {
$button = '<p><a id="ppcp-oxxo-payer-action" class="button" href="' . $payer_action . '" target="_blank">See OXXO Voucher/Ticket</a></p>';
}
return $message . ' ' . $button;
},
10,
2
);
add_action(
'wp_enqueue_scripts',
array( $this, 'register_assets' )
@ -123,7 +107,7 @@ class OXXO {
public function register_assets(): void {
$gateway_settings = get_option( 'woocommerce_ppcp-oxxo-gateway_settings' );
$gateway_enabled = $gateway_settings['enabled'] ?? '';
if ( $gateway_enabled === 'yes' && is_checkout() && ! empty( is_wc_endpoint_url( 'order-received' ) ) ) {
if ( $gateway_enabled === 'yes' && is_checkout() ) {
wp_enqueue_script(
'ppcp-pay-upon-invoice',
trailingslashit( $this->module_url ) . 'assets/js/oxxo.js',

View file

@ -131,6 +131,7 @@ class OXXOGateway extends WC_Payment_Gateway {
$wc_order = wc_get_order( $order_id );
$wc_order->update_status( 'on-hold', __( 'Awaiting OXXO payment.', 'woocommerce-paypal-payments' ) );
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
$payer_action = '';
try {
$shipping_preference = $this->shipping_preference_factory->from_state(
@ -149,7 +150,8 @@ class OXXOGateway extends WC_Payment_Gateway {
$payment_method = $this->order_endpoint->confirm_payment_source( $order->id(), $payment_source );
foreach ( $payment_method->links as $link ) {
if ( $link->rel === 'payer-action' ) {
$wc_order->add_meta_data( 'ppcp_oxxo_payer_action', $link->href );
$payer_action = $link->href;
$wc_order->add_meta_data( 'ppcp_oxxo_payer_action', $payer_action );
$wc_order->save_meta_data();
}
}
@ -184,9 +186,15 @@ class OXXOGateway extends WC_Payment_Gateway {
WC()->cart->empty_cart();
return array(
$result = array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
if ( $payer_action ) {
$result['payer_action'] = $payer_action;
}
return $result;
}
}