From c0ad4c286cee20ef47482c954e29265ece51d6ad Mon Sep 17 00:00:00 2001 From: Mygmarsuren Sedjav Date: Sat, 19 Aug 2023 09:52:13 +0800 Subject: [PATCH] rename gateway to driver --- README.md | 4 +- config/simple-payment.php | 2 +- .../migrations/create_payments_table.php.stub | 8 +- src/Actions/CreatePayment.php | 22 ++--- src/Actions/VerifyPayment.php | 6 +- src/CheckedPayment.php | 4 +- src/Concerns/InteractsWithPayments.php | 4 +- ...WithGatewayData.php => WithDriverData.php} | 4 +- .../AbstractDriver.php} | 4 +- .../Golomt/GolomtCheckedPayment.php | 6 +- .../Golomt/GolomtClient.php | 2 +- .../Golomt/GolomtDriver.php} | 6 +- .../Golomt/GolomtPendingPayment.php | 4 +- .../Qpay/QpayCheckedPayment.php | 6 +- src/{Gateways => Drivers}/Qpay/QpayClient.php | 2 +- .../Qpay/QpayDriver.php} | 8 +- .../Qpay/QpayPendingPayment.php | 10 +-- src/{Gateways => Drivers}/Qpay/result.json | 0 src/Facades/SimplePayment.php | 4 +- src/Payment.php | 20 ++--- src/PendingPayment.php | 4 +- src/SimplePaymentManager.php | 24 ++--- src/Support/PaymentFactory.php | 2 +- tests/Actions/CreatePaymentTest.php | 90 +++++++++---------- tests/Actions/VerifyPaymentTest.php | 18 ++-- tests/Http/BrowserReturnTest.php | 6 +- tests/Http/NotificationTest.php | 4 +- 27 files changed, 137 insertions(+), 137 deletions(-) rename src/Contracts/Results/{WithGatewayData.php => WithDriverData.php} (51%) rename src/{Gateways/AbstractGateway.php => Drivers/AbstractDriver.php} (92%) rename src/{Gateways => Drivers}/Golomt/GolomtCheckedPayment.php (81%) rename src/{Gateways => Drivers}/Golomt/GolomtClient.php (96%) rename src/{Gateways/Golomt/GolomtGateway.php => Drivers/Golomt/GolomtDriver.php} (87%) rename src/{Gateways => Drivers}/Golomt/GolomtPendingPayment.php (88%) rename src/{Gateways => Drivers}/Qpay/QpayCheckedPayment.php (76%) rename src/{Gateways => Drivers}/Qpay/QpayClient.php (98%) rename src/{Gateways/Qpay/QpayGateway.php => Drivers/Qpay/QpayDriver.php} (84%) rename src/{Gateways => Drivers}/Qpay/QpayPendingPayment.php (80%) rename src/{Gateways => Drivers}/Qpay/result.json (100%) diff --git a/README.md b/README.md index eaaa55d..241e811 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ This is the contents of the published config file: return [ 'default' => env('SIMPLE_PAYMENT_DEFAULT', 'qpay'), - 'gateways' => [ + 'drivers' => [ 'qpay' => [ 'env' => env('QPAY_ENV', 'fake'), 'username' => env('QPAY_USERNAME'), @@ -78,7 +78,7 @@ Route::get('/invoices/{invoice}/payment', function (Invoice $invoice) { }); ``` -If you need specific gateway, you can use `driver` method. +If you need specific driver, you can use `driver` method. ```php SimplePayment::driver('socialpay')->create($invoice); diff --git a/config/simple-payment.php b/config/simple-payment.php index fc5e8d0..60857c8 100644 --- a/config/simple-payment.php +++ b/config/simple-payment.php @@ -6,7 +6,7 @@ return [ 'default' => env('SIMPLE_PAYMENT_DEFAULT', 'qpay'), - 'gateways' => [ + 'drivers' => [ 'qpay' => [ 'env' => env('QPAY_ENV', 'fake'), 'username' => env('QPAY_USERNAME'), diff --git a/database/migrations/create_payments_table.php.stub b/database/migrations/create_payments_table.php.stub index 11683c3..1be6257 100644 --- a/database/migrations/create_payments_table.php.stub +++ b/database/migrations/create_payments_table.php.stub @@ -10,16 +10,16 @@ return new class extends Migration { Schema::create('payments', function (Blueprint $table) { $table->uuid('id')->primary(); - $table->string('gateway', 20)->index(); + $table->string('driver', 20)->index(); $table->string('status'); $table->decimal('amount', 12, 2); $table->foreignIdFor(config('simple-payment.user_model'), 'user_id')->nullable(); $table->uuidMorphs('payable'); $table->string('description'); - $table->string('gateway_transaction_id')->nullable(); - $table->decimal('gateway_transaction_fee', 12, 2)->nullable(); + $table->string('transaction_id')->nullable(); + $table->decimal('transaction_fee', 12, 2)->nullable(); $table->text('error_message')->nullable(); - $table->json('gateway_data')->nullable(); + $table->json('driver_data')->nullable(); $table->timestamp('expires_at')->nullable(); $table->timestamp('paid_at')->nullable(); $table->timestamp('verified_at')->nullable(); diff --git a/src/Actions/CreatePayment.php b/src/Actions/CreatePayment.php index 35be060..dca4e03 100644 --- a/src/Actions/CreatePayment.php +++ b/src/Actions/CreatePayment.php @@ -8,17 +8,17 @@ use MyagmarsurenSedjav\SimplePayment\Contracts\CanBePaidPartially; use MyagmarsurenSedjav\SimplePayment\Contracts\Payable; use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithExpiresAt; -use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithGatewayData; +use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithDriverData; use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithTransactionFee; use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithTransactionId; use MyagmarsurenSedjav\SimplePayment\Exceptions\NothingToPay; use MyagmarsurenSedjav\SimplePayment\Facades\SimplePayment; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\PendingPayment; class CreatePayment { - public function __invoke(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment + public function __invoke(AbstractDriver $driver, Payable $payable, array $options = []): PendingPayment { if ($payable->getPaymentAmount() <= 0) { throw new NothingToPay(__('Payment amount cannot be zero.')); @@ -26,10 +26,10 @@ public function __invoke(AbstractGateway $gateway, Payable $payable, array $opti $this->guardAgainstInvalidAmountOption($options, $payable); - return DB::transaction(fn () => $this->process($gateway, $payable, $options)); + return DB::transaction(fn () => $this->process($driver, $payable, $options)); } - private function process(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment + private function process(AbstractDriver $driver, Payable $payable, array $options = []): PendingPayment { // Урьдчилаад хүлээгдэж байгаа төлбөрийг өгөгдлийн санд үүсгээд өгнө. $payment = SimplePayment::paymentModel()::create([ @@ -38,29 +38,29 @@ private function process(AbstractGateway $gateway, Payable $payable, array $opti 'description' => $payable->getPaymentDescription(), 'payable_type' => $payable->getMorphClass(), 'payable_id' => $payable->getKey(), - 'gateway' => $gateway->name(), + 'driver' => $driver->name(), 'options' => $options, ]); // Төлбөрийг тухайн төлбөрийн гарцад бүртгэж өгнө. - $pendingPayment = $gateway->register($payment, $options); + $pendingPayment = $driver->register($payment, $options); $attributesShouldBeUpdated = []; // Хэрэв тухайн төлбөрийн хэлбэр нь гүйлгээг шалгахад өөрийн гүйлгээний // дугаарыг ашиглахыг шаарддаг бол хадгалж авах хэрэгтэй болно. if ($pendingPayment instanceof WithTransactionId) { - $attributesShouldBeUpdated['gateway_transaction_id'] = $pendingPayment->getTransactionId(); + $attributesShouldBeUpdated['transaction_id'] = $pendingPayment->getTransactionId(); } // Тухайн төлбөрийн гарц гүйлгээг хийхэд шимтгэл авдаг бол хадгалж авна. if ($pendingPayment instanceof WithTransactionFee) { - $attributesShouldBeUpdated['gateway_transaction_fee'] = $pendingPayment->getTransactionFee(); + $attributesShouldBeUpdated['transaction_fee'] = $pendingPayment->getTransactionFee(); } // Тухайн төлбөрийн гарц нэмэлт өгөгдөлтэй бол хадгалж авна. - if ($pendingPayment instanceof WithGatewayData) { - $attributesShouldBeUpdated['gateway_data'] = $pendingPayment->getGatewayData(); + if ($pendingPayment instanceof WithDriverData) { + $attributesShouldBeUpdated['driver_data'] = $pendingPayment->getDriverData(); } // Тухайн төлбөрийн гарц дуусах хугацаатай бол хадгалж авна. diff --git a/src/Actions/VerifyPayment.php b/src/Actions/VerifyPayment.php index 712aa8c..033c672 100644 --- a/src/Actions/VerifyPayment.php +++ b/src/Actions/VerifyPayment.php @@ -4,14 +4,14 @@ use MyagmarsurenSedjav\SimplePayment\CheckedPayment; use MyagmarsurenSedjav\SimplePayment\Events\PaymentWasMade; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\Payment; class VerifyPayment { - public function __invoke(AbstractGateway $gateway, Payment $payment): CheckedPayment + public function __invoke(AbstractDriver $driver, Payment $payment): CheckedPayment { - $result = $gateway->check($payment); + $result = $driver->check($payment); $attributesShouldBeUpdated = [ 'status' => $result->status(), diff --git a/src/CheckedPayment.php b/src/CheckedPayment.php index 22ab450..0e8ecf1 100644 --- a/src/CheckedPayment.php +++ b/src/CheckedPayment.php @@ -7,7 +7,7 @@ abstract class CheckedPayment implements Arrayable { - public function __construct(public Payment $payment, public array $gatewayResponse = []) + public function __construct(public Payment $payment, public array $driverResponse = []) { } @@ -26,7 +26,7 @@ public function toArray(): array 'status' => $this->status(), 'error_message' => $this->errorMessage(), 'payment' => $this->payment->toArray(), - 'gateway_response' => $this->gatewayResponse, + 'driver_response' => $this->driverResponse, ]; } } diff --git a/src/Concerns/InteractsWithPayments.php b/src/Concerns/InteractsWithPayments.php index 8fac803..07d118d 100644 --- a/src/Concerns/InteractsWithPayments.php +++ b/src/Concerns/InteractsWithPayments.php @@ -14,8 +14,8 @@ public function payments(): MorphMany return $this->morphMany(SimplePayment::paymentModel(), 'payable'); } - public function createPayment(string $gateway): Payment + public function createPayment(string $driver): Payment { - return app(CreatePayment::class)($gateway, $this); + return app(CreatePayment::class)($driver, $this); } } diff --git a/src/Contracts/Results/WithGatewayData.php b/src/Contracts/Results/WithDriverData.php similarity index 51% rename from src/Contracts/Results/WithGatewayData.php rename to src/Contracts/Results/WithDriverData.php index bd04aa2..918a8fb 100644 --- a/src/Contracts/Results/WithGatewayData.php +++ b/src/Contracts/Results/WithDriverData.php @@ -2,7 +2,7 @@ namespace MyagmarsurenSedjav\SimplePayment\Contracts\Results; -interface WithGatewayData +interface WithDriverData { - public function getGatewayData(): array; + public function getDriverData(): array; } diff --git a/src/Gateways/AbstractGateway.php b/src/Drivers/AbstractDriver.php similarity index 92% rename from src/Gateways/AbstractGateway.php rename to src/Drivers/AbstractDriver.php index 118af07..4933440 100644 --- a/src/Gateways/AbstractGateway.php +++ b/src/Drivers/AbstractDriver.php @@ -1,6 +1,6 @@ gatewayResponse, 'errorDesc'); + return Arr::get($this->driverResponse, 'errorDesc'); } private function errorCode() { - return Arr::get($this->gatewayResponse, 'errorCode'); + return Arr::get($this->driverResponse, 'errorCode'); } } diff --git a/src/Gateways/Golomt/GolomtClient.php b/src/Drivers/Golomt/GolomtClient.php similarity index 96% rename from src/Gateways/Golomt/GolomtClient.php rename to src/Drivers/Golomt/GolomtClient.php index ee27a78..6a09f28 100644 --- a/src/Gateways/Golomt/GolomtClient.php +++ b/src/Drivers/Golomt/GolomtClient.php @@ -1,6 +1,6 @@ gatewayResponse['invoice']; + return $this->driverResponse['invoice']; } public function getExpiresAt(): Carbon diff --git a/src/Gateways/Qpay/QpayCheckedPayment.php b/src/Drivers/Qpay/QpayCheckedPayment.php similarity index 76% rename from src/Gateways/Qpay/QpayCheckedPayment.php rename to src/Drivers/Qpay/QpayCheckedPayment.php index 26746db..6e93ef4 100644 --- a/src/Gateways/Qpay/QpayCheckedPayment.php +++ b/src/Drivers/Qpay/QpayCheckedPayment.php @@ -1,6 +1,6 @@ gatewayResponse, 'count') > 0 - && Arr::get($this->gatewayResponse, 'paid_amount') > 0; + return Arr::get($this->driverResponse, 'count') > 0 + && Arr::get($this->driverResponse, 'paid_amount') > 0; } public function status(): PaymentStatus diff --git a/src/Gateways/Qpay/QpayClient.php b/src/Drivers/Qpay/QpayClient.php similarity index 98% rename from src/Gateways/Qpay/QpayClient.php rename to src/Drivers/Qpay/QpayClient.php index 31332e6..e85f495 100644 --- a/src/Gateways/Qpay/QpayClient.php +++ b/src/Drivers/Qpay/QpayClient.php @@ -1,6 +1,6 @@ client->checkPayment($payment->gateway_transaction_id); + $checkedPayment = $this->client->checkPayment($payment->transaction_id); return new QpayCheckedPayment($payment, $checkedPayment); } diff --git a/src/Gateways/Qpay/QpayPendingPayment.php b/src/Drivers/Qpay/QpayPendingPayment.php similarity index 80% rename from src/Gateways/Qpay/QpayPendingPayment.php rename to src/Drivers/Qpay/QpayPendingPayment.php index 5115eae..5e77589 100644 --- a/src/Gateways/Qpay/QpayPendingPayment.php +++ b/src/Drivers/Qpay/QpayPendingPayment.php @@ -1,6 +1,6 @@ gatewayResponse['qr_image']; + return $this->driverResponse['qr_image']; } public function getRedirectUrl(): string { - return $this->gatewayResponse['qPay_shortUrl']; + return $this->driverResponse['qPay_shortUrl']; } public function render(): View @@ -28,13 +28,13 @@ public function render(): View 'payment' => $this->payment, 'base64QrImage' => $this->getBase64QrImage(), 'redirectUrl' => $this->getRedirectUrl(), - 'urls' => $this->gatewayResponse['urls'], + 'urls' => $this->driverResponse['urls'], ]); } public function getTransactionId(): string { - return $this->gatewayResponse['invoice_id']; + return $this->driverResponse['invoice_id']; } public function getTransactionFee(): float diff --git a/src/Gateways/Qpay/result.json b/src/Drivers/Qpay/result.json similarity index 100% rename from src/Gateways/Qpay/result.json rename to src/Drivers/Qpay/result.json diff --git a/src/Facades/SimplePayment.php b/src/Facades/SimplePayment.php index 0d9a787..2d6d03c 100644 --- a/src/Facades/SimplePayment.php +++ b/src/Facades/SimplePayment.php @@ -3,12 +3,12 @@ namespace MyagmarsurenSedjav\SimplePayment\Facades; use Illuminate\Support\Facades\Facade; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\PendingPayment; use MyagmarsurenSedjav\SimplePayment\SimplePaymentManager; /** - * @method static AbstractGateway driver($model, array $options = []) + * @method static AbstractDriver driver($model, array $options = []) * @method static PendingPayment create($model, array $options = []) * @method static mixed onBrowserReturn(\Closure $handler) * @method static string paymentModel() diff --git a/src/Payment.php b/src/Payment.php index ee80c5a..43fb0b8 100644 --- a/src/Payment.php +++ b/src/Payment.php @@ -13,13 +13,13 @@ use MyagmarsurenSedjav\SimplePayment\Contracts\Payable; use MyagmarsurenSedjav\SimplePayment\Enums\PaymentStatus; use MyagmarsurenSedjav\SimplePayment\Facades\SimplePayment; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\Support\PaymentFactory; /** * @property string $id * @property float $amount - * @property string $gateway_transaction_id + * @property string $transaction_id * @property ?Payable $payable * @property string $error_message * @property PaymentStatus $status @@ -27,12 +27,12 @@ * @property string $description * @property array|mixed $qpay * @property int $verifies_count - * @property string $gateway + * @property string $driver * @property Carbon $paid_at * @property Carbon $verified_at * @property Carbon $expires_at - * @property Carbon $gateway_transaction_fee - * @property array $gateway_data + * @property Carbon $transaction_fee + * @property array $driver_data * @property string $user_id * @property string $payable_type * @property string $payable_id @@ -53,7 +53,7 @@ class Payment extends Model protected $casts = [ 'status' => PaymentStatus::class, - 'gateway_data' => 'array', + 'driver_data' => 'array', 'paid_at' => 'datetime', 'verified_at' => 'datetime', 'expires_at' => 'datetime', @@ -100,17 +100,17 @@ public function isPaid(): bool public function verify(): CheckedPayment { - return $this->gateway()->verify($this); + return $this->driver()->verify($this); } public function check(): CheckedPayment { - return $this->gateway()->check($this); + return $this->driver()->check($this); } - public function gateway(): AbstractGateway + public function driver(): AbstractDriver { - return SimplePayment::driver($this->gateway); + return SimplePayment::driver($this->driver); } public function setOptionsAttribute(array $options) diff --git a/src/PendingPayment.php b/src/PendingPayment.php index 01062f5..a2e22c2 100644 --- a/src/PendingPayment.php +++ b/src/PendingPayment.php @@ -9,7 +9,7 @@ abstract class PendingPayment implements Arrayable, Responsable { - public function __construct(public Payment $payment, public array $gatewayResponse = []) + public function __construct(public Payment $payment, public array $driverResponse = []) { } @@ -19,7 +19,7 @@ public function toArray(): array 'handler' => $this instanceof ShouldRedirect ? 'redirect' : 'render', 'redirect_url' => $this instanceof ShouldRedirect ? $this->getRedirectUrl() : null, 'payment' => $this->payment, - 'gateway_response' => $this->gatewayResponse, + 'driver_response' => $this->driverResponse, ]; } diff --git a/src/SimplePaymentManager.php b/src/SimplePaymentManager.php index 19b24c9..5bc2b5a 100644 --- a/src/SimplePaymentManager.php +++ b/src/SimplePaymentManager.php @@ -4,9 +4,9 @@ use Closure; use Illuminate\Support\Manager; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; -use MyagmarsurenSedjav\SimplePayment\Gateways\Golomt\GolomtGateway; -use MyagmarsurenSedjav\SimplePayment\Gateways\Qpay\QpayGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; +use MyagmarsurenSedjav\SimplePayment\Drivers\Golomt\GolomtDriver; +use MyagmarsurenSedjav\SimplePayment\Drivers\Qpay\QpayDriver; class SimplePaymentManager extends Manager { @@ -17,29 +17,29 @@ public function getDefaultDriver() return $this->config->get('simple-payment.default'); } - public function createGolomtDriver(): AbstractGateway + public function createGolomtDriver(): AbstractDriver { - return new GolomtGateway( + return new GolomtDriver( name: 'golomt', - config: $this->config->get('simple-payment.gateways.golomt'), + config: $this->config->get('simple-payment.drivers.golomt'), isSocialPay: false ); } - public function createSocialPayDriver(): AbstractGateway + public function createSocialPayDriver(): AbstractDriver { - return new GolomtGateway( + return new GolomtDriver( name: 'socialpay', - config: $this->config->get('simple-payment.gateways.golomt'), + config: $this->config->get('simple-payment.drivers.golomt'), isSocialPay: true ); } - public function createQpayDriver(): AbstractGateway + public function createQpayDriver(): AbstractDriver { - return new QpayGateway( + return new QpayDriver( name: 'qpay', - config: $this->config->get('simple-payment.gateways.qpay') + config: $this->config->get('simple-payment.drivers.qpay') ); } diff --git a/src/Support/PaymentFactory.php b/src/Support/PaymentFactory.php index 9b68aa4..d808f8a 100644 --- a/src/Support/PaymentFactory.php +++ b/src/Support/PaymentFactory.php @@ -16,7 +16,7 @@ class PaymentFactory extends Factory public function definition() { return [ - 'gateway' => 'fake', + 'driver' => 'fake', 'amount' => rand(100, 1000) * 1000, 'payable_type' => Payable::class, 'payable_id' => 'fake-payable-id', diff --git a/tests/Actions/CreatePaymentTest.php b/tests/Actions/CreatePaymentTest.php index f26490f..13a0e3d 100644 --- a/tests/Actions/CreatePaymentTest.php +++ b/tests/Actions/CreatePaymentTest.php @@ -3,12 +3,12 @@ use Carbon\Carbon; use MyagmarsurenSedjav\SimplePayment\Actions\CreatePayment; use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithExpiresAt; -use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithGatewayData; +use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithDriverData; use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithTransactionFee; use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithTransactionId; use MyagmarsurenSedjav\SimplePayment\Enums\PaymentStatus; use MyagmarsurenSedjav\SimplePayment\Exceptions\NothingToPay; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\Payment; use MyagmarsurenSedjav\SimplePayment\PendingPayment; use MyagmarsurenSedjav\SimplePayment\Tests\Support\TestCanBePaidPartially; @@ -17,30 +17,30 @@ use function Pest\Laravel\assertDatabaseHas; it('should throw an exception if the payment amount for the given payable is zero', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect(); + $driver = mockWithPest(AbstractDriver::class)->expect(); $payable = TestPayable::create(['amount' => 0]); expect($payable->getPaymentAmount())->toBe(0.0); - app(CreatePayment::class)($gateway, $payable); + app(CreatePayment::class)($driver, $payable); })->throws(NothingToPay::class); it('should throw an exception if the payment amount for the given payable is negative', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect(); + $driver = mockWithPest(AbstractDriver::class)->expect(); $payable = TestPayable::create(['amount' => -100]); expect($payable->getPaymentAmount())->toBe(-100.0); - app(CreatePayment::class)($gateway, $payable); + app(CreatePayment::class)($driver, $payable); })->throws(NothingToPay::class); it('creates a pending payment', function () { $pendingPaymentMock = mockWithPest(PendingPayment::class)->expect(); - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock', + $driver = mockWithPest(AbstractDriver::class)->expect( + name: fn () => 'driver-mock', register: fn () => $pendingPaymentMock ); - $pendingPayment = app(CreatePayment::class)($gateway, $payable = TestPayable::create()); + $pendingPayment = app(CreatePayment::class)($driver, $payable = TestPayable::create()); expect($pendingPayment) ->toBeInstanceOf(PendingPayment::class) @@ -52,14 +52,14 @@ 'description' => $payable->getPaymentDescription(), 'payable_type' => $payable->getMorphClass(), 'payable_id' => $payable->getKey(), - 'gateway' => 'gateway-mock', + 'driver' => 'driver-mock', 'status' => PaymentStatus::Pending->value, ]); }); -test('if the gateway result has a transaction ID, it should be stored in the payment', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock', +test('if the driver result has a transaction ID, it should be stored in the payment', function () { + $driver = mockWithPest(AbstractDriver::class)->expect( + name: fn () => 'driver-mock', register: fn ($payment) => new class($payment) extends PendingPayment implements WithTransactionId { public function getTransactionId(): string @@ -69,16 +69,16 @@ public function getTransactionId(): string } ); - app(CreatePayment::class)($gateway, TestPayable::create()); + app(CreatePayment::class)($driver, TestPayable::create()); assertDatabaseHas(Payment::class, [ - 'gateway_transaction_id' => 'transaction-id', + 'transaction_id' => 'transaction-id', ]); }); -test('if the gateway result has a transaction Fee, it should be stored in the payment', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock', +test('if the driver result has a transaction Fee, it should be stored in the payment', function () { + $driver = mockWithPest(AbstractDriver::class)->expect( + name: fn () => 'driver-mock', register: fn ($payment) => new class($payment) extends PendingPayment implements WithTransactionFee { public function getTransactionFee(): float @@ -88,35 +88,35 @@ public function getTransactionFee(): float } ); - app(CreatePayment::class)($gateway, TestPayable::create()); + app(CreatePayment::class)($driver, TestPayable::create()); assertDatabaseHas(Payment::class, [ - 'gateway_transaction_fee' => 10.0, + 'transaction_fee' => 10.0, ]); }); -test('if the gateway result has a custom data, it should be stored in the payment', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock', - register: fn ($payment) => new class($payment) extends PendingPayment implements WithGatewayData +test('if the driver result has a custom data, it should be stored in the payment', function () { + $driver = mockWithPest(AbstractDriver::class)->expect( + name: fn () => 'driver-mock', + register: fn ($payment) => new class($payment) extends PendingPayment implements WithDriverData { - public function getGatewayData(): array + public function getDriverData(): array { return ['foo' => 'bar']; } } ); - app(CreatePayment::class)($gateway, TestPayable::create()); + app(CreatePayment::class)($driver, TestPayable::create()); assertDatabaseHas(Payment::class, [ - 'gateway_data' => json_encode(['foo' => 'bar']), + 'driver_data' => json_encode(['foo' => 'bar']), ]); }); -test('if the gateway result has a expire date, it should be stored in the payment', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock', +test('if the driver result has a expire date, it should be stored in the payment', function () { + $driver = mockWithPest(AbstractDriver::class)->expect( + name: fn () => 'driver-mock', register: fn ($payment) => new class($payment) extends PendingPayment implements WithExpiresAt { public function getExpiresAt(): Carbon @@ -126,7 +126,7 @@ public function getExpiresAt(): Carbon } ); - app(CreatePayment::class)($gateway, TestPayable::create()); + app(CreatePayment::class)($driver, TestPayable::create()); assertDatabaseHas(Payment::class, [ 'expires_at' => Carbon::now()->addDays(1), @@ -134,8 +134,8 @@ public function getExpiresAt(): Carbon }); test('it should override the payment amount if the amount option is provided', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock', + $driver = mockWithPest(AbstractDriver::class)->expect( + name: fn () => 'driver-mock', register: fn ($payment) => new class($payment) extends PendingPayment { } @@ -143,14 +143,14 @@ public function getExpiresAt(): Carbon $payable = TestCanBePaidPartially::create(); - app(CreatePayment::class)($gateway, $payable, ['amount' => 50]); + app(CreatePayment::class)($driver, $payable, ['amount' => 50]); assertDatabaseHas(Payment::class, ['amount' => 50]); }); test('it sets additional attributes if the extended model has', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock' + $driver = mockWithPest(AbstractDriver::class)->expect( + name: fn () => 'driver-mock' ); $payable = new class extends TestPayable @@ -163,30 +163,30 @@ public function getExpiresAt(): Carbon throw new \InvalidArgumentException('expected'); }); - app(CreatePayment::class)($gateway, $payable, [ + app(CreatePayment::class)($driver, $payable, [ 'foo' => 'bar', 'baz' => 'bol', ]); })->throws(\InvalidArgumentException::class); it('should throw an exception when the provided amount option is greater than amount of the payable', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect(); + $driver = mockWithPest(AbstractDriver::class)->expect(); - app(CreatePayment::class)($gateway, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => 100]); + app(CreatePayment::class)($driver, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => 100]); })->throws(InvalidArgumentException::class, 'Payment amount cannot be greater than payable amount.'); it('should throw an exception when the provided amount option is less than zero', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect(); + $driver = mockWithPest(AbstractDriver::class)->expect(); - app(CreatePayment::class)($gateway, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => -100]); + app(CreatePayment::class)($driver, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => -100]); })->throws(InvalidArgumentException::class, 'Payment amount cannot be zero.'); it('should throw an exception when the provided amount option is zero', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect(); - app(CreatePayment::class)($gateway, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => 0]); + $driver = mockWithPest(AbstractDriver::class)->expect(); + app(CreatePayment::class)($driver, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => 0]); })->throws(InvalidArgumentException::class, 'Payment amount cannot be zero.'); it('should throw an exception when the payable does not support partial payments', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect(); - app(CreatePayment::class)($gateway, TestPayable::create(['amount' => 50]), ['amount' => 25]); + $driver = mockWithPest(AbstractDriver::class)->expect(); + app(CreatePayment::class)($driver, TestPayable::create(['amount' => 50]), ['amount' => 25]); })->throws(InvalidArgumentException::class, 'Payment amount cannot be specified.'); diff --git a/tests/Actions/VerifyPaymentTest.php b/tests/Actions/VerifyPaymentTest.php index ad2f993..10857f7 100644 --- a/tests/Actions/VerifyPaymentTest.php +++ b/tests/Actions/VerifyPaymentTest.php @@ -6,7 +6,7 @@ use MyagmarsurenSedjav\SimplePayment\CheckedPayment; use MyagmarsurenSedjav\SimplePayment\Enums\PaymentStatus; use MyagmarsurenSedjav\SimplePayment\Events\PaymentWasMade; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\Payment; use MyagmarsurenSedjav\SimplePayment\Tests\Support\TestPayable; @@ -18,9 +18,9 @@ ->create(['verifies_count' => 1]); }); -function verify(AbstractGateway $gateway, Payment &$payment): CheckedPayment +function verify(AbstractDriver $driver, Payment &$payment): CheckedPayment { - $result = app(VerifyPayment::class)($gateway, $payment); + $result = app(VerifyPayment::class)($driver, $payment); $payment->refresh(); @@ -34,11 +34,11 @@ function verify(AbstractGateway $gateway, Payment &$payment): CheckedPayment successful: fn () => true, ); - $gateway = mockWithPest(AbstractGateway::class)->expect( + $driver = mockWithPest(AbstractDriver::class)->expect( check: fn () => $checkedPaymentMock, ); - $checkedPayment = verify($gateway, $this->payment); + $checkedPayment = verify($driver, $this->payment); expect($checkedPayment) ->toBeInstanceOf(CheckedPayment::class) @@ -54,7 +54,7 @@ function verify(AbstractGateway $gateway, Payment &$payment): CheckedPayment it('verifies a paid payment', function () { Event::fake(); - $gateway = mockWithPest(AbstractGateway::class)->expect( + $driver = mockWithPest(AbstractDriver::class)->expect( check: fn () => mockWithPest(CheckedPayment::class)->expect( status: fn () => PaymentStatus::Paid, errorMessage: fn () => 'Payment is complete', @@ -67,7 +67,7 @@ function verify(AbstractGateway $gateway, Payment &$payment): CheckedPayment ->with(Mockery::on(fn ($payment) => $payment->is($this->payment))) ->once(); - verify($gateway, $this->payment); + verify($driver, $this->payment); expect($this->payment) ->status->toBe(PaymentStatus::Paid) @@ -77,7 +77,7 @@ function verify(AbstractGateway $gateway, Payment &$payment): CheckedPayment }); it('verifies a failed payment', function () { - $gateway = mockWithPest(AbstractGateway::class)->expect( + $driver = mockWithPest(AbstractDriver::class)->expect( check: fn () => mockWithPest(CheckedPayment::class)->expect( status: fn () => PaymentStatus::Failed, errorMessage: fn () => 'Payment is failed', @@ -85,7 +85,7 @@ function verify(AbstractGateway $gateway, Payment &$payment): CheckedPayment ), ); - verify($gateway, $this->payment); + verify($driver, $this->payment); expect($this->payment->status)->toBe(PaymentStatus::Failed) ->and($this->payment->paid_at)->toBeNull(); diff --git a/tests/Http/BrowserReturnTest.php b/tests/Http/BrowserReturnTest.php index 1b1d4f5..9763a86 100644 --- a/tests/Http/BrowserReturnTest.php +++ b/tests/Http/BrowserReturnTest.php @@ -3,13 +3,13 @@ use MyagmarsurenSedjav\SimplePayment\CheckedPayment; use MyagmarsurenSedjav\SimplePayment\Enums\PaymentStatus; use MyagmarsurenSedjav\SimplePayment\Facades\SimplePayment; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\Payment; it('should verify and render the result', function () { $payment = Payment::factory()->create(); - SimplePayment::extend($payment->gateway, fn () => mockWithPest(AbstractGateway::class)->expect( + SimplePayment::extend($payment->driver, fn () => mockWithPest(AbstractDriver::class)->expect( verify: fn ($p) => new class($p) extends CheckedPayment { public function status(): PaymentStatus @@ -31,7 +31,7 @@ public function errorMessage(): string|null it('should return the result for the global filter of the simple manager', function () { $payment = Payment::factory()->create(); - SimplePayment::extend($payment->gateway, fn () => mockWithPest(AbstractGateway::class)->expect( + SimplePayment::extend($payment->driver, fn () => mockWithPest(AbstractDriver::class)->expect( verify: fn ($p) => new class($p) extends CheckedPayment { public function status(): PaymentStatus diff --git a/tests/Http/NotificationTest.php b/tests/Http/NotificationTest.php index 9cd265e..acd4b98 100644 --- a/tests/Http/NotificationTest.php +++ b/tests/Http/NotificationTest.php @@ -2,13 +2,13 @@ use MyagmarsurenSedjav\SimplePayment\CheckedPayment; use MyagmarsurenSedjav\SimplePayment\Facades\SimplePayment; -use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver; use MyagmarsurenSedjav\SimplePayment\Payment; it('should verify the given payment', function () { $payment = Payment::factory()->create(); - SimplePayment::extend($payment->gateway, fn () => mockWithPest(AbstractGateway::class)->expect( + SimplePayment::extend($payment->driver, fn () => mockWithPest(AbstractDriver::class)->expect( verify: fn ($p) => mockWithPest(CheckedPayment::class)->expect() ));