TestGatewayOffsite.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\TestGateway; |
| 4 | |
| 5 | use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 6 | use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 7 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 8 | use Give\Helpers\Form\Utils as FormUtils; |
| 9 | use Give\PaymentGateways\DataTransferObjects\GatewayPaymentData; |
| 10 | use Give\PaymentGateways\Gateways\TestGateway\Views\LegacyFormFieldMarkup; |
| 11 | |
| 12 | use function Give\Framework\Http\Response\response; |
| 13 | |
| 14 | /** |
| 15 | * Class TestGatewayOffsite |
| 16 | * @since 2.18.0 |
| 17 | */ |
| 18 | class TestGatewayOffsite extends PaymentGateway |
| 19 | { |
| 20 | /** |
| 21 | * @inheritDoc |
| 22 | */ |
| 23 | public $routeMethods = [ |
| 24 | 'returnFromOffsiteRedirect' |
| 25 | ]; |
| 26 | |
| 27 | /** |
| 28 | * @inheritDoc |
| 29 | */ |
| 30 | public $secureRouteMethods = [ |
| 31 | 'securelyReturnFromOffsiteRedirect' |
| 32 | ]; |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public static function id() |
| 38 | { |
| 39 | return 'test-gateway-offsite'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @inheritDoc |
| 44 | */ |
| 45 | public function getId() |
| 46 | { |
| 47 | return self::id(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | public function getName() |
| 54 | { |
| 55 | return __('Test Gateway Offsite', 'give'); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @inheritDoc |
| 60 | */ |
| 61 | public function getPaymentMethodLabel() |
| 62 | { |
| 63 | return __('Test Gateway Offsite', 'give'); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @inheritDoc |
| 68 | */ |
| 69 | public function getLegacyFormFieldMarkup($formId, $args) |
| 70 | { |
| 71 | if (FormUtils::isLegacyForm($formId)) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | /** @var LegacyFormFieldMarkup $legacyFormFieldMarkup */ |
| 76 | $legacyFormFieldMarkup = give(LegacyFormFieldMarkup::class); |
| 77 | |
| 78 | return $legacyFormFieldMarkup(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @inheritDoc |
| 83 | */ |
| 84 | public function createPayment(GatewayPaymentData $paymentData) |
| 85 | { |
| 86 | $redirectUrl = $this->generateSecureGatewayRouteUrl( |
| 87 | 'securelyReturnFromOffsiteRedirect', |
| 88 | $paymentData->donationId, |
| 89 | ['give-donation-id' => $paymentData->donationId] |
| 90 | ); |
| 91 | |
| 92 | return new RedirectOffsite($redirectUrl); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * An example of using a routeMethod for extending the Gateway API to handle a redirect. |
| 97 | * |
| 98 | * @since 2.19.0 |
| 99 | * |
| 100 | * @param array $queryParams |
| 101 | * @throws PaymentGatewayException |
| 102 | */ |
| 103 | public function returnFromOffsiteRedirect($queryParams) |
| 104 | { |
| 105 | $donationId = $queryParams['give-donation-id']; |
| 106 | |
| 107 | if (!get_post($donationId)) { |
| 108 | throw new PaymentGatewayException('Donation does not exist'); |
| 109 | } |
| 110 | |
| 111 | $this->updateDonation($donationId); |
| 112 | |
| 113 | return response()->redirectTo(give_get_success_page_uri()); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * An example of using a secureRouteMethod for extending the Gateway API to handle a redirect. |
| 118 | * |
| 119 | * @since 2.19.0 |
| 120 | * |
| 121 | * @param array $queryParams |
| 122 | * @throws PaymentGatewayException |
| 123 | */ |
| 124 | public function securelyReturnFromOffsiteRedirect($queryParams) |
| 125 | { |
| 126 | $donationId = $queryParams['give-donation-id']; |
| 127 | |
| 128 | if (!get_post($donationId)) { |
| 129 | throw new PaymentGatewayException('Donation does not exist'); |
| 130 | } |
| 131 | |
| 132 | $this->updateDonation($donationId); |
| 133 | |
| 134 | return response()->redirectTo(give_get_success_page_uri()); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Helper for updating donation |
| 139 | * |
| 140 | * @param $donationId |
| 141 | * @return void |
| 142 | */ |
| 143 | private function updateDonation($donationId) |
| 144 | { |
| 145 | give_insert_payment_note($donationId, 'NOTE GOES HERE'); |
| 146 | give_update_payment_status($donationId); |
| 147 | give_set_payment_transaction_id($donationId, "test-gateway-transaction-id"); |
| 148 | } |
| 149 | } |
| 150 |