Skip to content

Commit

Permalink
Merge branch 'develop' into fix/stripe-checkout-modal-placement
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaHungDinh authored Aug 2, 2023
2 parents 58a236c + 90a72cd commit 9f6920a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
10 changes: 8 additions & 2 deletions includes/class-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public function render_admin_notices() {
/**
* Render give frontend notices.
*
* @unreleased Display registered error on donation form.
* @since 1.8.9
* @access public
*
Expand All @@ -295,8 +296,13 @@ public function render_frontend_notices( $form_id = 0 ) {

$request_form_id = isset( $_REQUEST['form-id'] ) ? absint( $_REQUEST['form-id'] ) : 0;

// Sanity checks first: Ensure that gateway returned errors display on the appropriate form.
if ( ! isset( $_POST['give_ajax'] ) && $request_form_id !== $form_id ) {
// Sanity checks first:
// - Ensure that gateway returned errors display on the appropriate form.
// - Error should not display on AJAX request.
if (
isset( $_POST['give_ajax'] )
|| ( $request_form_id && $request_form_id !== $form_id )
) {
return;
}

Expand Down
19 changes: 18 additions & 1 deletion src/Framework/PaymentGateways/Traits/HandleHttpResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ trait HandleHttpResponses
/**
* Handle Response
*
* @unreleased added check for responding with json
* @since 2.27.0 add support for json content-type
* @since 2.18.0
*
Expand All @@ -19,7 +20,7 @@ trait HandleHttpResponses
public function handleResponse($type)
{
if ($type instanceof RedirectResponse) {
if (isset($_SERVER['CONTENT_TYPE']) && str_contains($_SERVER['CONTENT_TYPE'], "application/json")) {
if ($this->wantsJson()) {
wp_send_json([
'type' => 'redirect',
'data' => [
Expand Down Expand Up @@ -60,4 +61,20 @@ public function handleExceptionResponse(\Exception $exception, string $message)
give_set_error('PaymentGatewayException', $message);
give_send_back_to_checkout();
}

/**
* This checks the server headers for 'application/json' to determine if it should respond with json.
*
* @unreleased
*
* @return bool
*/
protected function wantsJson(): bool
{
if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json')) {
return true;
}

return isset($_SERVER['CONTENT_TYPE']) && str_contains($_SERVER['CONTENT_TYPE'], 'application/json');
}
}
19 changes: 18 additions & 1 deletion src/PaymentGateways/PayPalCommerce/ScriptLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function givePayPalOnBoardedCallback(mode, authCode, sharedId) {
/**
* Load public assets.
*
* @unreleased Handle exception if client token is not generated.
* @since 2.9.0
*/
public function loadPublicAssets()
Expand All @@ -144,6 +145,22 @@ public function loadPublicAssets()
/* @var MerchantDetail $merchant */
$merchant = give(MerchantDetail::class);
$scriptId = 'give-paypal-commerce-js';
$clientToken = '';

try{
$clientToken = $this->merchantRepository->getClientToken();
} catch ( \Exception $exception ) {
give_set_error(
'give-paypal-commerce-client-token-error',
sprintf(
esc_html__(
'Unable to load PayPal Commerce client token. Please try again later. Error: %1$s',
'give'
),
$exception->getMessage()
)
);
}

/**
* List of PayPal query parameters: https://developer.paypal.com/docs/checkout/reference/customize-sdk/#query-parameters
Expand All @@ -156,7 +173,7 @@ public function loadPublicAssets()
'disable-funding' => 'credit',
'vault' => true,
'data-partner-attribution-id' => give('PAYPAL_COMMERCE_ATTRIBUTION_ID'),
'data-client-token' => $this->merchantRepository->getClientToken(),
'data-client-token' => $clientToken,
];

if (give_is_setting_enabled(give_get_option('paypal_commerce_accept_venmo', 'disabled'))) {
Expand Down

0 comments on commit 9f6920a

Please sign in to comment.