Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wgorczyca committed Mar 21, 2024
0 parents commit 3c75f59
Show file tree
Hide file tree
Showing 45 changed files with 2,238 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ps_root_dir/
vendor/

composer.lock
.php-cs-fixer.php
.php-cs-fixer.cache
25 changes: 25 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__,
])
->exclude([
'vendor',
'ps_root_dir',
]);

$config = new PhpCsFixer\Config();

return $config
->setRules([
'@PER-CS2.0' => true,
'declare_strict_types' => true,
'strict_param' => true,
'concat_space' => ['spacing' => 'one'],
'protected_to_private' => true,
'nullable_type_declaration_for_default_null_value' => true,
'final_class' => true,
])
->setRiskyAllowed(true)
->setFinder($finder);
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 SimPay.pl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fixer:
vendor/bin/php-cs-fixer fix

phpstan:
vendor/bin/phpstan analyse

sa: fixer phpstan
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Simpay Prestashop
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"license": "MIT",
"name": "simpaypl/prestashop",
"type": "prestashop-module",
"description": "Module for integration with SimPay payment gateway",
"keywords": [
"simpay",
"prestashop",
"payment",
"gateway"
],
"authors": [
{
"name": "SimPay Core Team"
}
],
"autoload": {
"psr-4": {
"SimPaypl\\PrestaShop\\": "src/"
}
},
"require": {
"php": "^8.1",
"simpaypl/simpay": "^3.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"prestashop/php-dev-tools": "^4.3"
},
"config": {
"preferred-install": "dist",
"prepend-autoloader": false,
"sort-packages": true
}
}
12 changes: 12 additions & 0 deletions config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>simpay</name>
<displayName><![CDATA[SimPay]]></displayName>
<version><![CDATA[1.0.0]]></version>
<description><![CDATA[Accept payments by SimPay payment gateway for easy and secure payments.]]></description>
<author><![CDATA[Payments Solution Sp. z o.o.]]></author>
<tab><![CDATA[payments_gateways]]></tab>
<confirmUninstall><![CDATA[Are you sure you want to uninstall?]]></confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
</module>
11 changes: 11 additions & 0 deletions config/admin/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
36 changes: 36 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
_defaults:
public: true

SimPaypl\PrestaShop\Controller\SimpayConfigurationAdminController:
arguments:
- '@prestashop.module.simpay.admin.form.simpay_form_data_handler'
tags:
- { name: 'controller.service_arguments' }

# Demo configuration text form
prestashop.module.simpay.admin.form.simpay_form_type:
class: 'SimPaypl\PrestaShop\Form\SimpayFormType'
parent: 'form.type.translatable.aware'
public: true
tags:
- { name: form.type }

prestashop.module.simpay.admin.form.simpay_data_configuration:
class: 'SimPaypl\PrestaShop\Form\SimpayDataConfiguration'
arguments:
- '@prestashop.adapter.legacy.configuration'

prestashop.module.simpay.admin.form.simpay_form_data_provider:
class: 'SimPaypl\PrestaShop\Form\SimpayFormDataProvider'
arguments:
- '@prestashop.module.simpay.admin.form.simpay_data_configuration'

prestashop.module.simpay.admin.form.simpay_form_data_handler:
class: 'PrestaShop\PrestaShop\Core\Form\Handler'
arguments:
- '@form.factory'
- '@prestashop.core.hook.dispatcher'
- '@prestashop.module.simpay.admin.form.simpay_form_data_provider'
- 'SimPaypl\PrestaShop\Form\SimpayFormType'
- 'SimpayConfiguration'
11 changes: 11 additions & 0 deletions config/front/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
12 changes: 12 additions & 0 deletions config/front/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
_defaults:
public: true

prestashop.module.simpay.front.payment_client_factory:
class: 'SimPaypl\PrestaShop\PaymentClientFactory'
arguments:
- '@prestashop.adapter.legacy.configuration'

prestashop.module.simpay.front.payment_client:
class: 'Simpay\PaymentInterface'
factory: '@prestashop.module.simpay.front.payment_client_factory'
11 changes: 11 additions & 0 deletions config/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
8 changes: 8 additions & 0 deletions config/routes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
simpay_configuration:
path: /simpay/configuration
methods: [GET, POST]
defaults:
_controller: SimPaypl\PrestaShop\Controller\SimpayConfigurationAdminController::configureAction
# Needed to work with tab system
_legacy_controller: 'SimpayConfigurationAdminController'
_legacy_link: 'SimpayConfigurationAdminController'
15 changes: 15 additions & 0 deletions controllers/front/failed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

final class SimpayFailedModuleFrontController extends ModuleFrontController
{
/** @var Simpay */
public $module;

public function initContent(): void
{
parent::initContent();
$this->setTemplate('module:simpay/views/templates/front/failed.tpl');
}
}
11 changes: 11 additions & 0 deletions controllers/front/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
101 changes: 101 additions & 0 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

use Simpay\Model\Request\Amount;
use Simpay\Model\Request\CallbackReturnUrl;
use Simpay\Model\Request\CreatePayment;
use Simpay\Model\Request\ServiceId;
use Simpay\Model\Request\Currency;
use Simpay\PaymentInterface;

final class SimpayNotifyModuleFrontController extends ModuleFrontController
{
/** @var Simpay */
public $module;

public function init(): void
{
parent::init();
// validate module is enabled and configured correctly
}

public function postProcess(): void
{
$jsonPayload = Tools::file_get_contents('php://input');

if (!is_string($jsonPayload)) {
http_response_code(400);
header('Content-Type: application/json');
die(json_encode(['status' => 'error', 'message' => 'Invalid payload, expected JSON']));
}

/** @var array{
* id: string,
* service_id: string,
* status: string,
* amount: array{
* value: float,
* currency: string,
* commission: float,
* },
* control: string,
* channel: string,
* environment: string,
* signature: string
* }|bool|null $payload
*/
$payload = json_decode($jsonPayload, true);

if (!is_array($payload)) {
http_response_code(400);
header('Content-Type: application/json');
die(json_encode(['status' => 'error', 'message' => 'Invalid payload, expected JSON']));
}

$signatureValues = [];
array_walk_recursive($payload, function (mixed $item, string $key) use (&$signatureValues) {
if ($key === 'signature') {
return;
}

$signatureValues[] = (string) $item;
});

$signatureValues[] = Configuration::get('SIMPAY_SERVICE_IPN_SIGNATURE_KEY');
$signatureValues = implode('|', $signatureValues);
$signatureHash = hash('sha256', $signatureValues);

if ($payload['signature'] !== $signatureHash) {
http_response_code(400);
header('Content-Type: application/json');
die(json_encode(['status' => 'error', 'message' => 'Invalid signature']));
}

$order = Order::getByCartId((int) $payload['control']);

if (null === $order) {
http_response_code(400);
header('Content-Type: application/json');
die(json_encode(['status' => 'error', 'message' => 'Order not found']));
}

if ((int) Configuration::get(Simpay::CONFIG_OS_AWAITING) === $order->getCurrentState()) {

$orderStatusId = (int) match($payload['status']) {
'transaction_paid' => Configuration::get('PS_OS_PAYMENT'),
default => Configuration::get('PS_OS_ERROR'),
};

$orderHistory = new OrderHistory();
$orderHistory->id_order = (int) $order->id;
$orderHistory->changeIdOrderState($orderStatusId, (int) $order->id, true);
$orderHistory->save();
}


http_response_code(200);
header('Content-Type: application/json');
die(json_encode(['status' => 'ok']));
}
}
Loading

0 comments on commit 3c75f59

Please sign in to comment.