|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MusahMusah\LaravelMultipaymentGateways\Gateways; |
| 6 | + |
| 7 | +use MusahMusah\LaravelMultipaymentGateways\Abstracts\BaseGateWay; |
| 8 | +use MusahMusah\LaravelMultipaymentGateways\Contracts\FlutterwaveContract; |
| 9 | +use MusahMusah\LaravelMultipaymentGateways\Exceptions\InvalidConfigurationException; |
| 10 | +use MusahMusah\LaravelMultipaymentGateways\Traits\ConsumesExternalServices; |
| 11 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\BankTrait; |
| 12 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\ChargeTrait; |
| 13 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\OtpTrait; |
| 14 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\PaymentPlanTrait; |
| 15 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\SettlementTrait; |
| 16 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\SubscriptionTrait; |
| 17 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\TransactionTrait; |
| 18 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\TransferBeneficiaryTrait; |
| 19 | +use MusahMusah\LaravelMultipaymentGateways\Traits\Flutterwave\TransferTrait; |
| 20 | + |
| 21 | +class FlutterwaveService extends BaseGateWay implements FlutterwaveContract |
| 22 | +{ |
| 23 | + use ConsumesExternalServices, |
| 24 | + BankTrait, |
| 25 | + SettlementTrait, |
| 26 | + SubscriptionTrait, |
| 27 | + PaymentPlanTrait, |
| 28 | + TransferBeneficiaryTrait, |
| 29 | + TransferTrait, |
| 30 | + OtpTrait, |
| 31 | + ChargeTrait, |
| 32 | + TransactionTrait; |
| 33 | + |
| 34 | + /** |
| 35 | + * The redirect url to consume the Flutterwave's service |
| 36 | + */ |
| 37 | + protected string $redirectUrl; |
| 38 | + |
| 39 | + /** |
| 40 | + * The payload to initiate the transaction |
| 41 | + */ |
| 42 | + protected array $payload; |
| 43 | + |
| 44 | + /** |
| 45 | + * The encryption key to encrypt payload for direct card charge |
| 46 | + */ |
| 47 | + protected string $encryptionKey; |
| 48 | + |
| 49 | + public function __construct() |
| 50 | + { |
| 51 | + $this->setPaymentGateway(); |
| 52 | + $this->setBaseUri(); |
| 53 | + $this->setSecret(); |
| 54 | + $this->setEncryptionKey(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Set the payment gateway for the class |
| 59 | + */ |
| 60 | + public function setPaymentGateway(): void |
| 61 | + { |
| 62 | + $this->paymentGateway = 'flutterwave'; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Set the encryption key for the class |
| 67 | + */ |
| 68 | + public function setEncryptionKey(): void |
| 69 | + { |
| 70 | + $encryptionKey = config('multipayment-gateways.flutterwave.encryption_key'); |
| 71 | + |
| 72 | + if (! $encryptionKey) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + $this->encryptionKey = $encryptionKey; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Set the base URI for the API request |
| 81 | + * |
| 82 | + * @throws InvalidConfigurationException |
| 83 | + */ |
| 84 | + public function setBaseUri(): void |
| 85 | + { |
| 86 | + $baseUri = config('multipayment-gateways.flutterwave.base_uri'); |
| 87 | + |
| 88 | + if (! $baseUri) { |
| 89 | + throw new InvalidConfigurationException("The Base URI for `{$this->paymentGateway}` is missing. Please ensure that the `base_uri` config key for `{$this->paymentGateway}` is set correctly."); |
| 90 | + } |
| 91 | + |
| 92 | + $this->baseUri = $baseUri; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Set the secret key for the API request |
| 97 | + * |
| 98 | + * @throws InvalidConfigurationException |
| 99 | + */ |
| 100 | + public function setSecret(): void |
| 101 | + { |
| 102 | + $secret = config('multipayment-gateways.flutterwave.secret'); |
| 103 | + |
| 104 | + if (! $secret) { |
| 105 | + throw new InvalidConfigurationException("The secret key for `{$this->paymentGateway}` is missing. Please ensure that the `secret` config key for `{$this->paymentGateway}` is set correctly."); |
| 106 | + } |
| 107 | + |
| 108 | + $this->secret = $secret; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Resolve the authorization URL / Endpoint |
| 113 | + */ |
| 114 | + public function resolveAuthorization(&$queryParams, &$formParams, &$headers): void |
| 115 | + { |
| 116 | + $headers['Authorization'] = $this->resolveAccessToken(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Set the access token for the request |
| 121 | + */ |
| 122 | + public function resolveAccessToken(): string |
| 123 | + { |
| 124 | + return "Bearer {$this->secret}"; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Decode the response |
| 129 | + */ |
| 130 | + public function decodeResponse(): array |
| 131 | + { |
| 132 | + return json_decode($this->response, true); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get the response |
| 137 | + */ |
| 138 | + public function getResponse(): array |
| 139 | + { |
| 140 | + return $this->response; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Get the data from the response |
| 145 | + */ |
| 146 | + public function getData(): array |
| 147 | + { |
| 148 | + return $this->getResponse()['data']; |
| 149 | + } |
| 150 | +} |
0 commit comments