Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "altapay/craftcms-altapay",
"description": "Altapay gateway integration for Craft Commerce 5",
"description": "AltaPay gateway integration for Craft Commerce 5",
"type": "craft-plugin",
"version": "5.0.0",
"keywords": [
Expand All @@ -25,7 +25,7 @@
"authors": [
{
"name": "AltaPay A/S",
"homepage": "https://www.altapay.com/"
"homepage": "https://www.altapay.com/"
}
],
"require": {
Expand All @@ -39,10 +39,8 @@
}
},
"extra": {
"name": "Altapay for Craft Commerce",
"name": "Altapay",
"handle": "craftcms-altapay",
"class": "QD\\altapay\\Altapay",
"hasCpSettings": false,
"hasCpSection": false
"class": "QD\\altapay\\Altapay"
}
}
20 changes: 16 additions & 4 deletions src/Altapay.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@

class Altapay extends Plugin
{
// Use
use Routes;
use Events;

// Settings
public static $plugin;
public string $schemaVersion = "5.0.0";
public bool $hasCpSettings = true;
public bool $hasCpSection = false;

use Routes;
use Events;
// Hooks
const HOOK_SUBSCRIPTION_AGREEMENT = 'beforeSubscriptionAgreement';
const HOOK_RECURRING_CHARGE = 'beforeRecurringCharge';

// Events
const EVENT_RECURRING_CHARGE = 'afterRecurringCharge';
const EVENT_SUBSCRIPTION_CREATED = 'afterSubscriptionCreated';
const EVENT_PAYMENT_AUTHORIZATION = 'afterPaymentAuthorization';
const EVENT_PAYMENT_CAPTURE = 'afterPaymentCapture';

public function init()
{
parent::init();
Craft::setAlias('@QD/altapay', __DIR__);

self::$plugin = $this;

$this->routes();
$this->events();

self::$plugin = $this;
}

protected function createSettingsModel(): ?Model
Expand Down
6 changes: 3 additions & 3 deletions src/api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ private static function _code($array)

private static function _message($array)
{
$header = $array['Header']['ErrorMessage'] ?? null;
if ($header) return implode(', ', $header);
$header = $array['Header']['ErrorMessage'] ?? [];
if ($header) return Utils::stringify($header);

$merchant = $array['Body']['MerchantErrorMessage'] ?? null;
if ($merchant) return $merchant;
if ($merchant) return Utils::stringify($merchant);

return 'Unknown';
}
Expand Down
19 changes: 10 additions & 9 deletions src/api/SubscriptionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

class SubscriptionApi extends Api
{
public static function chargeSubscription()
public function __construct()
{
return 'Not implemented';
parent::__construct();
}
public static function reserveSubscriptionCharge()
{
return 'Not implemented';
}
public static function createSubscription(array $payload, $type = 'subscription')

public static function chargeSubscription(array $payload): ApiResponse
{
$payload['type'] = $type;
return PaymentApi::createPaymentRequest($payload);
$response = (new Api())
->setMethod('chargeSubscription')
->setPayload($payload)
->post();

return $response;
}
}
11 changes: 11 additions & 0 deletions src/config/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ abstract class Data
const PAYMENT_REQUEST_TYPE_SUBSCRIPTION = 'subscription';
const PAYMENT_REQUEST_TYPE_SUBSCRIPTION_CHARGE = 'subscriptionAndCharge';
const PAYMENT_REQUEST_TYPE_SUBSCRIPTION_RESERVE = 'subscriptionAndReserve';

const AGREEMENT_TYPE_UNSCHEDULED = 'unscheduled';
const AGREEMENT_TYPE_RECURRING = 'recurring';
const AGREEMENT_TYPE_INSTALMENT = 'instalment';

const AGREEMENT_UNSCHEDULED_INCREMENTAL = 'incremental';
const AGREEMENT_UNSCHEDULED_RESUBMISSION = 'resubmission';
const AGREEMENT_UNSCHEDULED_DELAYED_CHARGES = 'delayedCharges';
const AGREEMENT_UNSCHEDULED_REAUTHORISATION = 'reauthorisation';
const AGREEMENT_UNSCHEDULED_NO_SHOW = 'noShow';
const AGREEMENT_UNSCHEDULED_CHARGE = 'charge';
}
2 changes: 1 addition & 1 deletion src/config/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function global(): void
Gateways::EVENT_REGISTER_GATEWAY_TYPES,
function (RegisterComponentTypesEvent $event) {
$event->types[] = PaymentGateway::class;
// $event->types[] = SubscriptionGateway::class;
$event->types[] = SubscriptionGateway::class;
}
);

