Skip to content

Commit

Permalink
chore: update test scripts for improved coverage
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 Jan 2, 2025
1 parent 929a6b1 commit b283093
Show file tree
Hide file tree
Showing 31 changed files with 2,161 additions and 875 deletions.
38 changes: 9 additions & 29 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ parameters:
path: src/Classes/BasePaymentGateway.php

-
message: "#^Method Igniter\\\\PayRegister\\\\Classes\\\\BasePaymentGateway\\:\\:initialize\\(\\) should return array but return statement is missing\\.$#"
message: "#^Method Igniter\\\\PayRegister\\\\Classes\\\\BasePaymentGateway\\:\\:getPaymentFormViewName\\(\\) invoked with 1 parameter, 0 required\\.$#"
count: 1
path: src/Classes/BasePaymentGateway.php

-
message: "#^Access to an undefined property Igniter\\\\PayRegister\\\\Classes\\\\PayPalClient\\:\\:\\$config\\.$#"
message: "#^Method Igniter\\\\PayRegister\\\\Classes\\\\BasePaymentGateway\\:\\:initialize\\(\\) should return array but return statement is missing\\.$#"
count: 1
path: src/Classes/PayPalClient.php
path: src/Classes/BasePaymentGateway.php

-
message: "#^Call to an undefined method Illuminate\\\\Contracts\\\\View\\\\Factory\\:\\:getFinder\\(\\)\\.$#"
Expand Down Expand Up @@ -155,6 +155,11 @@ parameters:
count: 2
path: src/Models/Observers/PaymentObserver.php

-
message: "#^Access to an undefined property Igniter\\\\PayRegister\\\\Models\\\\PaymentProfile\\:\\:\\$is_primary\\.$#"
count: 1
path: src/Models/Observers/PaymentProfileObserver.php

-
message: "#^Access to an undefined property Igniter\\\\PayRegister\\\\Models\\\\Payment\\:\\:\\$class_name\\.$#"
count: 4
Expand All @@ -165,11 +170,6 @@ parameters:
count: 1
path: src/Models/Payment.php

-
message: "#^Access to an undefined property Igniter\\\\PayRegister\\\\Models\\\\Payment\\:\\:\\$model\\.$#"
count: 1
path: src/Models/Payment.php

-
message: "#^Access to an undefined property Igniter\\\\PayRegister\\\\Models\\\\Payment\\:\\:\\$payment_id\\.$#"
count: 2
Expand All @@ -190,21 +190,11 @@ parameters:
count: 2
path: src/Models/Payment.php

-
message: "#^Call to an undefined method Igniter\\\\PayRegister\\\\Models\\\\Payment\\:\\:beforeRenderPaymentForm\\(\\)\\.$#"
count: 1
path: src/Models/Payment.php

-
message: "#^Call to an undefined method Igniter\\\\PayRegister\\\\Models\\\\Payment\\:\\:getConfigFields\\(\\)\\.$#"
count: 1
path: src/Models/Payment.php

-
message: "#^Call to an undefined method Igniter\\\\PayRegister\\\\Models\\\\Payment\\:\\:getPaymentFormViewName\\(\\)\\.$#"
count: 1
path: src/Models/Payment.php

-
message: "#^Call to an undefined method Igniter\\\\PayRegister\\\\Models\\\\Payment\\:\\:whereIsEnabled\\(\\)\\.$#"
count: 1
Expand Down Expand Up @@ -290,11 +280,6 @@ parameters:
count: 2
path: src/Models/PaymentProfile.php

-
message: "#^Access to an undefined property Igniter\\\\PayRegister\\\\Models\\\\PaymentProfile\\:\\:\\$is_primary\\.$#"
count: 1
path: src/Models/PaymentProfile.php

-
message: "#^Access to an undefined property Igniter\\\\PayRegister\\\\Models\\\\PaymentProfile\\:\\:\\$payment_profile_id\\.$#"
count: 1
Expand Down Expand Up @@ -617,19 +602,14 @@ parameters:

-
message: "#^Call to an undefined method Igniter\\\\Flame\\\\Database\\\\Model\\:\\:initPaymentProfile\\(\\)\\.$#"
count: 3
count: 2
path: src/Payments/Stripe.php

-
message: "#^Call to an undefined static method Igniter\\\\Cart\\\\Models\\\\Order\\:\\:find\\(\\)\\.$#"
count: 1
path: src/Payments/Stripe.php

-
message: "#^Call to an undefined static method Illuminate\\\\Support\\\\Facades\\\\Event\\:\\:fire\\(\\)\\.$#"
count: 1
path: src/Payments/Stripe.php

-
message: "#^Method Igniter\\\\PayRegister\\\\Payments\\\\Stripe\\:\\:processPaymentForm\\(\\) should return bool\\|Illuminate\\\\Http\\\\RedirectResponse but return statement is missing\\.$#"
count: 1
Expand Down
65 changes: 50 additions & 15 deletions src/Classes/AuthorizeNetClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
namespace Igniter\PayRegister\Classes;

