TestGateway.php
114 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\GatewayCommand; |
| 8 | use Give\Framework\PaymentGateways\Commands\PaymentComplete; |
| 9 | use Give\Framework\PaymentGateways\Commands\SubscriptionComplete; |
| 10 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 11 | use Give\Helpers\Form\Utils as FormUtils; |
| 12 | use Give\PaymentGateways\Gateways\TestGateway\Views\LegacyFormFieldMarkup; |
| 13 | use Give\Subscriptions\Models\Subscription; |
| 14 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 15 | |
| 16 | /** |
| 17 | * Class TestGateway |
| 18 | * @since 2.18.0 |
| 19 | */ |
| 20 | class TestGateway extends PaymentGateway |
| 21 | { |
| 22 | /** |
| 23 | * @inheritDoc |
| 24 | */ |
| 25 | public static function id(): string |
| 26 | { |
| 27 | return 'test-gateway'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @inheritDoc |
| 32 | */ |
| 33 | public function getId(): string |
| 34 | { |
| 35 | return self::id(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @inheritDoc |
| 40 | */ |
| 41 | public function getName(): string |
| 42 | { |
| 43 | return __('Test Gateway', 'give'); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @inheritDoc |
| 48 | */ |
| 49 | public function getPaymentMethodLabel(): string |
| 50 | { |
| 51 | return __('Test Gateway', 'give'); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @inheritDoc |
| 56 | */ |
| 57 | public function getLegacyFormFieldMarkup(int $formId, array $args): string |
| 58 | { |
| 59 | if (FormUtils::isLegacyForm($formId)) { |
| 60 | return ''; |
| 61 | } |
| 62 | |
| 63 | /** @var LegacyFormFieldMarkup $legacyFormFieldMarkup */ |
| 64 | $legacyFormFieldMarkup = give(LegacyFormFieldMarkup::class); |
| 65 | |
| 66 | return $legacyFormFieldMarkup(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @inheritDoc |
| 71 | */ |
| 72 | public function createPayment(Donation $donation, $gatewayData = null): GatewayCommand |
| 73 | { |
| 74 | return new PaymentComplete("test-gateway-transaction-id-$donation->id"); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @inheritDoc |
| 79 | * |
| 80 | * @since 2.23.0 |
| 81 | */ |
| 82 | public function createSubscription( |
| 83 | Donation $donation, |
| 84 | Subscription $subscription, |
| 85 | $gatewayData = null |
| 86 | ): GatewayCommand { |
| 87 | return new SubscriptionComplete( |
| 88 | "test-gateway-transaction-id-$donation->id", |
| 89 | "test-gateway-subscription-id-$subscription->id" |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 2.23.0 |
| 95 | * |
| 96 | * @inheritDoc |
| 97 | */ |
| 98 | public function cancelSubscription(Subscription $subscription) |
| 99 | { |
| 100 | $subscription->status = SubscriptionStatus::CANCELLED(); |
| 101 | $subscription->save(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @since 2.20.0 |
| 106 | * @inerhitDoc |
| 107 | * @throws Exception |
| 108 | */ |
| 109 | public function refundDonation(Donation $donation) |
| 110 | { |
| 111 | throw new Exception('Method has not been implemented yet. Please use the legacy method in the meantime.'); |
| 112 | } |
| 113 | } |
| 114 |