Skip to content

Commit

Permalink
fix(auth): simplify pending checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe committed Oct 11, 2024
1 parent bfab5b7 commit b769277
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 103 deletions.
22 changes: 17 additions & 5 deletions includes/class-donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class Donations {
public static function init() {
self::$donation_product_name = __( 'Donate', 'newspack-plugin' );

add_action( 'wp_loaded', [ __CLASS__, 'process_donation_form' ], 99 );
// Process donation request.
add_action( 'wp_ajax_modal_checkout_request', [ __CLASS__, 'process_donation_request' ] );
add_action( 'wp_ajax_nopriv_modal_checkout_request', [ __CLASS__, 'process_donation_request' ] );
add_action( 'wp_loaded', [ __CLASS__, 'process_donation_request' ], 99 );

add_action( 'woocommerce_checkout_update_order_meta', [ __CLASS__, 'woocommerce_checkout_update_order_meta' ] );
add_filter( 'woocommerce_billing_fields', [ __CLASS__, 'woocommerce_billing_fields' ] );
add_filter( 'pre_option_woocommerce_enable_guest_checkout', [ __CLASS__, 'disable_guest_checkout' ] );
Expand Down Expand Up @@ -649,8 +653,8 @@ public static function is_platform_other() {
/**
* Handle submission of the donation form.
*/
public static function process_donation_form() {
if ( is_admin() ) {
public static function process_donation_request() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}

Expand Down Expand Up @@ -777,8 +781,16 @@ function ( $item ) {
);

// Redirect to checkout.
\wp_safe_redirect( apply_filters( 'newspack_donation_checkout_url', $checkout_url, $donation_value, $donation_frequency ) );
exit;
$checkout_url = apply_filters( 'newspack_donation_checkout_url', $checkout_url, $donation_value, $donation_frequency );

if ( defined( 'DOING_AJAX' ) ) {
echo wp_json_encode( [ 'url' => $checkout_url ] );
exit;
} else {
// Redirect to checkout.
\wp_safe_redirect( $checkout_url );
exit;
}
}

/**
Expand Down
9 changes: 1 addition & 8 deletions includes/class-magic-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@ final class Magic_Link {
const OTP_AUTH_ACTION = 'np_otp_auth';
const OTP_HASH_COOKIE = 'np_otp_hash';
const ACCEPTED_PARAMS = [
'newspack_modal_checkout',
'type',
'layout',
'frequency',
'amount',
'other',
'product_id',
'variation_id',
'checkout',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/data-events/class-woo-user-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function init() {
public static function checkout_process() {

/**
* On Newspack\Donations::process_donation_form(), we add these values to the cart.
* On Newspack\Donations::process_donation_request(), we add these values to the cart.
*
* Later, we add them to the order (Newspack\Donations::checkout_create_order_line_item()) and use it to send the metadata to Newspack on donation events.
*
Expand Down
22 changes: 11 additions & 11 deletions src/reader-activation-auth/auth-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Internal dependencies.
*/
import { domReady, formatTime } from '../utils';
import { getCheckoutRedirectUrl } from '../reader-activation/checkout';
import { getPendingCheckout } from '../reader-activation/checkout';
import { openNewslettersSignupModal } from '../reader-activation-newsletters/newsletters-modal';

import './google-oauth';
Expand Down Expand Up @@ -219,11 +219,11 @@ window.newspackRAS.push( function ( readerActivation ) {
body.set( 'reader-activation-auth-form', 1 );
body.set( 'npe', emailInput.value );
body.set( 'action', 'link' );
if ( readerActivation.isPendingCheckout() ) {
const redirectUrl = getCheckoutRedirectUrl();
if ( redirectUrl ) {
body.set( 'redirect_url', redirectUrl );
}
const pendingCheckout = getPendingCheckout();
if ( pendingCheckout ) {
const url = new URL( window.location.href );
url.searchParams.set( 'checkout', 1 );
body.set( 'redirect_url', url.toString() );
}
fetch( form.getAttribute( 'action' ) || window.location.pathname, {
method: 'POST',
Expand Down Expand Up @@ -372,11 +372,11 @@ window.newspackRAS.push( function ( readerActivation ) {
if ( ! body.has( 'npe' ) || ! body.get( 'npe' ) ) {
return form.endLoginFlow( newspack_reader_activation_labels.invalid_email, 400 );
}
if ( readerActivation.isPendingCheckout() ) {
const redirectUrl = getCheckoutRedirectUrl();
if ( redirectUrl ) {
body.set( 'redirect_url', redirectUrl );
}
const pendingCheckout = getPendingCheckout();
if ( pendingCheckout ) {
const url = new URL( window.location.href );
url.searchParams.set( 'checkout', 1 );
body.set( 'redirect_url', url.toString() );
}
if ( 'otp' === action ) {
readerActivation
Expand Down
81 changes: 8 additions & 73 deletions src/reader-activation/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,20 @@ import Store from './store.js';
export const store = Store();

/**
* Get the current checkout.
* Set the pending checkout URL.
*
* @return {Object} Checkout data.
* @param {string|false} url
*/
export function getCheckout() {
return store.get( 'checkout' ) || {};
}

/**
* Set the current checkout data.
*
* @param {Object} data Checkout data. Optional.
* If empty or not provided, the checkout data will be cleared.
*/
export function setCheckoutData( data = {} ) {
store.set( 'checkout', data, false );
export function setPendingCheckout( url = false ) {
store.set( 'pending_checkout', url, false );
emit( EVENTS.reader, getReader() );
}

/**
* Get the reader checkout data.
*
* @param {string} key Checkout data key. Optional.
*
* @return {any} Reader checkout data.
*/
export function getCheckoutData( key ) {
const checkout = getCheckout();
if ( ! key ) {
return checkout;
}
return checkout?.[ key ];
}

/**
* Whether checkout is pending.
*
* @return {boolean} Whether checkout is pending.
*/
export function isPendingCheckout() {
const checkout = getCheckout();
if ( Object.keys( checkout ).length ) {
return true;
}
return false;
}

/**
* Reset the reader checkout data.
*/
export function resetCheckoutData() {
setCheckoutData();
}

/**
* Get a checkout redirect URL.
* Get the pending checkout URL.
*
* @return {string} A checkout redirect URL if checkout data is present.
* Otherwise, an empty string
* @return {string|false} Pending checkout URL.
*/
export function getCheckoutRedirectUrl() {
const checkoutType = getCheckoutData( 'type' );
if ( ! checkoutType ) {
return '';
}
const redirectUrl = new URL( window.location.href );
redirectUrl.searchParams.set( 'newspack_modal_checkout', 1 );
redirectUrl.searchParams.set( 'type', checkoutType );
// Add checkout button params.
if ( checkoutType === 'checkout_button' ) {
redirectUrl.searchParams.set( 'product_id', getCheckoutData( 'product_id' ) ?? '' );
redirectUrl.searchParams.set( 'variation_id', getCheckoutData( 'variation_id' ) ?? '' );
}
// Add donate params.
if ( checkoutType === 'donate' ) {
redirectUrl.searchParams.set( 'layout', getCheckoutData( 'layout' ) ?? '' );
redirectUrl.searchParams.set( 'frequency', getCheckoutData( 'frequency' ?? '' ) );
redirectUrl.searchParams.set( 'amount', getCheckoutData( 'amount' ) ?? '' );
redirectUrl.searchParams.set( 'other', getCheckoutData( 'other' ) ?? '' );
}
return redirectUrl.href;
export function getPendingCheckout() {
return store.get( 'pending_checkout' ) || false;
}
8 changes: 3 additions & 5 deletions src/reader-activation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
window.newspack_ras_config = window.newspack_ras_config || {};

import Store from './store.js';
import { isPendingCheckout, setCheckoutData, getCheckoutData, resetCheckoutData } from './checkout.js';
import { getPendingCheckout, setPendingCheckout } from './checkout.js';
import { EVENTS, on, off, emit } from './events.js';
import { getCookie, setCookie, generateID } from './utils.js';
import overlays from './overlays.js';
Expand Down Expand Up @@ -429,10 +429,8 @@ const readerActivation = {
authenticateOTP,
setAuthStrategy,
getAuthStrategy,
setCheckoutData,
getCheckoutData,
isPendingCheckout,
resetCheckoutData,
setPendingCheckout,
getPendingCheckout,
...( newspack_ras_config.is_ras_enabled && { openAuthModal } )
};

Expand Down

0 comments on commit b769277

Please sign in to comment.