-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix stripe checkout URL being cached
Fixed instances where the checkout session URL was being cached by generating it on the fly.
- Loading branch information
1 parent
f488fbe
commit 410ce17
Showing
1 changed file
with
97 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,92 @@ | |
* Class Stripe_Checkout_Block | ||
*/ | ||
class Stripe_Checkout_Block { | ||
/** | ||
* Stripe API instance. | ||
* | ||
* @var Stripe_API | ||
*/ | ||
private $stripe_api; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @access public | ||
* @since 3.0.0 | ||
*/ | ||
public function __construct() { | ||
$this->stripe_api = new Stripe_API(); | ||
|
||
add_filter( 'allowed_redirect_hosts', array( $this, 'add_allowed_redirect_hosts' ) ); | ||
add_action( 'wp_loaded', array( $this, 'watch_checkout' ) ); | ||
} | ||
|
||
/** | ||
* Add allowed redirect hosts. | ||
* | ||
* @param array $hosts Allowed hosts. | ||
* @return array | ||
*/ | ||
public function add_allowed_redirect_hosts( $hosts ) { | ||
$hosts[] = 'checkout.stripe.com'; | ||
return $hosts; | ||
} | ||
|
||
/** | ||
* Watch for the checkout. | ||
*/ | ||
public function watch_checkout() { | ||
if ( ! isset( $_GET['action'] ) || 'buy_stripe' !== $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
return; | ||
} | ||
|
||
$product_id = isset( $_GET['product_id'] ) ? sanitize_text_field( wp_unslash( $_GET['product_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
$price_id = isset( $_GET['price_id'] ) ? sanitize_text_field( wp_unslash( $_GET['price_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
$url = isset( $_GET['url'] ) ? sanitize_text_field( wp_unslash( $_GET['url'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
$mode = isset( $_GET['mode'] ) ? sanitize_text_field( wp_unslash( $_GET['mode'] ) ) : 'payment'; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
|
||
if ( empty( $product_id ) || empty( $price_id ) || empty( $url ) ) { | ||
return sprintf( | ||
'<div %1$s><div class="o-stripe-checkout">%2$s</div></div>', | ||
get_block_wrapper_attributes(), | ||
__( 'An error occurred! Could not retrieve the product information!', 'otter-blocks' ) | ||
); | ||
} | ||
|
||
$permalink = add_query_arg( | ||
array( | ||
'stripe_session_id' => '{CHECKOUT_SESSION_ID}', | ||
'product_id' => $product_id, | ||
), | ||
$url | ||
); | ||
|
||
$session = $this->stripe_api->create_request( | ||
'create_session', | ||
array( | ||
'success_url' => $permalink, | ||
'cancel_url' => $permalink, | ||
'line_items' => array( | ||
array( | ||
'price' => $price_id, | ||
'quantity' => 1, | ||
), | ||
), | ||
'mode' => $mode, | ||
) | ||
); | ||
|
||
if ( is_wp_error( $session ) ) { | ||
return sprintf( | ||
'<div %1$s><div class="o-stripe-checkout">%2$s</div></div>', | ||
get_block_wrapper_attributes(), | ||
__( 'An error occurred! Could not create the request!', 'otter-blocks' ) . $this->format_error( $session ) | ||
); | ||
} | ||
|
||
wp_safe_redirect( esc_url( $session->url ) ); | ||
exit; | ||
} | ||
|
||
/** | ||
* Block render function for server-side. | ||
|
@@ -30,17 +116,15 @@ public function render( $attributes ) { | |
return ''; | ||
} | ||
|
||
$stripe = new Stripe_API(); | ||
|
||
if ( isset( $_GET['stripe_session_id'] ) ) {// phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
$session_id = esc_attr( $_GET['stripe_session_id'] );// phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | ||
$status = $stripe->get_status_for_price_id( $session_id, esc_attr( $attributes['price'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
$status = $this->stripe_api->get_status_for_price_id( $session_id, esc_attr( $attributes['price'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | ||
|
||
if ( false !== $status ) { | ||
if ( 'success' === $status ) { | ||
$message = isset( $attributes['successMessage'] ) ? wp_kses_post( $attributes['successMessage'] ) : __( 'Your payment was successful. If you have any questions, please email [email protected].', 'otter-blocks' ); | ||
|
||
do_action( 'otter_blocks_stripe_checkout_success', $attributes, $stripe, $session_id ); | ||
do_action( 'otter_blocks_stripe_checkout_success', $attributes, $this->stripe_api, $session_id ); | ||
} else { | ||
$message = isset( $attributes['cancelMessage'] ) ? wp_kses_post( $attributes['cancelMessage'] ) : __( 'Your payment was unsuccessful. If you have any questions, please email [email protected].', 'otter-blocks' ); | ||
} | ||
|
@@ -49,7 +133,7 @@ public function render( $attributes ) { | |
} | ||
} | ||
|
||
$product = $stripe->create_request( 'product', $attributes['product'] ); | ||
$product = $this->stripe_api->create_request( 'product', $attributes['product'] ); | ||
|
||
if ( is_wp_error( $product ) ) { | ||
return sprintf( | ||
|
@@ -65,7 +149,7 @@ public function render( $attributes ) { | |
$details_markup .= '<img src="' . esc_url( $product['images'][0] ) . '" alt="' . esc_attr( $product['description'] ) . '" />'; | ||
} | ||
|
||
$price = $stripe->create_request( 'price', $attributes['price'] ); | ||
$price = $this->stripe_api->create_request( 'price', $attributes['price'] ); | ||
|
||
if ( is_wp_error( $price ) ) { | ||
return sprintf( | ||
|
@@ -85,34 +169,18 @@ public function render( $attributes ) { | |
|
||
$mode = 'recurring' === $price['type'] ? 'subscription' : 'payment'; | ||
|
||
$permalink = add_query_arg( | ||
$session_url = add_query_arg( | ||
array( | ||
'stripe_session_id' => '{CHECKOUT_SESSION_ID}', | ||
'product_id' => $attributes['product'], | ||
'action' => 'buy_stripe', | ||
'product_id' => $attributes['product'], | ||
'price_id' => $attributes['price'], | ||
'url' => get_permalink(), | ||
'mode' => $mode, | ||
), | ||
get_permalink() | ||
); | ||
|
||
$session = $stripe->create_request( | ||
'create_session', | ||
array( | ||
'success_url' => $permalink, | ||
'cancel_url' => $permalink, | ||
'line_items' => array( | ||
array( | ||
'price' => $attributes['price'], | ||
'quantity' => 1, | ||
), | ||
), | ||
'mode' => $mode, | ||
) | ||
); | ||
|
||
if ( is_wp_error( $session ) ) { | ||
$button_markup = '<a>' . __( 'The product can not be purchased anymore.', 'otter-blocks' ) . $this->format_error( $session ) . '</a>'; | ||
} else { | ||
$button_markup = '<a href="' . esc_url( $session->url ) . '">' . __( 'Checkout', 'otter-blocks' ) . '</a>'; | ||
} | ||
$button_markup = '<a href="' . esc_url( $session_url ) . '">' . __( 'Checkout', 'otter-blocks' ) . '</a>'; | ||
|
||
return sprintf( | ||
'<div %1$s><div class="o-stripe-checkout">%2$s</div>%3$s</div>', | ||
|