Skip to content

Commit

Permalink
feature: add off-site gateway simulation page
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubersilva committed Aug 30, 2024
1 parent 80373dd commit 9d3a63c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 26 deletions.
59 changes: 38 additions & 21 deletions src/OffSiteGateway/Gateway/OffSiteGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ public function createPayment(Donation $donation, $gatewayData): RedirectOffsite
$donation->gatewayTransactionId = $payment->id;
$donation->save();

return new RedirectOffsite($payment->checkoutUrl);
$paymentParameters['off-site-gateway-simulation'] = true;

$redirectUrl = add_query_arg($paymentParameters, home_url());

return new RedirectOffsite($redirectUrl);
//return new RedirectOffsite(add_query_arg('off-site-gateway-simulation', true, home_url()));
} catch (Exception $e) {
$donation->status = DonationStatus::FAILED();
$donation->save();
Expand Down Expand Up @@ -218,13 +223,17 @@ protected function handleCanceledPaymentReturn(array $queryParams): RedirectResp
*/
private function getPaymentsReturnURL(Donation $donation, $gatewayData): string
{
return $this->generateSecureGatewayRouteUrl(
'handleSuccessPaymentReturn',
$donation->id,
[
'donation-id' => $donation->id,
'givewp-return-url' => $gatewayData['successUrl'],
]
return urlencode(
esc_url_raw(
$this->generateSecureGatewayRouteUrl(
'handleSuccessPaymentReturn',
$donation->id,
[
'donation-id' => $donation->id,
'givewp-return-url' => $gatewayData['successUrl'],
]
)
)
);
}

Expand All @@ -233,13 +242,17 @@ private function getPaymentsReturnURL(Donation $donation, $gatewayData): string
*/
private function getPaymentsCancelURL(Donation $donation, $gatewayData): string
{
return $this->generateSecureGatewayRouteUrl(
'handleCanceledPaymentReturn',
$donation->id,
[
'donation-id' => $donation->id,
'givewp-return-url' => $gatewayData['cancelUrl'],
]
return urlencode(
esc_url_raw(
$this->generateSecureGatewayRouteUrl(
'handleCanceledPaymentReturn',
$donation->id,
[
'donation-id' => $donation->id,
'givewp-return-url' => $gatewayData['cancelUrl'],
]
)
)
);
}

Expand All @@ -248,12 +261,16 @@ private function getPaymentsCancelURL(Donation $donation, $gatewayData): string
*/
private function getPaymentsWebhookUrl(Donation $donation): string
{
return $this->generateGatewayRouteUrl(
$this->getWebhookNotificationsListener(),
[
'notification_type' => 'payments',
'payment_id' => $donation->id,
]
return urlencode(
esc_url_raw(
$this->generateGatewayRouteUrl(
$this->getWebhookNotificationsListener(),
[
'notification_type' => 'payments',
'payment_id' => $donation->id,
]
)
)
);
}
}
85 changes: 80 additions & 5 deletions src/OffSiteGateway/Gateway/OffSiteSimulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,98 @@

namespace GiveAddon\OffSiteGateway\Gateway;

/**
* @unreleased
*/
class OffSiteSimulation
{
/**
* @unreleased
*/
public function __invoke()
{
if ( ! isset($_GET['off-site-gateway-simulation'])) {
return;
}

$referrer = $_SERVER['HTTP_REFERER'];

//V2 referrer: https://givewp.local/give/v2-tests?giveDonationFormInIframe=1
if ($this->isLegacyFormReferrer()) {
echo '<script>window.top.location.href ="' . home_url($_SERVER['REQUEST_URI']) . '";</script>';
exit();
}

//V3 referrer: https://givewp.local/?givewp-route=donation-form-view&form-id=1350
ob_start();

$this->loadOffSiteGatewaySimulationMarkup();

echo 'Off-site Gateway Simulation';
echo ob_get_clean();

exit();
}

/**
* @unreleased
*/
private function isLegacyFormReferrer(): bool
{
//V2 referrer: https://example.com/give/v2-tests?giveDonationFormInIframe=1
//V3 referrer: https://example.com/?givewp-route=donation-form-view&form-id=1350
return strpos($_SERVER['HTTP_REFERER'], 'giveDonationFormInIframe') !== false;
}

/**
* @unreleased
*/
private function loadOffSiteGatewaySimulationMarkup()
{
?>
<style>
.container {
font-family: "Open Sans", Helvetica, Arial, sans-serif;
max-width: 800px;
margin: 100px auto;
}

a {
font-size: 1.5rem;
}
</style>
<div class="container">
<h1>
<?php
echo esc_html__('Off-site Gateway Simulation', 'ADDON_TEXTDOMAIN');
?>
</h1>
<p>
<?php
echo esc_html__('Donation amount:', 'ADDON_TEXTDOMAIN');
?>
<strong>
<?php
echo isset($_GET['amount']) ? $_GET['amount']['currency'] . ' ' . $_GET['amount']['value'] : 0;
?>
</strong>
</p>
<hr />
<p>
<strong>
<?php
echo esc_html__('Click on the links below to simulate off-site gateway actions:',
'ADDON_TEXTDOMAIN');
?>
</strong>
</p>
<a style="color:green;font-weight: bold;" href="<?php
echo $_GET['returnUrl'] ?>">Success Payment</a> | <a style="color:red;font-weight: bold;" href="<?php
echo $_GET['cancelUrl'] ?>">Canceled Payment</a>
<br />
<br />
<p>
<?php
echo ⚠️ . esc_html__('This page is being loaded directly from your site to demonstrate how off-site gateways work. In real-world integrations, this page should be the checkout page provided by the gateway you are integrating, so users can complete or cancel the payment and be redirected back to your site.',
'ADDON_TEXTDOMAIN');
?>
</p>
</div>
<?php
}
}

0 comments on commit 9d3a63c

Please sign in to comment.