| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\PaymentGateways\Gateways\TestOffsiteGateway; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
use Give\Donations\Models\DonationNote; |
| 7 |
use Give\Donations\ValueObjects\DonationStatus; |
| 8 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 9 |
use Give\Framework\Http\Response\Types\RedirectResponse; |
| 10 |
use Give\Framework\PaymentGateways\Commands\PaymentRefunded; |
| 11 |
use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 12 |
use Give\Framework\PaymentGateways\PaymentGateway; |
| 13 |
use Give\Framework\Support\Facades\Scripts\ScriptAsset; |
| 14 |
use Give\Helpers\Form\Utils as FormUtils; |
| 15 |
use Give\Helpers\Language; |
| 16 |
use Give\PaymentGateways\Gateways\TestGateway\Views\LegacyFormFieldMarkup; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class TestGatewayOffsite |
| 20 |
* @since 2.18.0 |
| 21 |
*/ |
| 22 |
class TestOffsiteGateway extends PaymentGateway |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @inheritDoc |
| 26 |
*/ |
| 27 |
public $secureRouteMethods = [ |
| 28 |
'securelyReturnFromOffsiteRedirect' |
| 29 |
]; |
| 30 |
|
| 31 |
/** |
| 32 |
* @inheritDoc |
| 33 |
*/ |
| 34 |
public static function id(): string |
| 35 |
{ |
| 36 |
return 'test-offsite-gateway'; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @inheritDoc |
| 41 |
*/ |
| 42 |
public function getId(): string |
| 43 |
{ |
| 44 |
return self::id(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @inheritDoc |
| 49 |
*/ |
| 50 |
public function getName(): string |
| 51 |
{ |
| 52 |
return __('Test Gateway (Offsite)', 'give'); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @since 3.0.0 |
| 57 |
*/ |
| 58 |
public function formSettings(int $formId): array |
| 59 |
{ |
| 60 |
return [ |
| 61 |
'message' => __( |
| 62 |
'There are no fields for this gateway and you will not be charged. This payment option is only for you to test the donation experience.', |
| 63 |
'give' |
| 64 |
) |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @inheritDoc |
| 70 |
*/ |
| 71 |
public function getPaymentMethodLabel(): string |
| 72 |
{ |
| 73 |
return __('Test Gateway (Offsite)', 'give'); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @since 3.1.0 set translations for script |
| 78 |
* @since 2.30.0 |
| 79 |
*/ |
| 80 |
public function enqueueScript(int $formId) |
| 81 |
{ |
| 82 |
$scriptAsset = ScriptAsset::get(GIVE_PLUGIN_DIR . 'build/testOffsiteGateway.asset.php'); |
| 83 |
$handle = $this::id(); |
| 84 |
|
| 85 |
wp_enqueue_script( |
| 86 |
$this::id(), |
| 87 |
GIVE_PLUGIN_URL . 'build/testOffsiteGateway.js', |
| 88 |
$scriptAsset['dependencies'], |
| 89 |
$scriptAsset['version'], |
| 90 |
true |
| 91 |
); |
| 92 |
|
| 93 |
Language::setScriptTranslations($handle); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @since 2.18.0 |
| 98 |
*/ |
| 99 |
public function getLegacyFormFieldMarkup(int $formId, array $args): string |
| 100 |
{ |
| 101 |
if (FormUtils::isLegacyForm($formId)) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
/** @var LegacyFormFieldMarkup $legacyFormFieldMarkup */ |
| 106 |
$legacyFormFieldMarkup = give(LegacyFormFieldMarkup::class); |
| 107 |
|
| 108 |
return $legacyFormFieldMarkup(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @inheritDoc |
| 113 |
*/ |
| 114 |
public function createPayment(Donation $donation, $gatewayData) |
| 115 |
{ |
| 116 |
$redirectUrl = $this->generateSecureGatewayRouteUrl( |
| 117 |
'securelyReturnFromOffsiteRedirect', |
| 118 |
$donation->id, |
| 119 |
[ |
| 120 |
'givewp-donation-id' => $donation->id, |
| 121 |
'givewp-return-url' => $gatewayData['successUrl'] |
| 122 |
] |
| 123 |
); |
| 124 |
|
| 125 |
return new RedirectOffsite($redirectUrl); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @since 2.29.0 Return PaymentRefunded instead of a bool value |
| 130 |
* @since 2.20.0 |
| 131 |
* @inerhitDoc |
| 132 |
*/ |
| 133 |
public function refundDonation(Donation $donation): PaymentRefunded |
| 134 |
{ |
| 135 |
return new PaymentRefunded(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* An example of using a secureRouteMethod for extending the Gateway API to handle a redirect. |
| 140 |
* |
| 141 |
* @since 3.0.0 |
| 142 |
* |
| 143 |
* @throws Exception |
| 144 |
*/ |
| 145 |
protected function securelyReturnFromOffsiteRedirect(array $queryParams): RedirectResponse |
| 146 |
{ |
| 147 |
/** @var Donation $donation */ |
| 148 |
$donation = Donation::find($queryParams['givewp-donation-id']); |
| 149 |
$this->updateDonation($donation); |
| 150 |
|
| 151 |
return new RedirectResponse($queryParams['givewp-return-url']); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param Donation $donation |
| 156 |
* |
| 157 |
* @return void |
| 158 |
* @throws Exception |
| 159 |
*/ |
| 160 |
private function updateDonation(Donation $donation) |
| 161 |
{ |
| 162 |
$donation->status = DonationStatus::COMPLETE(); |
| 163 |
$donation->gatewayTransactionId = "test-gateway-transaction-id"; |
| 164 |
$donation->save(); |
| 165 |
|
| 166 |
DonationNote::create([ |
| 167 |
'donationId' => $donation->id, |
| 168 |
'content' => 'Donation Completed from Test Gateway Offsite.' |
| 169 |
]); |
| 170 |
} |
| 171 |
} |
| 172 |
|