Expand Down
3 changes: 3 additions & 0 deletions src/config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ private function publicRoutes(): void
$event->rules['callback/v1/altapay/payment/fail'] = 'craftcms-altapay/payment-callback/fail';
$event->rules['callback/v1/altapay/payment/open'] = 'craftcms-altapay/payment-callback/open';
$event->rules['callback/v1/altapay/payment/notification'] = 'craftcms-altapay/payment-callback/notification';

$event->rules['callback/v1/altapay/recurring/ok'] = 'craftcms-altapay/recurring-callback/ok';
$event->rules['callback/v1/altapay/recurring/fail'] = 'craftcms-altapay/recurring-callback/fail';
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/config/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

class Utils
{
public static function stringify($data): string
{
if (is_string($data)) return $data;
if (is_array($data)) return json_encode($data);
if (is_object($data)) return json_encode($data);

return '';
}


public static function objectify($data): object|array|string
{
if (is_object($data)) {
Expand Down
73 changes: 73 additions & 0 deletions src/controllers/RecurringCallbackController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace QD\altapay\controllers;

use Craft;
use craft\web\Controller;
use QD\altapay\config\Data;
use QD\altapay\config\Utils;
use QD\altapay\domains\payment\CaptureCallbackService;
use Exception;
use Throwable;

class RecurringCallbackController extends Controller
{
public $enableCsrfValidation = false;
protected array|bool|int $allowAnonymous = [
'ok' => self::ALLOW_ANONYMOUS_LIVE | self::ALLOW_ANONYMOUS_OFFLINE,
'fail' => self::ALLOW_ANONYMOUS_LIVE | self::ALLOW_ANONYMOUS_OFFLINE,
];


// callback_ok
public function actionOk()
{
try {
$response = $this->_response();
$this->_validate($response);
CaptureCallbackService::callback(Data::CALLBACK_OK, $response);
} catch (\Throwable $th) {
throw new Exception($th->getMessage(), 1);
}
}

// callback_fail
public function actionFail()
{
try {
$response = $this->_response();
$this->_validate($response);
CaptureCallbackService::callback(Data::CALLBACK_FAIL, $response);
} catch (\Throwable $th) {
throw new Exception($th->getMessage(), 1);
}
}

private function _response()
{
$request = Craft::$app->getRequest()->getBodyParams();

$xml = simplexml_load_string($request['xml'], 'SimpleXMLElement', LIBXML_NOCDATA);
if ($xml === false) throw new Exception('Failed to parse XML response', 1);
unset($request['xml']);

$meta = json_decode(json_encode($xml), true);
unset($meta['@attributes']);

$response = Utils::objectify($request);
$response->meta = Utils::objectify($meta);

return $response;
}

private function _validate($response)
{
if (!$response->data->CaptureAmount) {
throw new Exception('Invalid response: Missing capture arguments', 1);
}

if (!$response->data->Transactions->Transaction[0]->PaymentId) {
throw new Exception('Invalid response: Missing payment reference', 1);
}
}
}
2 changes: 1 addition & 1 deletion src/domains/gateways/PaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PaymentGateway extends Gateway

public static function displayName(): string
{
return Craft::t('commerce', 'Altapay Payment');
return Craft::t('commerce', 'AltaPay Payment');
}

//* Authorize
Expand Down
Loading