Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Redefine contracts. #56

Merged
merged 36 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8c2a011
Rename payment method to payment instrument
r-kujawa May 1, 2024
c10f975
Update payavel/orchestration to v2.0.0
r-kujawa May 1, 2024
d97590c
Fix payment instrument renaming issues
r-kujawa May 4, 2024
76369ce
Rename PaymentTransaction to Payment
r-kujawa May 4, 2024
21b2f05
There is more...
r-kujawa May 4, 2024
4d5d194
Rename PaymentTransactionEvent to TransactionEvent
r-kujawa May 4, 2024
2edab89
Update base migrations for checkout
r-kujawa May 4, 2024
cc5c1c5
Adapt `Wallet` Model
r-kujawa May 5, 2024
6d63e26
Adapt `PaymentType` & `PaymentRail` models
r-kujawa May 5, 2024
ad48c34
Adapt `PaymentInstrument` model
r-kujawa May 5, 2024
8db5ebf
Adapt `Payment` model
r-kujawa May 5, 2024
3746f13
Fix integrity constraint violations
r-kujawa May 5, 2024
06dab33
Define `Refund` model
r-kujawa May 5, 2024
343b632
Define `Dispute` model
r-kujawa May 5, 2024
282bb39
Adapt `TransactionEvent` model
r-kujawa May 6, 2024
cc7c429
Add new models to config-service.stub
r-kujawa May 6, 2024
6ba3802
Add config-service-database.stub
r-kujawa May 6, 2024
693293d
Fix PaymentFactory
r-kujawa May 7, 2024
16d9753
Fix typo in migration
r-kujawa May 7, 2024
7b7cfb6
Update comments in Dispute
r-kujawa May 7, 2024
4dd6aa5
Fix comment to make more sense
r-kujawa May 7, 2024
5d1a751
Fix PaymentType model
r-kujawa May 8, 2024
04452e7
Fix WalletFactory
r-kujawa May 8, 2024
a3d60dc
Fix PaymentRail model
r-kujawa May 8, 2024
e4ac344
Fix Payment & Wallet factories
r-kujawa May 8, 2024
f7724b3
Add PaymentTypeTest
r-kujawa May 8, 2024
513e468
Add PaymentRailTest
r-kujawa May 8, 2024
e17ccac
Add another payment rail id test case
r-kujawa May 8, 2024
5c93e66
Add WalletTest
r-kujawa May 10, 2024
708695f
Add PaymentInstrumentTest
r-kujawa May 11, 2024
dd9d1a0
Rename model tests
r-kujawa May 11, 2024
eed2691
Add PaymentModelTest
r-kujawa May 11, 2024
1560b96
Add RefundModelTest
r-kujawa May 11, 2024
38f9a34
Add DisputeModelTest
r-kujawa May 11, 2024
9f87a35
Add TransactionEventModelTest
r-kujawa May 16, 2024
cb6c34e
getActualClassNameForMorph
r-kujawa May 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"test": "vendor/bin/phpunit"
},
"require": {
"payavel/orchestration": "dev-master"
"payavel/orchestration": "^2.0"
},
"require-dev": {
"orchestra/testbench": "^8.0|^9.0"
Expand Down
49 changes: 49 additions & 0 deletions database/factories/DisputeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Payavel\Checkout\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Payavel\Checkout\Models\Payment;
use Payavel\Checkout\Models\Dispute;

class DisputeFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Dispute::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'reference' => $this->faker->uuid(),
];
}

/**
* Configure the model factory.
*
* @return $this
*/
public function configure()
{
return $this->afterMaking(function (Dispute $dispute) {
if (is_null($dispute->payment_id)) {
$dispute->payment_id = Payment::inRandomOrder()->firstOr(
fn () => Payment::factory()->create()
)->id;
}

if (is_null($dispute->amount)) {
$dispute->amount = $dispute->payment->amount;
}
});
}
}
87 changes: 87 additions & 0 deletions database/factories/PaymentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Payavel\Checkout\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Payavel\Checkout\Models\Payment;
use Payavel\Checkout\Models\PaymentRail;
use Payavel\Orchestration\Models\Account;
use Payavel\Orchestration\Models\Provider;

class PaymentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Payment::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'reference' => $this->faker->uuid(),
'amount' => $this->faker->numberBetween(1, 999) * 100,
'currency' => $this->faker->currencyCode(),
];
}

