Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Payment] decouple Payment from Payum and consider decimal factor #1021

Merged
merged 6 commits into from
Mar 9, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace CoreShop\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\BigIntType;
use Pimcore\Migrations\Migration\AbstractPimcoreMigration;

class Version20200224164328 extends AbstractPimcoreMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->addSql(' ALTER TABLE coreshop_payment DROP client_email, DROP client_id;');

if ($schema->hasTable('coreshop_payment')) {
$table = $schema->getTable('coreshop_payment');
if ($table->hasColumn('total_amount')) {
if (!$table->getColumn('total_amount')->getType() instanceof BigIntType) {
$this->addSql('ALTER TABLE coreshop_payment CHANGE total_amount total_amount BIGINT DEFAULT NULL;');
}
}
}
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ CoreShop\Component\Core\Model\Payment:
fields:
order:
column: '`order`'
type: pimcoreObject
type: pimcoreObject
manyToOne:
currency:
targetEntity: CoreShop\Component\Currency\Model\CurrencyInterface
joinColumn:
name: currency_id
referencedColumnName: id
nullable: true
onDelete: 'SET NULL'
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ CoreShop\Component\Payment\Model\Payment:
state:
column: state
type: string
number:
column: number
type: string
nullable: true
description:
number: description
type: string
nullable: true
totalAmount:
column: total_amount
type: bigint
nullable: true
currencyCode:
column: currency_code
type: string
nullable: true
details:
column: details
type: json_array
orderId:
column: orderId
type: integer
Expand All @@ -35,10 +54,3 @@ CoreShop\Component\Payment\Model\Payment:
name: payment_provider_id
referencedColumnName: id
nullable: true
currency:
targetEntity: CoreShop\Component\Currency\Model\CurrencyInterface
joinColumn:
name: currency_id
referencedColumnName: id
nullable: true
onDelete: 'SET NULL'
1 change: 0 additions & 1 deletion src/CoreShop/Bundle/PaymentBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"require": {
"php": "^7.2",
"coreshop/payment": "^2.0",
"coreshop/currency-bundle": "^2.0",
"coreshop/resource-bundle": "^2.0",
"coreshop/workflow-bundle": "^2.0",
"pimcore/pimcore": "^6.5.3"
Expand Down
87 changes: 87 additions & 0 deletions src/CoreShop/Bundle/PayumBundle/Action/AuthorizePaymentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\PayumBundle\Action;

use CoreShop\Bundle\PayumBundle\Request\GetStatus;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Model\Payment as PayumPayment;
use Payum\Core\Request\Authorize;
use Payum\Core\Request\Convert;
use CoreShop\Component\Core\Model\PaymentInterface as CoreShopPaymentInterface;

final class AuthorizePaymentAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* @var int
*/
protected $decimalFactor;

public function __construct(int $decimalFactor)
{
$this->decimalFactor = $decimalFactor;
}

/**
* {@inheritdoc}
*
* @param Authorize $request
*/
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);
/** @var CoreShopPaymentInterface $payment */
$payment = $request->getModel();

$this->gateway->execute($status = new GetStatus($payment));
if ($status->isNew()) {
try {
$this->gateway->execute($convert = new Convert($payment, 'array', $request->getToken()));
$payment->setDetails($convert->getResult());
} catch (RequestNotSupportedException $e) {
$payumPayment = new PayumPayment();
$payumPayment->setNumber($payment->getNumber());
//Payum Payment works with ints with a precision of 2
$payumPayment->setTotalAmount(100 * round($payment->getTotalAmount() / $this->decimalFactor, 2));
$payumPayment->setCurrencyCode($payment->getCurrency()->getIsoCode());
$payumPayment->setDescription($payment->getDescription());
$payumPayment->setDetails($payment->getDetails());
$this->gateway->execute($convert = new Convert($payumPayment, 'array', $request->getToken()));
$payment->setDetails($convert->getResult());
}
}
$details = ArrayObject::ensureArrayObject($payment->getDetails());

try {
$request->setModel($details);
$this->gateway->execute($request);
} finally {
$payment->setDetails((array) $details);
}
}

/**
* {@inheritdoc}
*/
public function supports($request): bool
{
return
$request instanceof Authorize &&
$request->getModel() instanceof CoreShopPaymentInterface;
}
}
91 changes: 91 additions & 0 deletions src/CoreShop/Bundle/PayumBundle/Action/CapturePaymentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\PayumBundle\Action;

use CoreShop\Bundle\PayumBundle\Request\GetStatus;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Model\Payment as PayumPayment;
use Payum\Core\Request\Capture;
use Payum\Core\Request\Convert;
use CoreShop\Component\Core\Model\PaymentInterface as CoreShopPaymentInterface;

