<?php
namespace Plugin\ASJPayment\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Common\Constant;
use Eccube\Entity\Master\OrderStatus;
use Eccube\Entity\Order;
use Eccube\Entity\Plugin;
use Eccube\Repository\Master\OrderStatusRepository;
use Eccube\Repository\OrderRepository;
use Eccube\Repository\ClassCategoryRepository;
use Eccube\Repository\ProductClassRepository;
use Eccube\Repository\PluginRepository;
use Eccube\Service\CartService;
use Eccube\Service\ShoppingService;
use Plugin\ASJPayment\Repository\ConfigRepository;
use Plugin\ASJRegular\Repository\ASJRegularConfigRepository;
use Plugin\ASJRegular\Repository\ASJRegularProductRegularKindRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SendController extends AbstractController
{
/**
* @var OrderRepository
*/
protected $orderRepository;
/**
* @var OrderStatusRepository
*/
protected $orderStatusRepository;
/**
* @var CartService
*/
protected $cartService;
/**
* @var PluginRepository
*/
protected $pluginRepository;
/**
* @var ConfigRepository
*/
protected $configRepository;
/**
* @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,
];
/**
* SendController constructor.
*
* @param OrderRepository $orderRepository
* @param OrderStatusRepository $orderStatusRepository
* @param CartService $cartService
* @param PluginRepository $pluginRepository
* @param ConfigRepository $configRepository
*/
public function __construct(
OrderRepository $orderRepository,
OrderStatusRepository $orderStatusRepository,
CartService $cartService,
PluginRepository $pluginRepository,
ConfigRepository $configRepository,
?ASJRegularConfigRepository $ASJRegularConfigRepository,
ProductClassRepository $ProductClassRepository,
ClassCategoryRepository $ClassCategoryRepository
) {
$this->orderRepository = $orderRepository;
$this->orderStatusRepository = $orderStatusRepository;
$this->cartService = $cartService;
$this->pluginRepository = $pluginRepository;
$this->configRepository = $configRepository;
$this->ASJRegularConfigRepository = $ASJRegularConfigRepository;
$this->ProductClassRepository = $ProductClassRepository;
$this->ClassCategoryRepository = $ClassCategoryRepository;
}
/**
* @Route("/shopping/ASJPayment/send", name="asj_payment_send")
*
* @param Request $request
*/
public function send(Request $request, ContainerInterface $container)
{
$Config = $this->configRepository->get();
$preOrderId = $this->cartService->getPreOrderId();
$Order = $this->orderRepository->findOneBy(['pre_order_id' => $preOrderId]);
$methodClass = $Order->getPayment()->getMethodClass();
$chargeType = $this->chargeTypes[$methodClass];
if (!$chargeType) {
return $this->redirectToRoute('shopping_error');
}
$Plugin = $this->pluginRepository->findOneBy(['code' => 'ASJPayment']);
if ($this->isGranted('IS_AUTHENTICATED_FULLY') && $chargeType == 1 && $Config->getCardRetention() == 1) {
$unix_time = time();
$var_key_param = 'admin='.$Config->getCreditId().'&amount='.(int)$Order->getPaymentTotal().'&charge='.$Order->getId().'&user='.$Order->getCustomer()->getId().'&time='.$unix_time.'&'.mb_convert_encoding($Config->getCardRetentionPass(), 'EUC-JP', 'auto');
$arrSendData = [
'admin' => $Config->getCreditId(),
'charge' => $Order->getId(),
'amount' => (int)$Order->getPaymentTotal(),
'mail' => $Order->getEmail(),
'name' => htmlspecialchars($Order->getName01().' '.$Order->getName02(), ENT_QUOTES),
'charge_type' => $chargeType,
'user' => $Order->getCustomer()->getId(),
'time' => $unix_time,
'key' => hash("sha256", $var_key_param),
'version' => Constant::VERSION. '_' .$Plugin->getVersion(),
'free1' => $Order->getOrderNo()
];
} else {
$arrSendData = [
'admin' => $Config->getCreditId(),
'charge' => $Order->getId(),
'amount' => (int)$Order->getPaymentTotal(),
'mail' => $Order->getEmail(),
'name' => htmlspecialchars($Order->getName01().' '.$Order->getName02(), ENT_QUOTES),
'charge_type' => $chargeType,
'version' => Constant::VERSION. '_' .$Plugin->getVersion(),
'free1' => $Order->getOrderNo()
];
}
if ($chargeType == 3) {
$arrSendData += [
'zip1' => substr($Order->getPostalCode(), 0, 3),
'zip2' => substr($Order->getPostalCode(), 3, 4),
'state' => $Order->getPref(),
'city' => $Order->getAddr01(),
'street' => $Order->getAddr02(),
'tel' => $Order->getPhoneNumber(),
];
} else if ($chargeType == 4 || $chargeType == 7) {
$arrSendData += [
'zip1' => substr($Order->getPostalCode(), 0, 3),
'zip2' => substr($Order->getPostalCode(), 3, 4),
'state' => $Order->getPref()->getId(),
'city' => htmlspecialchars($Order->getAddr01(), ENT_QUOTES),
'street' => htmlspecialchars($Order->getAddr02(), ENT_QUOTES),
'tel' => substr($Order->getPhoneNumber(), 0, 3).'-'.substr($Order->getPhoneNumber(), 3, 4).'-'.substr($Order->getPhoneNumber(), 7),
];
foreach ($Order->getShippings() as $Shipping) {
$arrSendData += [
'ship_name' => htmlspecialchars($Shipping->getName01().' '.$Shipping->getName02(), ENT_QUOTES),
'ship_zip1' => substr($Shipping->getPostalCode(), 0, 3),
'ship_zip2' => substr($Shipping->getPostalCode(), 3, 4),
'ship_state' => $Shipping->getPref()->getId(),
'ship_city' => htmlspecialchars($Shipping->getAddr01(), ENT_QUOTES),
'ship_street' => htmlspecialchars($Shipping->getAddr02(), ENT_QUOTES),
'ship_tel' => substr($Shipping->getPhoneNumber(), 0, 3).'-'.substr($Shipping->getPhoneNumber(), 3, 4).'-'.substr($Shipping->getPhoneNumber(), 7),
];
}
$i = 1;
foreach ($Order->getOrderItems() as $OrderItem) {
if ((int)$OrderItem->getPriceIncTax()) {
$arrSendData += [
'item'.$i => htmlspecialchars($OrderItem->getProductName(), ENT_QUOTES),
'price'.$i => (int)$OrderItem->getPriceIncTax(),
'count'.$i => $OrderItem->getQuantity(),
];
$i++;
}
}
} else {
$Plugin2 = $this->pluginRepository->findOneBy(['code' => 'ASJRegular', 'enabled' => true]);
$count = 0;
foreach ($Order->getOrderItems() as $OrderItem) {
if($OrderItem->isProduct()) {
$Product = $OrderItem->getProduct();
$ProductClass = $OrderItem->getProductClass();
$count++;
}
}
if ($Plugin2 && $count == 1) {
$Config2 = $this->ASJRegularConfigRepository->find(1);
if(!$Product->hasProductClass()) {
if($Config2->getSaleType() == $this->ProductClassRepository->findOneBy(['Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null])->getSaleType()) {
$RegularKind = $Product->getRegularKind();
$arrSendData += [
'regular' => 'on',
'regular_kind' => $RegularKind->getId(),
'min_day' => $Config2->getMinDay(),
];
if ($Order->getDiscount() > 0) {
$arrSendData += [
'regular_amount' => $Order->getPaymentTotal() + $Order->getDiscount(),
];
}
}
} else {
if($Config2->getSaleType() == $ProductClass->getSaleType()) {
$RegularKind = $ProductClass->getRegularKind();
$arrSendData += [
'regular' => 'on',
'regular_kind' => $RegularKind->getId(),
'min_day' => $Config2->getMinDay(),
];
if ($Order->getDiscount() > 0) {
$arrSendData += [
'regular_amount' => $Order->getPaymentTotal() + $Order->getDiscount(),
];
}
}
}
}
}
// Chrome時特別処理
if(version_compare(Constant::VERSION, '4.0.4', '<')) {
$useragent = $request->server->get('HTTP_USER_AGENT');
if(strpos($useragent, 'Chrome/') !== false && strpos($useragent, 'Edge/') === false) {
// セッションクッキーの設定をみて置き換えるかどうか
$cookieopt = session_get_cookie_params();
$re_cookie = true;
if($cookieopt['secure'] === true && isset($cookieopt['samesite']) && strcasecmp($cookieopt['samesite'], "none")) {
$re_cookie = false;
}
// セッションがクッキーとして発行されていたならセッションの設定を変更して再開する
if($re_cookie === true && in_array(session_name(), $request->cookies->keys())) {
session_write_close();
session_set_cookie_params($cookieopt["lifetime"], $cookieopt["path"]."; SameSite=None", $cookieopt["domain"], true, $cookieopt["httponly"]);
session_start();
}
}
}
return $this->render('@ASJPayment/default/send.twig', [
'action' => $Config->getCreditUrl(),
'arrSendData' => $arrSendData,
]);
}
/**
* @Route("/shopping/ASJPayment/cvs", name="asj_payment_cvs")
*
* @param Request $request
*/
public function cvs(Request $request)
{
$Config = $this->configRepository->get();
$preOrderId = $this->session->get('pre_order_id');
$Order = $this->orderRepository->findOneBy(['pre_order_id' => $preOrderId]);
$Plugin = $this->pluginRepository->findOneBy(['code' => 'ASJPayment']);
$arrSendData = [
'admin' => $Config->getCreditId(),
'charge' => $Order->getId(),
'amount' => (int)$Order->getPaymentTotal(),
'mail' => $Order->getEmail(),
'name' => htmlspecialchars($Order->getName01().' '.$Order->getName02(), ENT_QUOTES),
'charge_type' => 2,
'version' => Constant::VERSION. '_' .$Plugin->getVersion(),
];
return $this->render('@ASJPayment/default/send.twig', [
'action' => $Config->getCreditUrl(),
'arrSendData' => $arrSendData,
]);
}
}