TestGatewayOffsite.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\TestGateway; |
| 4 | |
| 5 | use Give\Framework\Http\Response\Types\JsonResponse; |
| 6 | use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 7 | use Give\Framework\PaymentGateways\Types\OffSitePaymentGateway; |
| 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 OffSitePaymentGateway |
| 19 | { |
| 20 | /** |
| 21 | * @inheritDoc |
| 22 | */ |
| 23 | public $routeMethods = [ |
| 24 | 'testGatewayMethod' |
| 25 | ]; |
| 26 | |
| 27 | /** |
| 28 | * @inheritDoc |
| 29 | */ |
| 30 | public static function id() |
| 31 | { |
| 32 | return 'test-gateway-offsite'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @inheritDoc |
| 37 | */ |
| 38 | public function getId() |
| 39 | { |
| 40 | return self::id(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @inheritDoc |
| 45 | */ |
| 46 | public function getName() |
| 47 | { |
| 48 | return __('Test Gateway Offsite', 'give'); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @inheritDoc |
| 53 | */ |
| 54 | public function getPaymentMethodLabel() |
| 55 | { |
| 56 | return __('Test Gateway Offsite', 'give'); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @inheritDoc |
| 61 | */ |
| 62 | public function getLegacyFormFieldMarkup($formId) |
| 63 | { |
| 64 | if (FormUtils::isLegacyForm($formId)) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | /** @var LegacyFormFieldMarkup $legacyFormFieldMarkup */ |
| 69 | $legacyFormFieldMarkup = give(LegacyFormFieldMarkup::class); |
| 70 | |
| 71 | return $legacyFormFieldMarkup(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @inheritDoc |
| 76 | */ |
| 77 | public function createPayment(GatewayPaymentData $paymentData) |
| 78 | { |
| 79 | // This will allow you to overload returnFromOffsiteRedirect($donationId) |
| 80 | $redirectUrl = $this->generateReturnUrlFromRedirectOffsite($paymentData->donationId); |
| 81 | |
| 82 | return new RedirectOffsite($redirectUrl); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * An example of using the provided offsite method of returning from a redirect. |
| 87 | * |
| 88 | * @inheritDoc |
| 89 | */ |
| 90 | public function returnFromOffsiteRedirect($donationId) |
| 91 | { |
| 92 | $this->updateDonation($donationId); |
| 93 | |
| 94 | return response()->redirectTo(give_get_success_page_uri()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * An example gateway method for extending the Gateway API for a given gateway. |
| 99 | * |
| 100 | * @param int $donationId |
| 101 | * @return JsonResponse |
| 102 | */ |
| 103 | public function testGatewayMethod($donationId) |
| 104 | { |
| 105 | $this->updateDonation($donationId); |
| 106 | |
| 107 | return response()->json(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Helper for updating donation |
| 112 | * |
| 113 | * @param $donationId |
| 114 | * @return void |
| 115 | */ |
| 116 | private function updateDonation($donationId) |
| 117 | { |
| 118 | give_insert_payment_note($donationId, 'NOTE GOES HERE'); |
| 119 | give_update_payment_status($donationId); |
| 120 | give_set_payment_transaction_id($donationId, "test-gateway-transaction-id"); |
| 121 | } |
| 122 | } |
| 123 |