diff --git a/README.md b/README.md index eaaa55d..744e44b 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' => [ + 'methods' => [ '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 method, 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..3ec622e 100644 --- a/config/simple-payment.php +++ b/config/simple-payment.php @@ -6,7 +6,7 @@ return [ 'default' => env('SIMPLE_PAYMENT_DEFAULT', 'qpay'), - 'gateways' => [ + 'methods' => [ '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..615a82d 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('method', 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('method_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 f76e0fa..848c2f1 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\WithMethodData; 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; use MyagmarsurenSedjav\SimplePayment\PendingPayment; class CreatePayment { - public function __invoke(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment + public function __invoke(AbstractPaymentMethod $method, 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($method, $payable, $options)); } - private function process(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment + private function process(AbstractPaymentMethod $method, 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(), + 'method' => $method->name(), 'options' => $options, ]); // Төлбөрийг тухайн төлбөрийн гарцад бүртгэж өгнө. - $pendingPayment = $gateway->register($payment, $options); + $pendingPayment = $method->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 WithMethodData) { + $attributesShouldBeUpdated['method_data'] = $pendingPayment->getMethodData(); } // Тухайн төлбөрийн гарц дуусах хугацаатай бол хадгалж авна. diff --git a/src/Actions/VerifyPayment.php b/src/Actions/VerifyPayment.php index ca793ed..8afecf4 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; use MyagmarsurenSedjav\SimplePayment\Payment; class VerifyPayment { - public function __invoke(AbstractGateway $gateway, Payment $payment): CheckedPayment + public function __invoke(AbstractPaymentMethod $method, Payment $payment): CheckedPayment { - $result = $gateway->check($payment); + $result = $method->check($payment); $attributesShouldBeUpdated = [ 'status' => $result->status(), diff --git a/src/CheckedPayment.php b/src/CheckedPayment.php index 22ab450..e9b6bac 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 $response = []) { } @@ -26,7 +26,7 @@ public function toArray(): array 'status' => $this->status(), 'error_message' => $this->errorMessage(), 'payment' => $this->payment->toArray(), - 'gateway_response' => $this->gatewayResponse, + 'response' => $this->response, ]; } } diff --git a/src/Concerns/InteractsWithPayments.php b/src/Concerns/InteractsWithPayments.php index 8fac803..daea023 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 $method): Payment { - return app(CreatePayment::class)($gateway, $this); + return app(CreatePayment::class)($method, $this); } } diff --git a/src/Contracts/Results/WithGatewayData.php b/src/Contracts/Results/WithMethodData.php similarity index 51% rename from src/Contracts/Results/WithGatewayData.php rename to src/Contracts/Results/WithMethodData.php index bd04aa2..07be0f6 100644 --- a/src/Contracts/Results/WithGatewayData.php +++ b/src/Contracts/Results/WithMethodData.php @@ -2,7 +2,7 @@ namespace MyagmarsurenSedjav\SimplePayment\Contracts\Results; -interface WithGatewayData +interface WithMethodData { - public function getGatewayData(): array; + public function getMethodData(): array; } diff --git a/src/Facades/SimplePayment.php b/src/Facades/SimplePayment.php index 6ff1861..cf074dc 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; use MyagmarsurenSedjav\SimplePayment\PendingPayment; use MyagmarsurenSedjav\SimplePayment\SimplePaymentManager; /** - * @method static AbstractGateway driver($model, array $options = []) + * @method static AbstractPaymentMethod 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/Methods/AbstractGateway.php b/src/Methods/AbstractPaymentMethod.php similarity index 96% rename from src/Methods/AbstractGateway.php rename to src/Methods/AbstractPaymentMethod.php index e9fbecc..847798f 100644 --- a/src/Methods/AbstractGateway.php +++ b/src/Methods/AbstractPaymentMethod.php @@ -9,7 +9,7 @@ use MyagmarsurenSedjav\SimplePayment\Payment; use MyagmarsurenSedjav\SimplePayment\PendingPayment; -abstract class AbstractGateway +abstract class AbstractPaymentMethod { public function __construct(public readonly string $name, protected readonly array $config) { diff --git a/src/Methods/Golomt/GolomtCheckedPayment.php b/src/Methods/Golomt/GolomtCheckedPayment.php index fab73be..4860264 100644 --- a/src/Methods/Golomt/GolomtCheckedPayment.php +++ b/src/Methods/Golomt/GolomtCheckedPayment.php @@ -25,11 +25,11 @@ public function status(): PaymentStatus public function errorMessage(): string|null { - return Arr::get($this->gatewayResponse, 'errorDesc'); + return Arr::get($this->response, 'errorDesc'); } private function errorCode() { - return Arr::get($this->gatewayResponse, 'errorCode'); + return Arr::get($this->response, 'errorCode'); } } diff --git a/src/Methods/Golomt/GolomtGateway.php b/src/Methods/Golomt/GolomtPaymentMethod.php similarity index 90% rename from src/Methods/Golomt/GolomtGateway.php rename to src/Methods/Golomt/GolomtPaymentMethod.php index c68d909..b91f901 100644 --- a/src/Methods/Golomt/GolomtGateway.php +++ b/src/Methods/Golomt/GolomtPaymentMethod.php @@ -3,11 +3,11 @@ namespace MyagmarsurenSedjav\SimplePayment\Methods\Golomt; use MyagmarsurenSedjav\SimplePayment\CheckedPayment; -use MyagmarsurenSedjav\SimplePayment\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; use MyagmarsurenSedjav\SimplePayment\Payment; use MyagmarsurenSedjav\SimplePayment\PendingPayment; -class GolomtGateway extends AbstractGateway +class GolomtPaymentMethod extends AbstractPaymentMethod { private GolomtClient $client; diff --git a/src/Methods/Golomt/GolomtPendingPayment.php b/src/Methods/Golomt/GolomtPendingPayment.php index 70ee815..7ef8f77 100644 --- a/src/Methods/Golomt/GolomtPendingPayment.php +++ b/src/Methods/Golomt/GolomtPendingPayment.php @@ -26,7 +26,7 @@ public function asSocialPay(): static public function getTransactionId(): string { - return $this->gatewayResponse['invoice']; + return $this->response['invoice']; } public function getExpiresAt(): Carbon diff --git a/src/Methods/Qpay/QpayCheckedPayment.php b/src/Methods/Qpay/QpayCheckedPayment.php index 37c64b6..2b8c1da 100644 --- a/src/Methods/Qpay/QpayCheckedPayment.php +++ b/src/Methods/Qpay/QpayCheckedPayment.php @@ -10,8 +10,8 @@ class QpayCheckedPayment extends CheckedPayment { private function isPaidOnQpay(): bool { - return Arr::get($this->gatewayResponse, 'count') > 0 - && Arr::get($this->gatewayResponse, 'paid_amount') > 0; + return Arr::get($this->response, 'count') > 0 + && Arr::get($this->response, 'paid_amount') > 0; } public function status(): PaymentStatus diff --git a/src/Methods/Qpay/QpayGateway.php b/src/Methods/Qpay/QpayPaymentMethod.php similarity index 88% rename from src/Methods/Qpay/QpayGateway.php rename to src/Methods/Qpay/QpayPaymentMethod.php index 1b81393..da9e92c 100644 --- a/src/Methods/Qpay/QpayGateway.php +++ b/src/Methods/Qpay/QpayPaymentMethod.php @@ -3,11 +3,11 @@ namespace MyagmarsurenSedjav\SimplePayment\Methods\Qpay; use MyagmarsurenSedjav\SimplePayment\CheckedPayment; -use MyagmarsurenSedjav\SimplePayment\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; use MyagmarsurenSedjav\SimplePayment\Payment; use MyagmarsurenSedjav\SimplePayment\PendingPayment; -class QpayGateway extends AbstractGateway +class QpayPaymentMethod extends AbstractPaymentMethod { private QpayClient $client; @@ -33,7 +33,7 @@ public function register(Payment $payment, array $options): PendingPayment public function check(Payment $payment): CheckedPayment { - $checkedPayment = $this->client->checkPayment($payment->gateway_transaction_id); + $checkedPayment = $this->client->checkPayment($payment->transaction_id); return new QpayCheckedPayment($payment, $checkedPayment); } diff --git a/src/Methods/Qpay/QpayPendingPayment.php b/src/Methods/Qpay/QpayPendingPayment.php index c89e819..b9f7098 100644 --- a/src/Methods/Qpay/QpayPendingPayment.php +++ b/src/Methods/Qpay/QpayPendingPayment.php @@ -14,12 +14,12 @@ class QpayPendingPayment extends PendingPayment implements ShouldRender, WithBas { public function getBase64QrImage(): string { - return $this->gatewayResponse['qr_image']; + return $this->response['qr_image']; } public function getRedirectUrl(): string { - return $this->gatewayResponse['qPay_shortUrl']; + return $this->response['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->response['urls'], ]); } public function getTransactionId(): string { - return $this->gatewayResponse['invoice_id']; + return $this->response['invoice_id']; } public function getTransactionFee(): float diff --git a/src/Payment.php b/src/Payment.php index a94b9a0..98a0516 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; 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 $method * @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 $method_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', + 'method_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->method()->verify($this); } public function check(): CheckedPayment { - return $this->gateway()->check($this); + return $this->method()->check($this); } - public function gateway(): AbstractGateway + public function method(): AbstractPaymentMethod { - return SimplePayment::driver($this->gateway); + return SimplePayment::driver($this->method); } public function setOptionsAttribute(array $options) diff --git a/src/PendingPayment.php b/src/PendingPayment.php index 01062f5..c6463b5 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 $response = []) { } @@ -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, + 'response' => $this->response, ]; } diff --git a/src/SimplePaymentManager.php b/src/SimplePaymentManager.php index c203f57..5b1c141 100644 --- a/src/SimplePaymentManager.php +++ b/src/SimplePaymentManager.php @@ -4,9 +4,9 @@ use Closure; use Illuminate\Support\Manager; -use MyagmarsurenSedjav\SimplePayment\Methods\AbstractGateway; -use MyagmarsurenSedjav\SimplePayment\Methods\Golomt\GolomtGateway; -use MyagmarsurenSedjav\SimplePayment\Methods\Qpay\QpayGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; +use MyagmarsurenSedjav\SimplePayment\Methods\Golomt\GolomtPaymentMethod; +use MyagmarsurenSedjav\SimplePayment\Methods\Qpay\QpayPaymentMethod; 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(): AbstractPaymentMethod { - return new GolomtGateway( + return new GolomtPaymentMethod( name: 'golomt', - config: $this->config->get('simple-payment.gateways.golomt'), + config: $this->config->get('simple-payment.methods.golomt'), isSocialPay: false ); } - public function createSocialPayDriver(): AbstractGateway + public function createSocialPayDriver(): AbstractPaymentMethod { - return new GolomtGateway( + return new GolomtPaymentMethod( name: 'socialpay', - config: $this->config->get('simple-payment.gateways.golomt'), + config: $this->config->get('simple-payment.methods.golomt'), isSocialPay: true ); } - public function createQpayDriver(): AbstractGateway + public function createQpayDriver(): AbstractPaymentMethod { - return new QpayGateway( + return new QpayPaymentMethod( name: 'qpay', - config: $this->config->get('simple-payment.gateways.qpay') + config: $this->config->get('simple-payment.methods.qpay') ); } diff --git a/src/Support/PaymentFactory.php b/src/Support/PaymentFactory.php index 9b68aa4..90a7389 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', + 'method' => '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 9bdcab7..c78b264 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\WithMethodData; 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; 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(); + $method = mockWithPest(AbstractPaymentMethod::class)->expect(); $payable = TestPayable::create(['amount' => 0]); expect($payable->getPaymentAmount())->toBe(0.0); - app(CreatePayment::class)($gateway, $payable); + app(CreatePayment::class)($method, $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(); + $method = mockWithPest(AbstractPaymentMethod::class)->expect(); $payable = TestPayable::create(['amount' => -100]); expect($payable->getPaymentAmount())->toBe(-100.0); - app(CreatePayment::class)($gateway, $payable); + app(CreatePayment::class)($method, $payable); })->throws(NothingToPay::class); it('creates a pending payment', function () { $pendingPaymentMock = mockWithPest(PendingPayment::class)->expect(); - $gateway = mockWithPest(AbstractGateway::class)->expect( - name: fn () => 'gateway-mock', + $method = mockWithPest(AbstractPaymentMethod::class)->expect( + name: fn () => 'method-mock', register: fn () => $pendingPaymentMock ); - $pendingPayment = app(CreatePayment::class)($gateway, $payable = TestPayable::create()); + $pendingPayment = app(CreatePayment::class)($method, $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', + 'method' => 'method-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 method result has a transaction ID, it should be stored in the payment', function () { + $method = mockWithPest(AbstractPaymentMethod::class)->expect( + name: fn () => 'method-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)($method, 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 method result has a transaction Fee, it should be stored in the payment', function () { + $method = mockWithPest(AbstractPaymentMethod::class)->expect( + name: fn () => 'method-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)($method, 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 method result has a custom data, it should be stored in the payment', function () { + $method = mockWithPest(AbstractPaymentMethod::class)->expect( + name: fn () => 'method-mock', + register: fn ($payment) => new class($payment) extends PendingPayment implements WithMethodData { - public function getGatewayData(): array + public function getMethodData(): array { return ['foo' => 'bar']; } } ); - app(CreatePayment::class)($gateway, TestPayable::create()); + app(CreatePayment::class)($method, TestPayable::create()); assertDatabaseHas(Payment::class, [ - 'gateway_data' => json_encode(['foo' => 'bar']), + 'method_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 method result has a expire date, it should be stored in the payment', function () { + $method = mockWithPest(AbstractPaymentMethod::class)->expect( + name: fn () => 'method-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)($method, 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', + $method = mockWithPest(AbstractPaymentMethod::class)->expect( + name: fn () => 'method-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)($method, $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' + $method = mockWithPest(AbstractPaymentMethod::class)->expect( + name: fn () => 'method-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)($method, $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(); + $method = mockWithPest(AbstractPaymentMethod::class)->expect(); - app(CreatePayment::class)($gateway, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => 100]); + app(CreatePayment::class)($method, 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(); + $method = mockWithPest(AbstractPaymentMethod::class)->expect(); - app(CreatePayment::class)($gateway, TestCanBePaidPartially::create(['amount' => 50]), ['amount' => -100]); + app(CreatePayment::class)($method, 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]); + $method = mockWithPest(AbstractPaymentMethod::class)->expect(); + app(CreatePayment::class)($method, 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]); + $method = mockWithPest(AbstractPaymentMethod::class)->expect(); + app(CreatePayment::class)($method, 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 2222158..c4fdcbf 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; 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(AbstractPaymentMethod $method, Payment &$payment): CheckedPayment { - $result = app(VerifyPayment::class)($gateway, $payment); + $result = app(VerifyPayment::class)($method, $payment); $payment->refresh(); @@ -34,11 +34,11 @@ function verify(AbstractGateway $gateway, Payment &$payment): CheckedPayment successful: fn () => true, ); - $gateway = mockWithPest(AbstractGateway::class)->expect( + $method = mockWithPest(AbstractPaymentMethod::class)->expect( check: fn () => $checkedPaymentMock, ); - $checkedPayment = verify($gateway, $this->payment); + $checkedPayment = verify($method, $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( + $method = mockWithPest(AbstractPaymentMethod::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($method, $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( + $method = mockWithPest(AbstractPaymentMethod::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($method, $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 a52b7f1..f923984 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; 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->method, fn () => mockWithPest(AbstractPaymentMethod::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->method, fn () => mockWithPest(AbstractPaymentMethod::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 219d4e8..44937fb 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\Methods\AbstractGateway; +use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod; 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->method, fn () => mockWithPest(AbstractPaymentMethod::class)->expect( verify: fn ($p) => mockWithPest(CheckedPayment::class)->expect() ));