<?php
namespace Plugin\ASJPayment;
use Eccube\Event\EventArgs;
use Eccube\Event\EccubeEvents;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\OrderRepository;
use Eccube\Service\CartService;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Eccube\Repository\PaymentRepository;
class ASJPaymentEvent implements EventSubscriberInterface
{
/**
* @var OrderRepository
*/
protected $orderRepository;
/**
* @var CartService
*/
protected $cartService;
/**
* @var Session
*/
protected $session;
/**
* @var PaymentRepository
*/
protected $paymentRepository;
/**
* @var array
*/
private $chargeTypes = [
'Plugin\ASJPayment\Service\Method\Credit' => 1,
'Plugin\ASJPayment\Service\Method\Cvs' => 2,
'Plugin\ASJPayment\Service\Method\Cvs2' => 3,
'Plugin\ASJPayment\Service\Method\Atodene' => 4,
'Plugin\ASJPayment\Service\Method\Atobarai' => 7,
];
/**
* ASJPaymentEvent constructor.
*
* @param OrderRepository $orderRepository
* @param CartService $cartService
* @param Session $session
* @param PaymentRepository $paymentRepository
*/
public function __construct(
OrderRepository $orderRepository,
CartService $cartService,
SessionInterface $session,
PaymentRepository $paymentRepository
) {
$this->orderRepository = $orderRepository;
$this->cartService = $cartService;
$this->session = $session;
$this->paymentRepository = $paymentRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Shopping/index.twig' => 'onShoppingIndex',
'Shopping/confirm.twig' => 'onShoppingConfirm',
'Shopping/complete.twig' => 'onShoppingComplete',
'@admin/Order/index.twig' => 'adminOrderIndexTwig', // 受注一覧
'@admin/Order/edit.twig' => 'adminOrderEditTwig' // 受注登録
];
}
/**
* @param TemplateEvent $event
*/
public function onShoppingConfirm(TemplateEvent $event)
{
$source = $event->getSource();
$preOrderId = $this->cartService->getPreOrderId();
if (!$preOrderId) {
return;
}
$Order = $this->orderRepository->findOneBy(['pre_order_id' => $preOrderId]);
$methodClass = $Order->getPayment()->getMethodClass();
$chargeType = isset($this->chargeTypes[$methodClass]) ? $this->chargeTypes[$methodClass] : 0;
if (!$chargeType) {
return;
}
if ($chargeType == 1 || $chargeType == 4 || $chargeType == 7) {
$old = '<button type="submit" class="ec-blockBtn--action">{{ \'注文する\'|trans }}</button>';
if (($chargeType == 4 || $chargeType == 7) && count($Order->getShippings()) > 1) {
$new = '<button type="submit" class="ec-blockBtn--action" disabled="disabled" style="font-size:14px;line-height:18px;">お届け先を複数にする場合は<br>他のお支払方法をご利用ください。</button>';
} else {
$new = '<button type="submit" class="ec-blockBtn--action">{{ \'決済手続きに進む\'|trans }}</button>';
}
$source = str_replace($old, $new, $source);
$event->setSource($source);
}
}
/**
* @param TemplateEvent $event
*/
public function onShoppingComplete(TemplateEvent $event)
{
$source = $event->getSource();
if ($this->session->get('pre_order_id')) {
$old = '<div class="ec-off3Grid__cell">';
$new = '<div class="ec-off3Grid__cell" style="width:60%;margin-left:20%;">';
$source = str_replace($old, $new, $source);
$old = '<div class="ec-off4Grid">';
$new = '<p class="ec-reportDescription" style="color:#ff0000;">'.
'【注意】注文は確定していません。<br>'.
'「決済手続きへ」をクリックして手続きを行ってください。<br>'.
'決済方法を選択した後、払込票等が表示されますので代金をお支払いください。<br>'.
'なお、払込票等の画面の印刷等が済みましたら、その画面を閉じてください。<br>'.
'</p>'.
'<div class="ec-off4Grid">'.
'<div class="ec-off4Grid__cell">'.
'<a class="ec-blockBtn--action" href="ASJPayment/cvs" target="_blank">決済手続きへ</a><br>'.
'</div>'.
'</div>'.
'<div class="ec-off4Grid">';
$source = str_replace($old, $new, $source);
$event->setSource($source);
} else if ($this->session->get('uncomp_atodene')) {
$this->session->remove('uncomp_atodene');
$old = '<div class="ec-off4Grid">';
$new = '<p class="ec-reportDescription" style="color:#ff0000;">'.
'【注意】現在、後払い決済の利用についての審査結果を確認しております。<br>'.
'結果が判明しましたら、改めてご連絡いたします。<br>'.
'しばらくお待ちください。<br>'.
'</p>'.
'<div class="ec-off4Grid">';
$source = str_replace($old, $new, $source);
$event->setSource($source);
} else if ($this->session->get('uncomp_atobarai')) {
$this->session->remove('uncomp_atobarai');
$old = '<div class="ec-off4Grid">';
$new = '<p class="ec-reportDescription" style="color:#ff0000;">'.
'【注意】現在、後払い決済の利用についての審査結果を確認しております。<br>'.
'結果が判明しましたら、改めてご連絡いたします。<br>'.
'しばらくお待ちください。<br>'.
'</p>'.
'<div class="ec-off4Grid">';
$source = str_replace($old, $new, $source);
$event->setSource($source);
}
}
/**
* @param TemplateEvent $event
*/
public function onShoppingIndex(TemplateEvent $event)
{
$this->session->remove('pre_order_id');
}
/**
* 管理受注一覧に ASJペイメントの操作追加
* @param TemplateEvent $event
*/
public function adminOrderIndexTwig(TemplateEvent $event)
{
$event->addSnippet('@ASJPayment/admin/Order/asjpayment_order_index_form.twig');
}
/**
* 管理. 管理受注編集に ASJペイメント売上 / 取消 追加
* @param TemplateEvent $event
*/
public function adminOrderEditTwig(TemplateEvent $event)
{
$Payments = $this->paymentRepository->findBy([], ['sort_no' => 'ASC']);
$asj_credit_id = "";
foreach($Payments as $Payment) {
if($Payment->getMethodClass() === 'Plugin\ASJPayment\Service\Method\Credit') {
$asj_credit_id = $Payment->getId();
break;
}
}
$event->addSnippet('@ASJPayment/admin/Order/asjpayment_order_edit_form.twig');
$parameters = $event->getParameters();
$parameters['ASJ_CREDIT_ID'] = $asj_credit_id;
$event->setParameters($parameters);
}
}