TestGateway.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\TestGateway; |
| 4 | |
| 5 | use Give\Donations\Models\Donation; |
| 6 | use Give\Framework\PaymentGateways\Commands\GatewayCommand; |
| 7 | use Give\Framework\PaymentGateways\Commands\PaymentComplete; |
| 8 | use Give\Framework\PaymentGateways\Commands\PaymentRefunded; |
| 9 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 10 | use Give\Framework\Support\Facades\Scripts\ScriptAsset; |
| 11 | use Give\Helpers\Form\Utils as FormUtils; |
| 12 | use Give\Helpers\Language; |
| 13 | use Give\PaymentGateways\Gateways\TestGateway\Views\LegacyFormFieldMarkup; |
| 14 | |
| 15 | /** |
| 16 | * A gateway for testing the donation process. No actual payment is processed and only form validation is performed. |
| 17 | * |
| 18 | * @since 3.0.0 change to Test Donations and manual id to replace legacy gateway |
| 19 | * @since 2.18.0 |
| 20 | */ |
| 21 | class TestGateway extends PaymentGateway |
| 22 | { |
| 23 | /** |
| 24 | * @inheritDoc |
| 25 | */ |
| 26 | public static function id(): string |
| 27 | { |
| 28 | return 'manual'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @inheritDoc |
| 33 | */ |
| 34 | public function getId(): string |
| 35 | { |
| 36 | return self::id(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @inheritDoc |
| 41 | */ |
| 42 | public function getName(): string |
| 43 | { |
| 44 | return __('Test Donation', 'give'); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 2.32.0 updated to enqueue script |
| 49 | * @since 2.30.0 |
| 50 | */ |
| 51 | public function enqueueScript(int $formId) |
| 52 | { |
| 53 | $scriptAsset = ScriptAsset::get(GIVE_PLUGIN_DIR . 'build/testGateway.asset.php'); |
| 54 | |
| 55 | wp_enqueue_script( |
| 56 | $this::id(), |
| 57 | GIVE_PLUGIN_URL . 'build/testGateway.js', |
| 58 | $scriptAsset['dependencies'], |
| 59 | $scriptAsset['version'], |
| 60 | true |
| 61 | ); |
| 62 | |
| 63 | Language::setScriptTranslations($this::id()); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @inheritDoc |
| 68 | */ |
| 69 | public function getPaymentMethodLabel(): string |
| 70 | { |
| 71 | return __('Test Donation', 'give'); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 2.18.0 |
| 76 | */ |
| 77 | public function getLegacyFormFieldMarkup(int $formId, array $args): string |
| 78 | { |
| 79 | if (FormUtils::isLegacyForm($formId)) { |
| 80 | return ''; |
| 81 | } |
| 82 | |
| 83 | /** @var LegacyFormFieldMarkup $legacyFormFieldMarkup */ |
| 84 | $legacyFormFieldMarkup = give(LegacyFormFieldMarkup::class); |
| 85 | |
| 86 | return $legacyFormFieldMarkup(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @inheritDoc |
| 91 | */ |
| 92 | public function createPayment(Donation $donation, $gatewayData): GatewayCommand |
| 93 | { |
| 94 | $intent = $gatewayData['testGatewayIntent'] ?? 'test-gateway-intent'; |
| 95 | |
| 96 | return new PaymentComplete("test-gateway-transaction-id-{$intent}-$donation->id"); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @since 2.29.0 Return PaymentRefunded instead of a bool value |
| 101 | * @since 2.20.0 |
| 102 | * @inerhitDoc |
| 103 | */ |
| 104 | public function refundDonation(Donation $donation): PaymentRefunded |
| 105 | { |
| 106 | return new PaymentRefunded(); |
| 107 | } |
| 108 | } |
| 109 |