Module Maintenance + création du module Recette
This commit is contained in:
103
local/modules/PayPlugModule/Service/OrderStatusService.php
Executable file
103
local/modules/PayPlugModule/Service/OrderStatusService.php
Executable file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace PayPlugModule\Service;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Thelia\Core\Event\OrderStatus\OrderStatusCreateEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Model\OrderStatusQuery;
|
||||
|
||||
class OrderStatusService
|
||||
{
|
||||
const REFUND_PENDING_ORDER_STATUS_CODE = "refund_pending";
|
||||
|
||||
const AUTHORIZED_CAPTURE_ORDER_STATUS_CODE = "authorized_capture";
|
||||
const EXPIRED_CAPTURE_ORDER_STATUS_CODE = "expired_capture";
|
||||
|
||||
public function __construct(EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
public function initAllStatuses()
|
||||
{
|
||||
$this->findOrCreateRefundPendingOrderStatus();
|
||||
$this->findOrCreateAuthorizedCaptureOrderStatus();
|
||||
$this->findOrCreateExpiredCaptureOrderStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Thelia\Model\OrderStatus
|
||||
*/
|
||||
public function findOrCreateRefundPendingOrderStatus()
|
||||
{
|
||||
$refundPendingOrderStatus = OrderStatusQuery::create()
|
||||
->findOneByCode($this::REFUND_PENDING_ORDER_STATUS_CODE);
|
||||
|
||||
if (null !== $refundPendingOrderStatus) {
|
||||
return $refundPendingOrderStatus;
|
||||
}
|
||||
|
||||
$refundPendingOrderStatusEvent = (new OrderStatusCreateEvent())
|
||||
->setCode(self::REFUND_PENDING_ORDER_STATUS_CODE)
|
||||
->setColor("#A7A7A7")
|
||||
->setLocale('en_US')
|
||||
->setTitle('Refund pending');
|
||||
|
||||
$this->dispatcher->dispatch(TheliaEvents::ORDER_STATUS_CREATE, $refundPendingOrderStatusEvent);
|
||||
|
||||
return $refundPendingOrderStatusEvent->getOrderStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Thelia\Model\OrderStatus
|
||||
*/
|
||||
public function findOrCreateAuthorizedCaptureOrderStatus()
|
||||
{
|
||||
$authorizedCaptureOrderStatus = OrderStatusQuery::create()
|
||||
->findOneByCode($this::AUTHORIZED_CAPTURE_ORDER_STATUS_CODE);
|
||||
|
||||
if (null !== $authorizedCaptureOrderStatus) {
|
||||
return $authorizedCaptureOrderStatus;
|
||||
}
|
||||
|
||||
$authorizedCaptureOrderStatus = (new OrderStatusCreateEvent())
|
||||
->setCode(self::AUTHORIZED_CAPTURE_ORDER_STATUS_CODE)
|
||||
->setColor("#71ED71")
|
||||
->setLocale('en_US')
|
||||
->setTitle('Authorized capture');
|
||||
|
||||
$this->dispatcher->dispatch(TheliaEvents::ORDER_STATUS_CREATE, $authorizedCaptureOrderStatus);
|
||||
|
||||
return $authorizedCaptureOrderStatus->getOrderStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Thelia\Model\OrderStatus
|
||||
*/
|
||||
public function findOrCreateExpiredCaptureOrderStatus()
|
||||
{
|
||||
$expiredCaptureOrderStatus = OrderStatusQuery::create()
|
||||
->findOneByCode($this::EXPIRED_CAPTURE_ORDER_STATUS_CODE);
|
||||
|
||||
if (null !== $expiredCaptureOrderStatus) {
|
||||
return $expiredCaptureOrderStatus;
|
||||
}
|
||||
|
||||
$expiredCaptureOrderStatus = (new OrderStatusCreateEvent())
|
||||
->setCode(self::EXPIRED_CAPTURE_ORDER_STATUS_CODE)
|
||||
->setColor("#4B4B4B")
|
||||
->setLocale('en_US')
|
||||
->setTitle('Expired capture');
|
||||
|
||||
$this->dispatcher->dispatch(TheliaEvents::ORDER_STATUS_CREATE, $expiredCaptureOrderStatus);
|
||||
|
||||
return $expiredCaptureOrderStatus->getOrderStatus();
|
||||
}
|
||||
|
||||
}
|
||||
152
local/modules/PayPlugModule/Service/PaymentService.php
Executable file
152
local/modules/PayPlugModule/Service/PaymentService.php
Executable file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace PayPlugModule\Service;
|
||||
|
||||
use Payplug\Notification;
|
||||
use Payplug\Payment;
|
||||
use Payplug\Payplug;
|
||||
use PayPlugModule\Event\PayPlugPaymentEvent;
|
||||
use PayPlugModule\Model\OrderPayPlugMultiPayment;
|
||||
use PayPlugModule\Model\PayPlugCardQuery;
|
||||
use PayPlugModule\Model\PayPlugConfigValue;
|
||||
use PayPlugModule\PayPlugModule;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Model\Order;
|
||||
|
||||
class PaymentService
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
public function __construct(EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
self::initAuth();
|
||||
}
|
||||
|
||||
public function isPayPlugAvailable()
|
||||
{
|
||||
if (!PayPlugModule::getConfigValue(PayPlugConfigValue::PAYMENT_ENABLED, false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check API availability
|
||||
try {
|
||||
Payment::listPayments(1);
|
||||
} catch (\Exception $exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Order $order
|
||||
* @return array
|
||||
* @throws \Propel\Runtime\Exception\PropelException
|
||||
*/
|
||||
public function sendOrderPayment(
|
||||
Order $order,
|
||||
bool $capture = false,
|
||||
bool $allowSaveCard = false,
|
||||
int $paymentSlice = 1
|
||||
) {
|
||||
$paymentEvent = (new PayPlugPaymentEvent())
|
||||
->buildFromOrder($order)
|
||||
->setCapture($capture)
|
||||
->setAllowSaveCard($allowSaveCard);
|
||||
|
||||
if (null !== $card = PayPlugCardQuery::create()->findOneByCustomerId($order->getCustomerId())) {
|
||||
$paymentEvent->setPaymentMethod($card->getUuid())
|
||||
->setInitiator('PAYER')
|
||||
->setAllowSaveCard(false);
|
||||
}
|
||||
|
||||
$firstPayment = null;
|
||||
if ($paymentSlice > 1) {
|
||||
$totalAmount = $paymentEvent->getAmount();
|
||||
$firstAmount = round($totalAmount / $paymentSlice) + $totalAmount % $paymentSlice;
|
||||
$paymentEvent->setForceSaveCard(true)
|
||||
->setAllowSaveCard(false)
|
||||
->setPaymentMethod(null)
|
||||
->setAmount($firstAmount);
|
||||
$today = (new \DateTime())->setTime(0,0,0,0);
|
||||
|
||||
$firstPayment = (new OrderPayPlugMultiPayment())
|
||||
->setAmount($paymentEvent->getAmount())
|
||||
->setOrder($order)
|
||||
->setPlannedAt($today)
|
||||
->setPaymentId($paymentEvent->getPaymentId())
|
||||
->setIsFirstPayment(true);
|
||||
$firstPayment->save();
|
||||
|
||||
for ($paymentCount = 1; $paymentCount < $paymentSlice ; $paymentCount++) {
|
||||
$paymentDay = (clone $today)->add((new \DateInterval('P'.intval($paymentCount * 30).'D')));
|
||||
$multiPayment = (new OrderPayPlugMultiPayment())
|
||||
->setAmount(round($totalAmount / $paymentSlice))
|
||||
->setOrder($order)
|
||||
->setPlannedAt($paymentDay);
|
||||
$multiPayment->save();
|
||||
}
|
||||
}
|
||||
|
||||
$this->dispatcher->dispatch(PayPlugPaymentEvent::ORDER_PAYMENT_EVENT, $paymentEvent);
|
||||
|
||||
if (null !== $firstPayment) {
|
||||
$firstPayment->setPaymentId($paymentEvent->getPaymentId())
|
||||
->save();
|
||||
|
||||
}
|
||||
|
||||
$isPaid = $paymentEvent->isPaid();
|
||||
|
||||
// If one click payment consider it as isPaid (redirect to order/placed)
|
||||
if (!$isPaid && $paymentEvent->isCapture() && null !== $paymentEvent->getPaymentMethod()) {
|
||||
$isPaid = true;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $paymentEvent->getPaymentId(),
|
||||
'url' => $paymentEvent->getPaymentUrl(),
|
||||
'isPaid' => $isPaid
|
||||
];
|
||||
}
|
||||
|
||||
public function doOrderCapture(Order $order)
|
||||
{
|
||||
$paymentEvent = (new PayPlugPaymentEvent())
|
||||
->buildFromOrder($order);
|
||||
|
||||
$this->dispatcher->dispatch(PayPlugPaymentEvent::ORDER_CAPTURE_EVENT, $paymentEvent);
|
||||
}
|
||||
|
||||
public function doOrderRefund(Order $order, int $amountRefund = null)
|
||||
{
|
||||
$paymentEvent = (new PayPlugPaymentEvent())
|
||||
->buildFromOrder($order);
|
||||
|
||||
if (null !== $amountRefund) {
|
||||
$paymentEvent->setAmount($amountRefund);
|
||||
}
|
||||
|
||||
$this->dispatcher->dispatch(PayPlugPaymentEvent::ORDER_REFUND_EVENT, $paymentEvent);
|
||||
}
|
||||
|
||||
public function getNotificationResource(Request $request)
|
||||
{
|
||||
return Notification::treat($request->getContent());
|
||||
}
|
||||
|
||||
public function initAuth()
|
||||
{
|
||||
return Payplug::init(
|
||||
[
|
||||
'secretKey' => PayPlugConfigValue::getApiKey(),
|
||||
'apiVersion' => '2019-08-06'
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user