use Igniter\Flame\Exception\ApplicationException;
use net\authorize\api\constants\ANetEnvironment;
use net\authorize\api\contract\v1\ANetApiResponseType;
use net\authorize\api\contract\v1\CreateTransactionRequest;
use net\authorize\api\contract\v1\CreditCardType;
use net\authorize\api\contract\v1\MerchantAuthenticationType;
use net\authorize\api\contract\v1\OpaqueDataType;
use net\authorize\api\contract\v1\PaymentType;
use net\authorize\api\contract\v1\TransactionRequestType;
use net\authorize\api\contract\v1\TransactionResponseType;
use net\authorize\api\controller\CreateTransactionController;

class AuthorizeNetClient
{
protected bool $sandbox = true;

protected ?MerchantAuthenticationType $authentication = null;

protected ?CreateTransactionRequest $transactionRequest = null;
public function setTestMode(bool $sandbox = true)
{
$this->sandbox = $sandbox;

public function __construct(protected bool $sandbox = false) {}
return $this;
}

public function authentication()
{
Expand All @@ -26,23 +34,50 @@ public function authentication()
return $this->authentication = new MerchantAuthenticationType;
}

public function createTransactionRequest(): CreateTransactionRequest
public function createTransactionRequest(array $fields = []): AuthorizeNetTransactionRequest
{
if ($this->transactionRequest) {
return $this->transactionRequest;
$paymentOne = new PaymentType;
$transactionRequestType = new TransactionRequestType;

if (array_has($fields, ['opaqueDataDescriptor', 'opaqueDataValue'])) {
$opaqueData = new OpaqueDataType;
$opaqueData->setDataDescriptor($fields['opaqueDataDescriptor']);
$opaqueData->setDataValue($fields['opaqueDataValue']);
$paymentOne->setOpaqueData($opaqueData);
}

if (array_has($fields, ['cardNumber', 'expirationDate'])) {
$creditCard = new CreditCardType;
$creditCard->setCardNumber($fields['cardNumber']);
$creditCard->setExpirationDate($fields['expirationDate']);
$paymentOne = new PaymentType;
$paymentOne->setCreditCard($creditCard);
}

$transactionRequestType->setPayment($paymentOne);
$transactionRequestType->setTransactionType($fields['transactionType'] ?? '');
if ($amount = array_get($fields, 'amount')) {
$transactionRequestType->setAmount($amount);
}

if ($transactionId = array_get($fields, 'transactionId')) {
$transactionRequestType->setRefTransId($transactionId);
}

$request = new CreateTransactionRequest;
$request = new AuthorizeNetTransactionRequest;
$request->setMerchantAuthentication($this->authentication());

return $this->transactionRequest = $request;
$request->setRefId($fields['refId'] ?? '');
$request->setTransactionRequest($transactionRequestType);

return $request;
}

public function createTransaction(CreateTransactionController $controller): TransactionResponseType
public function createTransaction(AuthorizeNetTransactionRequest $request): TransactionResponseType
{
$response = $controller->executeWithApiResponse($this->sandbox
? \net\authorize\api\constants\ANetEnvironment::SANDBOX
: \net\authorize\api\constants\ANetEnvironment::PRODUCTION);
$response = $request->controller()->executeWithApiResponse(
$this->sandbox ? ANetEnvironment::SANDBOX : ANetEnvironment::PRODUCTION,
);

throw_if(is_null($response), new ApplicationException('No response returned'));

Expand All @@ -53,15 +88,15 @@ public function createTransaction(CreateTransactionController $controller): Tran
}

if (is_null($transactionResponse) || is_null($transactionResponse->getMessages())) {
throw new ApplicationException($this->getErrorMessageFromResponse($response, $transactionResponse));
throw new ApplicationException('Transaction failed with empty message');
}

return $transactionResponse;
}

protected function getErrorMessageFromResponse(?AnetApiResponseType $response, ?TransactionResponseType $transactionResponse): string
{
$message = "Transaction Failed \n Error Code : %s \n Error Message : %s \n";
$message = "Transaction Failed \n Error Code : %s \n Error Message : %s \n";
if ($transactionResponse != null && $transactionResponse->getErrors() != null) {
return sprintf($message,
$transactionResponse->getErrors()[0]->getErrorCode(),
Expand Down
16 changes: 16 additions & 0 deletions src/Classes/AuthorizeNetTransactionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Igniter\PayRegister\Classes;

use net\authorize\api\contract\v1\CreateTransactionRequest;
use net\authorize\api\controller\CreateTransactionController;

class AuthorizeNetTransactionRequest extends CreateTransactionRequest
{
protected ?CreateTransactionController $controller = null;

public function controller(): CreateTransactionController
{
return $this->controller ??= new CreateTransactionController($this);
}
}
14 changes: 9 additions & 5 deletions src/Classes/BasePaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ public function completesPaymentOnClient()
return false;
}

protected function validatePaymentMethod($order, $host)
{
$this->validateApplicableFee($order, $host);
}

/**
* Processes payment using passed data.
*
Expand All @@ -170,6 +165,15 @@ public function processPaymentForm($data, $host, $order)
*/
public function beforeRenderPaymentForm($host, $controller) {}

public function renderPaymentForm()
{
$this->beforeRenderPaymentForm($this->model, controller());

$viewName = $this->getPaymentFormViewName($this);

return view($viewName, ['paymentMethod' => $this->model]);
}

public function getPaymentFormViewName()
{
$themeCode = resolve(ThemeManager::class)->getActiveThemeCode();
Expand Down
44 changes: 25 additions & 19 deletions src/Classes/PayPalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@

class PayPalClient
{
public function __construct(
protected ?string $clientId,
protected ?string $clientSecret,
protected bool $sandbox
) {
throw_unless($this->clientId, ApplicationException::class, 'PayPal client ID is not configured');
throw_unless($this->clientSecret, ApplicationException::class, 'PayPal client secret is not configured');
protected ?string $clientId = null;
protected ?string $clientSecret = null;
protected bool $sandbox = false;

public function setClientId(?string $clientId): PayPalClient
{
$this->clientId = $clientId;
return $this;
}

public function setClientSecret(?string $clientSecret): PayPalClient
{
$this->clientSecret = $clientSecret;
return $this;
}

public function setSandbox(bool $sandbox): PayPalClient
{
$this->sandbox = $sandbox;
return $this;
}

public function getOrder($orderId)
Expand Down Expand Up @@ -61,6 +74,9 @@ public function refundPayment($id, $params)

protected function generateAccessToken()
{
throw_unless($this->clientId, ApplicationException::class, 'PayPal client ID is not configured');
throw_unless($this->clientSecret, ApplicationException::class, 'PayPal client secret is not configured');

if (!cache()->has('payregister_paypal_access_token')) {
$response = Http::asForm()
->withBasicAuth($this->clientId, $this->clientSecret)
Expand All @@ -80,9 +96,8 @@ protected function generateAccessToken()

protected function endpoint(string $uri)
{
$endpoint = app()->environment('production')
? 'https://api-m.paypal.com/'
: 'https://api-m.sandbox.paypal.com/';
$endpoint = 'https://';
$endpoint .= app()->environment('production') ? 'api-m.paypal.com/' : 'api-m.sandbox.paypal.com/';

return $endpoint.$uri;
}
Expand All @@ -94,13 +109,4 @@ protected function prepareHeaders(): array
'PayPal-Request-Id' => Str::uuid()->toString(),
];
}

public function setTestMode(bool $isSandboxMode)
{
$this->config['sandbox'] = $isSandboxMode;

return $this;
}

public function setBrandName(mixed $setting) {}
}
16 changes: 6 additions & 10 deletions src/Classes/PaymentGateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,16 @@ public function listGateways()
$this->loadGateways();
}

if (!is_array($this->gateways)) {
return [];
}
!is_array($this->gateways) && $this->gateways = [];

$result = [];
foreach ($this->gateways as $gateway) {
if (!class_exists($gateway['class'])) {
continue;
if (class_exists($gateway['class'])) {
$gatewayObj = new $gateway['class'];
$result[$gateway['code']] = array_merge($gateway, [
'object' => $gatewayObj,
]);
}

$gatewayObj = new $gateway['class'];
$result[$gateway['code']] = array_merge($gateway, [
'object' => $gatewayObj,
]);
}

return $this->gateways = $result;
Expand Down
5 changes: 5 additions & 0 deletions src/Concerns/WithApplicableFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

trait WithApplicableFee
{
protected function validatePaymentMethod($order, $host)
{
$this->validateApplicableFee($order, $host);
}

protected function validateApplicableFee(Order $order, ?Payment $host = null)
{
$host = is_null($host) ? $this->model : $host;
Expand Down
11 changes: 11 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

namespace Igniter\PayRegister;

use Igniter\PayRegister\Classes\AuthorizeNetClient;
use Igniter\PayRegister\Classes\PaymentGateways;
use Igniter\PayRegister\Classes\PayPalClient;
use Igniter\PayRegister\Listeners\CaptureAuthorizedPayment;
use Igniter\PayRegister\Listeners\UpdatePaymentIntentSessionOnCheckout;
use Igniter\PayRegister\Models\Observers\PaymentObserver;
use Igniter\PayRegister\Models\Observers\PaymentProfileObserver;
use Igniter\PayRegister\Models\Payment;
use Igniter\PayRegister\Models\PaymentProfile;
use Igniter\PayRegister\Subscribers\FormFieldsSubscriber;
use Igniter\System\Classes\BaseExtension;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Event;
use Mollie\Api\MollieApiClient;
use Square\SquareClientBuilder;

class Extension extends BaseExtension
{
Expand All @@ -29,10 +35,15 @@ class Extension extends BaseExtension

protected $observers = [
Payment::class => PaymentObserver::class,
PaymentProfile::class => PaymentProfileObserver::class,
];

public array $singletons = [
AuthorizeNetClient::class,
MollieApiClient::class,
PaymentGateways::class,
PayPalClient::class,
SquareClientBuilder::class,
];

public function registerPaymentGateways(): array
Expand Down
Loading

0 comments on commit b283093

Please sign in to comment.