Skip to content

Commit

Permalink
Remove obsolete test case and add new test cases for payment methods.
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com>
  • Loading branch information
sampoyigi committed Nov 29, 2024
1 parent c283367 commit d909cee
Show file tree
Hide file tree
Showing 49 changed files with 3,544 additions and 159 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"mollie"
],
"require": {
"tastyigniter/core": "^v4.0@beta",
"tastyigniter/core": "^v4.0@beta || ^v4.0@dev",
"php-http/guzzle7-adapter": "~1.0",
"authorizenet/authorizenet": "2.0.2",
"stripe/stripe-php": "~7.93.0",
Expand Down Expand Up @@ -69,5 +69,5 @@
},
"sort-packages": true
},
"minimum-stability": "beta"
"minimum-stability": "dev"
}
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:YzE5YjR2b3hrem1ucmdmc2Fkbm92NW1veHBkMWdpa3k="/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="testbench"/>
<env name="DB_USERNAME" value="forge"/>
</php>
<source>
<include>
Expand Down
22 changes: 9 additions & 13 deletions src/Classes/AuthorizeNetClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public function createTransactionRequest(): CreateTransactionRequest
return $this->transactionRequest = $request;
}

public function createTransaction(?CreateTransactionRequest $request): TransactionResponseType
public function createTransaction(CreateTransactionController $controller): TransactionResponseType
{
$controller = new CreateTransactionController($request);

$response = $controller->executeWithApiResponse($this->sandbox
? \net\authorize\api\constants\ANetEnvironment::SANDBOX
: \net\authorize\api\constants\ANetEnvironment::PRODUCTION);
Expand All @@ -50,15 +48,13 @@ public function createTransaction(?CreateTransactionRequest $request): Transacti

$transactionResponse = $response->getTransactionResponse();

throw_unless(
$response->getMessages()->getResultCode() == 'Ok',
new ApplicationException($this->getErrorMessageFromResponse($response, $transactionResponse))
);
if ($response->getMessages()->getResultCode() !== 'Ok') {
throw new ApplicationException($this->getErrorMessageFromResponse($response, $transactionResponse));
}

throw_if(
is_null($transactionResponse) || is_null($transactionResponse->getMessages()),
new ApplicationException($this->getErrorMessageFromResponse($response, $transactionResponse))
);
if (is_null($transactionResponse) || is_null($transactionResponse->getMessages())) {
throw new ApplicationException($this->getErrorMessageFromResponse($response, $transactionResponse));
}

return $transactionResponse;
}
Expand All @@ -69,13 +65,13 @@ protected function getErrorMessageFromResponse(?AnetApiResponseType $response, ?
if ($transactionResponse != null && $transactionResponse->getErrors() != null) {
return sprintf($message,
$transactionResponse->getErrors()[0]->getErrorCode(),
$transactionResponse->getErrors()[0]->getErrorText()
$transactionResponse->getErrors()[0]->getErrorText(),
);
}

return sprintf($message,
$response->getMessages()->getMessage()[0]->getCode(),
$response->getMessages()->getMessage()[0]->getText()
$response->getMessages()->getMessage()[0]->getText(),
);
}
}
10 changes: 8 additions & 2 deletions src/Classes/BasePaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,18 @@ protected function validatePaymentMethod($order, $host)
* @param Model $host Type model object containing configuration fields values.
* @param Model $order Order model object.
*/
public function processPaymentForm($data, $host, $order) {}
public function processPaymentForm($data, $host, $order)
{
throw new \LogicException('Method processPaymentForm must be implemented on your custom payment class.');
}

/**
* Executed when this gateway is rendered on the checkout page.
*/
public function beforeRenderPaymentForm($host, $controller) {}
public function beforeRenderPaymentForm($host, $controller)
{
throw new \LogicException('Method beforeRenderPaymentForm must be implemented on your custom payment class.');
}

public function getPaymentFormViewName()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/WithApplicableFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function validateApplicableFee(Order $order, ?Payment $host = null)
if (!$this->isApplicable($order->order_total, $host)) {
throw new ApplicationException(sprintf(
lang('igniter.payregister::default.alert_min_order_total'),
currency_format($host->order_total), $host->name
currency_format($host->order_total), $host->name,
));
}
}
Expand Down Expand Up @@ -56,4 +56,4 @@ public function getFormattedApplicableFee(?Payment $host = null): string
? $host->order_fee.'%'
: currency_format($host->order_fee);
}
}
}
5 changes: 3 additions & 2 deletions src/Database/Factories/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Igniter\PayRegister\Database\Factories;

use Igniter\Flame\Database\Factories\Factory;
use Igniter\PayRegister\Tests\Payments\Fixtures\TestPayment;