/**
* Configure the model factory.
*
* @return $this
*/
public function configure()
{
return $this->afterMaking(function (Payment $payment) {
if (is_null($payment->provider_id)) {
$provider = ! is_null($payment->instrument_id)
? $payment->instrument->provider
: Provider::whereHas(
'accounts',
fn ($query) => $query->where('accounts.id', $payment->account_id)
)->inRandomOrder()->firstOr(
fn () => Provider::factory()->create()
);

$payment->provider_id = $provider->id;
}

if (is_null($payment->account_id)) {
$account = ! is_null($payment->instrument_id)
? $payment->instrument->account
: Account::whereHas(
'providers',
fn ($query) => $query->where('providers.id', $payment->provider_id)
)->inRandomOrder()
->firstOr(function () use ($payment) {
$account = Account::factory()->create(['default_provider_id' => $payment->provider_id]);

$account->providers()->attach($payment->provider_id);

return $account;
});

$payment->account_id = $account->id;
}

if (is_null($payment->rail_id)) {
$rail = ! is_null($payment->instrument_id)
? $payment->instrument->type->rails()->inRandomOrder()->firstOrCreate(
['parent_type_id' => $payment->instrument->type_id],
['type_id' => $payment->instrument->type_id]
) : PaymentRail::inRandomOrder()
->firstOr(
fn () => PaymentRail::factory()->create()
);

$payment->rail_id = $rail->id;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

use Illuminate\Database\Eloquent\Factories\Factory;
use Payavel\Checkout\Models\Wallet;
use Payavel\Checkout\Models\PaymentMethod;
use Payavel\Checkout\Models\PaymentInstrument;
use Payavel\Checkout\Models\PaymentType;

class PaymentMethodFactory extends Factory
class PaymentInstrumentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = PaymentMethod::class;
protected $model = PaymentInstrument::class;

/**
* Define the model's default state.
Expand All @@ -24,7 +24,7 @@ class PaymentMethodFactory extends Factory
public function definition()
{
return [
'token' => $this->faker->uuid(),
'reference' => $this->faker->uuid(),
];
}

Expand All @@ -35,21 +35,21 @@ public function definition()
*/
public function configure()
{
return $this->afterMaking(function (PaymentMethod $paymentMethod) {
if(is_null($paymentMethod->wallet_id)) {
return $this->afterMaking(function (PaymentInstrument $paymentInstrument) {
if (is_null($paymentInstrument->wallet_id)) {
$wallet = Wallet::inRandomOrder()->firstOr(
fn () => Wallet::factory()->create()
);

$paymentMethod->wallet_id = $wallet->id;
$paymentInstrument->wallet_id = $wallet->id;
}

if (is_null($paymentMethod->type_id)) {
if (is_null($paymentInstrument->type_id)) {
$type = PaymentType::inRandomOrder()->firstOr(
fn () => PaymentType::factory()->create()
);

$paymentMethod->type_id = $type->id;
$paymentInstrument->type_id = $type->id;
}
});
}
Expand Down
133 changes: 133 additions & 0 deletions database/factories/PaymentRailFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Payavel\Checkout\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Payavel\Checkout\Models\PaymentRail;
use Payavel\Checkout\Models\PaymentType;

class PaymentRailFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = PaymentRail::class;

public const REAL = [
[
'parent_type_id' => 'visa',
'type_id' => 'visa',
],
[
'parent_type_id' => 'mastercard',
'type_id' => 'mastercard',
],
[
'parent_type_id' => 'amex',
'type_id' => 'amex',
],
[
'parent_type_id' => 'discover',
'type_id' => 'discover',
],
[
'parent_type_id' => 'diners_club',
'type_id' => 'diners_club',
],
[
'parent_type_id' => 'jcb',
'type_id' => 'jcb',
],
[
'parent_type_id' => 'apple_pay',
'type_id' => 'visa',
],
[
'parent_type_id' => 'apple_pay',
'type_id' => 'mastercard',
],
[
'parent_type_id' => 'apple_pay',
'type_id' => 'amex',
],
[
'parent_type_id' => 'apple_pay',
'type_id' => 'discover',
],
[
'parent_type_id' => 'google_pay',
'type_id' => 'visa',
],
[
'parent_type_id' => 'google_pay',
'type_id' => 'mastercard',
],
[
'parent_type_id' => 'google_pay',
'type_id' => 'amex',
],
[
'parent_type_id' => 'google_pay',
'type_id' => 'discover',
],
[
'parent_type_id' => 'paypal',
'type_id' => 'paypal',
],
];

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [];
}

public function real()
{
return $this->state(function () {
$existingPaymentRails = PaymentRail::all()->pluck('id');

$rail = collect(static::REAL)
->filter(function ($realPaymentRail) use ($existingPaymentRails) {
$realPaymentRailId = $realPaymentRail['parent_type_id'] === $realPaymentRail['type_id']
? $realPaymentRail['type_id']
: "{$realPaymentRail['parent_type_id']}:{$realPaymentRail['type_id']}";

return $existingPaymentRails->doesntContain($realPaymentRailId);
})
->first();

if (is_null($rail)) {
return [];
}

return $rail;
});
}

/**
* Configure the model factory.
*
* @return $this
*/
public function configure()
{
return $this->afterMaking(function (PaymentRail $paymentRail) {
if (is_null($paymentRail->parent_type_id)) {
$paymentRail->parent_type_id = PaymentType::inRandomOrder()->firstOr(
fn () => PaymentType::factory()->create()
)->id;
}

if (is_null($paymentRail->type_id)) {
$paymentRail->type_id = PaymentType::factory()->create()->id;
}
});
}
}
57 changes: 0 additions & 57 deletions database/factories/PaymentTransactionEventFactory.php

This file was deleted.

Loading
Loading