Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
firstred committed Feb 16, 2018
1 parent d33d55e commit 1e22c6e
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 350 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vendor/
composer.lock
*omnipaymollie*.zip
*OmnipayWorldpay*.zip
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Mollie Omnipay module
# Worldpay Omnipay module

This module integrates Mollie in thirty bees with the help of the Omnipay library.
This module integrates Worldpay in thirty bees with the help of the Omnipay library.

## Building the module

In order to prevent library conflicts in thirty bees this module's dependencies need to be "scoped".
For this we use PHP Scoper (https://github.com/humbug/php-scoper) in the build script.
By default everything in the vendor folder is now scoped to `ThirtyBeesMollie`.
By default everything in the vendor folder is now scoped to `ThirtyBeesWorldpay`.

You can build the module by running the build script:
```shell
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rm build/ -rf
# Composer install and scoping
composer install --no-dev --prefer-dist
mv vendor/ pre-scoper/
php ./php-scoper.phar add-prefix -p ThirtyBeesMollie -n
php ./php-scoper.phar add-prefix -p ThirtyBeesWorldpay -n

# Cleanup
mv build/pre-scoper/ vendor/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"omnipay/mollie": "^3.2"
"omnipay/worldpay": "~2.0"
},
"autoload": {
"classmap": [
Expand Down
91 changes: 0 additions & 91 deletions controllers/front/payment.php

This file was deleted.

177 changes: 86 additions & 91 deletions controllers/front/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,129 +12,124 @@
* obtain it through the world-wide-web, please send an email
* to license@thirtybees.com so we can send you a copy immediately.
*
* @author thirty bees <modules@thirtybees.com>
* @copyright 2017-2018 thirty bees
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* @author thirty bees <modules@thirtybees.com>
* @copyright 2017-2018 thirty bees
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/

use ThirtyBeesMollie\Omnipay\Omnipay;
use ThirtyBeesWorldpay\Omnipay\Omnipay;

if (!defined('_TB_VERSION_')) {
exit;
}

/**
* Class OmnipayMollieValidationModuleFrontController
* Class OmnipayWorldpayValidationModuleFrontController
*/
class OmnipayMollieValidationModuleFrontController extends ModuleFrontController
class OmnipayWorldpayValidationModuleFrontController extends ModuleFrontController
{
/** @var bool $display_column_left */
public $display_column_left = false;
/** @var bool $display_column_right */
public $display_column_right = false;
/** @var OmnipayMollie $module */
/** @var OmnipayWorldpay $module */
public $module;

/**
* OmnipayMollieValidationModuleFrontController constructor.
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function __construct()
{
parent::__construct();

$this->ssl = Tools::usingSecureMode();
}

/**
* Post process
*
* @return void
*
* @throws PrestaShopException
*/
public function postProcess()
public function initContent()
{
if (!$this->module->active || strtoupper($this->context->currency->iso_code) !== 'EUR') {
Tools::redirect($this->context->link->getPageLink('order', null, null, ['step' => 3]));
}
$cart = $this->context->cart;
$cookie = new Cookie($this->module->name);
if ($cart->id_customer == 0
|| $cart->id_address_delivery == 0
|| $cart->id_address_invoice == 0
|| !$this->module->active
|| strtoupper($this->context->currency->iso_code) !== 'EUR'
) {
Tools::redirect('index.php?controller=order&step=3');
if (!$cart->id_customer || !$cart->id_address_delivery || !$cart->id_address_invoice || !$this->module->active) {
Tools::redirect($this->context->link->getPageLink('order', null, null, ['step' => 3]));
}

$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
Tools::redirect('index.php?controller=order&step=3');
Tools::redirect($this->context->link->getPageLink('order', null, null, ['step' => 3]));
}

$orderProcess = Configuration::get('PS_ORDER_PROCESS_TYPE') ? 'order-opc' : 'order';
$this->context->smarty->assign([
'orderLink' => $this->context->link->getPageLink($orderProcess, true),
]);

if ((float) $cart->getOrderTotal() !== (float) $cookie->amount) {
Tools::redirect('index.php?controller=order&step=3');
}
parent::initContent();

/** @var Omnipay $gateway */
$gateway = Omnipay::create(OmnipayMollie::GATEWAY_NAME);
/** @var \ThirtyBeesWorldpay\Omnipay\Worldpay\Gateway $gateway */
$gateway = Omnipay::create(OmnipayWorldpay::GATEWAY_NAME);
foreach (array_keys($gateway->getDefaultParameters()) as $key) {
$gateway->{'set'.ucfirst($key)}(Configuration::get(OmnipayMollie::CREDENTIAL.$key));
$gateway->{'set'.ucfirst($key)}(Configuration::get(OmnipayWorldpay::CREDENTIAL.$key));
}

$params = Tools::getAllValues();
$params['transactionReference'] = $cookie->transaction_reference;
try {
/** @var \ThirtyBeesMollie\Omnipay\Mollie\Message\CompletePurchaseResponse $response */
$response = $gateway->completePurchase($params)->send();
if (!$response->isSuccessful()) {
$error = $this->module->l('An error occurred. Please contact us for more information.', 'validation');
$this->errors[] = $error;
$this->setTemplate('error.tpl');

return;
}
} catch (Exception $e) {
if (!empty(_PS_MODE_DEV_)) {
$error = sprintf($this->module->l('An error occurred: %s', 'validation'), $e->getMessage());
} else {
$error = $this->module->l('An unknown error occurred. Please contact us for more information.', 'validation');
}
$this->errors[] = $error;
$this->setTemplate('error.tpl');
$billingAddress = new Address($cart->id_address_invoice);
$shippingAddress = new Address($cart->id_address_delivery);

$card = new \ThirtyBeesWorldpay\Omnipay\Common\CreditCard([
'name' => "{$customer->firstname} {$customer->lastname}",
'email' => $customer->email,
'billingAddress1' => $billingAddress->address1,
'billingAddress2' => $billingAddress->address2,
'billingPostcode' => $billingAddress->postcode,
'billingCity' => $billingAddress->city,
'billingState' => $billingAddress->id_state ? State::getNameById($billingAddress->id_state) : '',
'billingCountry' => Country::getIsoById($billingAddress->id_country),
'billingPhone' => $billingAddress->phone ?: $billingAddress->phone_mobile,
'shippingAddress1' => $shippingAddress->address1,
'shippingAddress2' => $shippingAddress->address2,
'shippingPostcode' => $shippingAddress->postcode,
'shippingCity' => $shippingAddress->city,
'shippingState' => $shippingAddress->id_state ? State::getNameById($shippingAddress->id_state) : '',
'shippingCountry' => Country::getIsoById($shippingAddress->id_country),
'shippingPhone' => $shippingAddress->phone ?: $shippingAddress->phone_mobile,
]);

return;
}
/** @var \ThirtyBeesWorldpay\Omnipay\Worldpay\Message\PurchaseResponse $response */
$response = $gateway->purchase(
[
'token' => Tools::getValue('token'),
'amount' => number_format($this->context->cart->getOrderTotal(), 2),
'currency' => strtoupper($this->context->currency->iso_code),
'description' => $this->context->cart->id,
'transactionId' => $this->context->cart->id,
'name' => 'test',
'card' => $card,

]
)->send();

if ($response->isSuccessful()) {
$this->module->validateOrder(
(int) $cart->id,
Configuration::get('PS_OS_PAYMENT'),
(float) $cart->getOrderTotal(),
$this->module->displayName,
null,
[],
(int) $cart->id_currency,
false,
$cart->secure_key
);

$this->module->validateOrder(
(int) $cart->id,
Configuration::get('PS_OS_PAYMENT'),
(float) $cart->getOrderTotal(),
$this->module->displayName,
null,
[],
(int) $cart->id_currency,
false,
$cart->secure_key
);

/**
* If the order has been validated we try to retrieve it
*/
$idOrder = Order::getOrderByCartId((int) $cart->id);

if ($idOrder) {
/**
* The order has been placed so we redirect the customer on the confirmation page.
* If the order has been validated we try to retrieve it
*/
Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id
.'&id_module='.$this->module->id.'&id_order='.$idOrder.'&key='.$customer->secure_key);
$idOrder = Order::getOrderByCartId((int) $cart->id);

if ($idOrder) {
/**
* The order has been placed so we redirect the customer on the confirmation page.
*/
Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id
.'&id_module='.$this->module->id.'&id_order='.$idOrder.'&key='.$customer->secure_key);
}
} else {
if (_PS_MODE_DEV_) {
$this->context->controller->errors[] = $response->getMessage();
} else {
$this->context->controller->errors[] = $this->module->l('An unknown error occurred.', 'payment');
}

$this->context->smarty->assign([
'orderLink' => $this->context->link->getPageLink('order', null, null, ['step' => 3]),
]);

/**
* An error occurred and is shown on a new page.
*/
Expand Down
Binary file modified logo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1e22c6e

Please sign in to comment.