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