TestGatewayOffsite.php
178 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\TestGateway; |
| 4 | |
| 5 | use Give\Donations\Models\Donation; |
| 6 | use Give\Donations\Models\DonationNote; |
| 7 | use Give\Donations\ValueObjects\DonationStatus; |
| 8 | use Give\Framework\Exceptions\Primitives\Exception; |
| 9 | use Give\Framework\Http\Response\Types\RedirectResponse; |
| 10 | use Give\Framework\PaymentGateways\Commands\GatewayCommand; |
| 11 | use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 12 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 13 | use Give\Helpers\Form\Utils as FormUtils; |
| 14 | use Give\PaymentGateways\Gateways\PayPalStandard\Actions\GenerateDonationReceiptPageUrl; |
| 15 | use Give\PaymentGateways\Gateways\TestGateway\Views\LegacyFormFieldMarkup; |
| 16 | use Give\Subscriptions\Models\Subscription; |
| 17 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 18 | |
| 19 | use function Give\Framework\Http\Response\response; |
| 20 | |
| 21 | /** |
| 22 | * Class TestGatewayOffsite |
| 23 | * @since 2.18.0 |
| 24 | */ |
| 25 | class TestGatewayOffsite extends PaymentGateway |
| 26 | { |
| 27 | /** |
| 28 | * @inheritDoc |
| 29 | */ |
| 30 | public $secureRouteMethods = [ |
| 31 | 'securelyReturnFromOffsiteRedirect' |
| 32 | ]; |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public static function id(): string |
| 38 | { |
| 39 | return 'test-gateway-offsite'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @inheritDoc |
| 44 | */ |
| 45 | public function getId(): string |
| 46 | { |
| 47 | return self::id(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | public function getName(): string |
| 54 | { |
| 55 | return __('Test Gateway Offsite', 'give'); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @inheritDoc |
| 60 | */ |
| 61 | public function getPaymentMethodLabel(): string |
| 62 | { |
| 63 | return __('Test Gateway Offsite', 'give'); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @inheritDoc |
| 68 | */ |
| 69 | public function getLegacyFormFieldMarkup(int $formId, array $args): string |
| 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(Donation $donation, $gatewayData) |
| 85 | { |
| 86 | $redirectUrl = $this->generateSecureGatewayRouteUrl( |
| 87 | 'securelyReturnFromOffsiteRedirect', |
| 88 | $donation->id, |
| 89 | ['give-donation-id' => $donation->id] |
| 90 | ); |
| 91 | |
| 92 | return new RedirectOffsite($redirectUrl); |
| 93 | } |
| 94 | |
| 95 | public function createSubscription( |
| 96 | Donation $donation, |
| 97 | Subscription $subscription, |
| 98 | $gatewayData |
| 99 | ): GatewayCommand { |
| 100 | $redirectUrl = $this->generateSecureGatewayRouteUrl( |
| 101 | 'securelyReturnFromOffsiteRedirect', |
| 102 | $donation->id, |
| 103 | [ |
| 104 | 'give-donation-id' => $donation->id, |
| 105 | 'give-subscription-id' => $subscription->id, |
| 106 | ] |
| 107 | ); |
| 108 | |
| 109 | return new RedirectOffsite($redirectUrl); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * An example of using a secureRouteMethod for extending the Gateway API to handle a redirect. |
| 114 | * |
| 115 | * @since 2.21.0 update to use Donation model |
| 116 | * @since 2.19.0 |
| 117 | * |
| 118 | * @param array $queryParams |
| 119 | * |
| 120 | * @return RedirectResponse |
| 121 | * @throws Exception |
| 122 | */ |
| 123 | protected function securelyReturnFromOffsiteRedirect(array $queryParams): RedirectResponse |
| 124 | { |
| 125 | $donation = Donation::find($queryParams['give-donation-id']); |
| 126 | |
| 127 | $this->updateDonation($donation); |
| 128 | |
| 129 | if ( $donation->type->isSubscription() ) { |
| 130 | $subscription = Subscription::find($queryParams['give-subscription-id']); |
| 131 | $this->updateSubscription($subscription); |
| 132 | } |
| 133 | |
| 134 | return response()->redirectTo((new GenerateDonationReceiptPageUrl())($donation->id)); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @since 2.20.0 |
| 139 | * @inerhitDoc |
| 140 | * @throws Exception |
| 141 | */ |
| 142 | public function refundDonation(Donation $donation) |
| 143 | { |
| 144 | $donation->status = DonationStatus::REFUNDED(); |
| 145 | $donation->save(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param Donation $donation |
| 150 | * |
| 151 | * @return void |
| 152 | * @throws Exception |
| 153 | */ |
| 154 | private function updateDonation(Donation $donation) |
| 155 | { |
| 156 | $donation->status = DonationStatus::COMPLETE(); |
| 157 | $donation->gatewayTransactionId = "test-gateway-transaction-id"; |
| 158 | $donation->save(); |
| 159 | |
| 160 | DonationNote::create([ |
| 161 | 'donationId' => $donation->id, |
| 162 | 'content' => 'Donation Completed from Test Gateway Offsite.' |
| 163 | ]); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @since 2.23.0 |
| 168 | * |
| 169 | * @return void |
| 170 | */ |
| 171 | private function updateSubscription(Subscription $subscription) |
| 172 | { |
| 173 | $subscription->status = SubscriptionStatus::ACTIVE(); |
| 174 | $subscription->transactionId = "test-gateway-transaction-id"; |
| 175 | $subscription->save(); |
| 176 | } |
| 177 | } |
| 178 |