final class CapturePaymentAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* @var int
*/
protected $decimalFactor;

public function __construct(int $decimalFactor)
{
$this->decimalFactor = $decimalFactor;
}

/**
* {@inheritdoc}
*
* @param Capture $request
*/
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);

/** @var CoreShopPaymentInterface $payment */
$payment = $request->getModel();

$this->gateway->execute($status = new GetStatus($payment));

if ($status->isNew()) {
try {
$this->gateway->execute($convert = new Convert($payment, 'array', $request->getToken()));
$payment->setDetails($convert->getResult());
} catch (RequestNotSupportedException $e) {
$payumPayment = new PayumPayment();
$payumPayment->setNumber($payment->getNumber());
//Payum Payment works with ints with a precision of 2
$payumPayment->setTotalAmount(100 * round($payment->getTotalAmount() / $this->decimalFactor, 2));
$payumPayment->setCurrencyCode($payment->getCurrency()->getIsoCode());
$payumPayment->setDescription($payment->getDescription());
$payumPayment->setDetails($payment->getDetails());
$this->gateway->execute($convert = new Convert($payumPayment, 'array', $request->getToken()));
$payment->setDetails($convert->getResult());
}
}

$details = ArrayObject::ensureArrayObject($payment->getDetails());

try {
$request->setModel($details);
$this->gateway->execute($request);
} finally {
$payment->setDetails((array) $details);
}
}

/**
* {@inheritdoc}
*/
public function supports($request): bool
{
return
$request instanceof Capture &&
$request->getModel() instanceof CoreShopPaymentInterface
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\PayumBundle\Action;

use CoreShop\Component\Core\Model\PaymentInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Generic;

final class ExecuteSameRequestWithPaymentDetailsAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* {@inheritdoc}
*
* @param Generic $request
*/
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);

/** @var PaymentInterface $payment */
$payment = $request->getModel();
$details = ArrayObject::ensureArrayObject($payment->getDetails());

try {
$request->setModel($details);

$this->gateway->execute($request);
} finally {
$payment->setDetails((array)$details);
}
}

/**
* {@inheritdoc}
*/
public function supports($request): bool
{
return
$request instanceof Generic &&
$request->getModel() instanceof PaymentInterface;
}
}
20 changes: 20 additions & 0 deletions src/CoreShop/Bundle/PayumBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ services:
tags:
- { name: payum.action, all: true, alias: coreshop.confirm_order }

coreshop.payum_action.authorize_payment: '@CoreShop\Bundle\PayumBundle\Action\AuthorizePaymentAction'
CoreShop\Bundle\PayumBundle\Action\AuthorizePaymentAction:
arguments:
- '%coreshop.currency.decimal_factor%'
tags:
- { name: payum.action, all: true, alias: coreshop.authorize_payment }

coreshop.payum_action.capture_payment: '@CoreShop\Bundle\PayumBundle\Action\CapturePaymentAction'
CoreShop\Bundle\PayumBundle\Action\CapturePaymentAction:
arguments:
- '%coreshop.currency.decimal_factor%'
tags:
- { name: payum.action, all: true, alias: coreshop.capture_payment }

coreshop.payum_action.execute_same_request_with_payment_details: '@CoreShop\Bundle\PayumBundle\Action\ExecuteSameRequestWithPaymentDetailsAction'
CoreShop\Bundle\PayumBundle\Action\ExecuteSameRequestWithPaymentDetailsAction:
tags:
- { name: payum.action, all: true }


# Offline Payment
coreshop.payum_action.offline.convert_payment: '@CoreShop\Bundle\PayumBundle\Action\Offline\ConvertPaymentAction'
CoreShop\Bundle\PayumBundle\Action\Offline\ConvertPaymentAction:
Expand Down
22 changes: 22 additions & 0 deletions src/CoreShop/Component/Core/Model/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Payment extends BasePayment implements PaymentInterface
*/
protected $order;

/**
* @var CurrencyInterface
*/
protected $currency;

/**
* {@inheritdoc}
*/
Expand All @@ -40,4 +45,21 @@ public function setOrder(\CoreShop\Component\Order\Model\OrderInterface $order)
$this->order = $order;
$this->orderId = $order->getId();
}

/**
* {@inheritdoc}
*/
public function getCurrency()
{
return $this->currency;
}

/**
* {@inheritdoc}
*/
public function setCurrency($currency)
{
$this->currencyCode = $currency->getIsoCode();
$this->currency = $currency;
}
}
Loading