class PaymentFactory extends Factory
{
Expand All @@ -12,8 +13,8 @@ public function definition(): array
{
return [
'name' => $this->faker->name,
'code' => $this->faker->name,
'class_name' => $this->faker->name,
'code' => $this->faker->word,
'class_name' => TestPayment::class,
'description' => $this->faker->text,
'data' => [],
'priority' => $this->faker->randomNumber(),
Expand Down
23 changes: 23 additions & 0 deletions src/Database/Factories/PaymentLogFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Igniter\PayRegister\Database\Factories;

use Igniter\Flame\Database\Factories\Factory;

class PaymentLogFactory extends Factory
{
protected $model = \Igniter\PayRegister\Models\PaymentLog::class;

public function definition(): array
{
return [
'message' => $this->faker->sentence,
'payment_code' => $this->faker->word,
'payment_name' => $this->faker->name,
'is_success' => true,
'request' => [],
'response' => [],
'is_refundable' => false,
];
}
}
23 changes: 23 additions & 0 deletions src/Database/Factories/PaymentProfileFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Igniter\PayRegister\Database\Factories;

use Igniter\PayRegister\Models\PaymentProfile;
use Illuminate\Database\Eloquent\Factories\Factory;

class PaymentProfileFactory extends Factory
{
protected $model = PaymentProfile::class;

public function definition(): array
{
return [
'customer_id' => 1,
'payment_id' => 1,
'card_brand' => 1,
'card_last4' => 1,
'profile_data' => [],
'is_primary' => false,
];
}
}
6 changes: 4 additions & 2 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

class Extension extends BaseExtension
{
protected $subscribe = [
FormFieldsSubscriber::class,
];

protected $listen = [
'igniter.checkout.afterSaveOrder' => [
UpdatePaymentIntentSessionOnCheckout::class,
Expand Down Expand Up @@ -117,8 +121,6 @@ public function registerOnboardingSteps()

public function boot()
{
Event::subscribe(FormFieldsSubscriber::class);

Event::listen('main.theme.activated', function() {
Payment::syncAll();
});
Expand Down
14 changes: 7 additions & 7 deletions src/FormWidgets/PaymentAttempts.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ public function getSaveValue(mixed $value): int

public function onLoadRecord()
{
$paymentLogId = post('recordId');
$paymentLogId = input('recordId');

throw_unless($model = PaymentLog::find($paymentLogId),
new FlashException('Record not found')
);
throw_unless($model = PaymentLog::find($paymentLogId), new FlashException('Record not found'));

$formTitle = sprintf(lang($this->formTitle), currency_format($model->order->order_total));

Expand All @@ -67,19 +65,21 @@ public function onLoadRecord()

public function onSaveRecord()
{
$paymentLog = PaymentLog::find(post('recordId'));
$paymentLogId = input('recordId');

throw_unless($paymentLog = PaymentLog::find($paymentLogId), new FlashException('Record not found'));

$paymentMethod = $this->model->payment_method;

throw_unless(
$paymentLog && $paymentMethod->canRefundPayment($paymentLog),
new FlashException('No successful payment to refund')
new FlashException('No successful payment to refund'),
);

$widget = $this->makeRefundFormWidget($paymentLog);
$data = $widget->getSaveData();

$this->validate($data, $widget->config['rules']);
$this->validate($data, $widget->config['rules'] ?? []);

$paymentMethod->processRefundForm($data, $this->model, $paymentLog);

Expand Down
19 changes: 3 additions & 16 deletions src/Http/Controllers/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function listOverrideColumnValue($record, $column, $alias = null)
public function formFindModelObject($paymentCode = null)
{
throw_unless(strlen($paymentCode),
new FlashException(lang('igniter.payregister::default.alert_setting_missing_id'))
new FlashException(lang('igniter.payregister::default.alert_setting_missing_id')),
);

$model = $this->formCreateModelObject();
Expand All @@ -115,25 +115,12 @@ public function formFindModelObject($paymentCode = null)
$this->formExtendQuery($query);

throw_unless($result = $query->whereCode($paymentCode)->first(),
new FlashException(lang('igniter::admin.form.not_found'))
new FlashException(lang('igniter::admin.form.not_found')),
);

return $this->formExtendModel($result) ?: $result;
}

protected function getGateway($code)
{
if ($this->gateway !== null) {
return $this->gateway;
}

throw_unless($gateway = resolve(PaymentGateways::class)->findGateway($code),
new FlashException(sprintf(lang('igniter.payregister::default.alert_code_not_found'), $code))
);

return $this->gateway = $gateway;
}

public function formExtendModel($model)
{
if (!$model->exists) {
Expand All @@ -158,7 +145,7 @@ public function formExtendFieldsBefore($form)
public function formBeforeCreate($model)
{
throw_unless(strlen($code = post('Payment.payment')),
new FlashException(lang('igniter.payregister::default.alert_invalid_code'))
new FlashException(lang('igniter.payregister::default.alert_invalid_code')),
);

$paymentGateway = resolve(PaymentGateways::class)->findGateway($code);
Expand Down
5 changes: 3 additions & 2 deletions src/Models/PaymentLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Igniter\Cart\Events\OrderBeforeRefundProcessedEvent;
use Igniter\Cart\Events\OrderRefundProcessedEvent;
use Igniter\Flame\Database\Factories\HasFactory;
use Igniter\Flame\Database\Model;
use Igniter\Flame\Database\Traits\Validation;

Expand All @@ -13,6 +14,7 @@
*/
class PaymentLog extends Model
{
use HasFactory;
use Validation;

/**
Expand All @@ -29,8 +31,6 @@ class PaymentLog extends Model

public $timestamps = true;

public $dates = ['refunded_at'];

public $relation = [
'belongsTo' => [
'order' => [\Igniter\Cart\Models\Order::class],
Expand All @@ -55,6 +55,7 @@ class PaymentLog extends Model
'response' => 'array',
'is_success' => 'boolean',
'is_refundable' => 'boolean',
'refunded_at' => 'datetime',
];

public static function logAttempt($order, $message, $isSuccess, $request = [], $response = [], $isRefundable = false)
Expand Down
3 changes: 3 additions & 0 deletions src/Models/PaymentProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Igniter\PayRegister\Models;

use Igniter\Flame\Database\Factories\HasFactory;
use Igniter\Flame\Database\Model;

class PaymentProfile extends Model
{
use HasFactory;

public $timestamps = true;

public $table = 'payment_profiles';
Expand Down
Loading

0 comments on commit d909cee

Please sign in to comment.