app/Plugin/CodeacLinepay/Service/Method/PayPay.php line 250

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * https://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\CodeacLinepay\Service\Method;
  13. use Eccube\Entity\Master\OrderStatus;
  14. use Eccube\Entity\Order;
  15. use Eccube\Exception\ShoppingException;
  16. use Eccube\Repository\Master\OrderStatusRepository;
  17. use Eccube\Service\Payment\PaymentDispatcher;
  18. use Eccube\Service\Payment\PaymentMethodInterface;
  19. use Eccube\Service\Payment\PaymentResult;
  20. use Eccube\Service\PurchaseFlow\PurchaseContext;
  21. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  22. use Exception;
  23. use Plugin\CodeacLinepay\Entity\PaymentStatus;
  24. use Plugin\CodeacLinepay\Repository\ConfigRepository;
  25. use Plugin\CodeacLinepay\Repository\PaymentStatusRepository;
  26. use Symfony\Component\Form\FormInterface;
  27. use Symfony\Component\HttpFoundation\RedirectResponse;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  30. use Symfony\Component\Routing\RouterInterface;
  31. use Plugin\CodeacLinepay\Util\PayClient;
  32. use Plugin\CodeacLinepay\Repository\PayPayConfigRepository;
  33. use Plugin\CodeacLinepay\Util\PayCreateQR;
  34. use Plugin\CodeacLinepay\Util\PayOrderItem;
  35. /**
  36.  * クレジットカード(リンク式)の決済処理を行う
  37.  * @method redirectToRoute(string $string)
  38.  */
  39. class PayPay implements PaymentMethodInterface
  40. {
  41.     private $router;
  42.     /**
  43.      * @var Order
  44.      */
  45.     public $Order;
  46.     /**
  47.      * @var FormInterface
  48.      */
  49.     public $form;
  50.     /**
  51.      * @var OrderStatusRepository
  52.      */
  53.     public $orderStatusRepository;
  54.     /**
  55.      * @var PaymentStatusRepository
  56.      */
  57.     public $paymentStatusRepository;
  58.     /**
  59.      * @var PurchaseFlow
  60.      */
  61.     public $purchaseFlow;
  62.     private $session;
  63.     /**
  64.      * @var ConfigRepository
  65.      */
  66.     public $configRepository;
  67.     /**
  68.      * @var PayPayConfigRepository
  69.      */
  70.     public $payconfigRepository;
  71.     /**
  72.      * LinkCreditCard constructor.
  73.      *
  74.      * @param OrderStatusRepository $orderStatusRepository
  75.      * @param PaymentStatusRepository $paymentStatusRepository
  76.      * @param PurchaseFlow $shoppingPurchaseFlow
  77.      */
  78.     public function __construct(
  79.         OrderStatusRepository $orderStatusRepository,
  80.         PaymentStatusRepository $paymentStatusRepository,
  81.         PurchaseFlow $shoppingPurchaseFlow,
  82.         RouterInterface $router,
  83.         SessionInterface $session,
  84.         ConfigRepository $configRepository,
  85.         PayPayConfigRepository $payconfigRepository
  86.     ) {
  87.         $this->orderStatusRepository $orderStatusRepository;
  88.         $this->paymentStatusRepository $paymentStatusRepository;
  89.         $this->purchaseFlow $shoppingPurchaseFlow;
  90.         $this->router $router;
  91.         $this->session $session;
  92.         $this->configRepository $configRepository;
  93.         $this->payconfigRepository $payconfigRepository;
  94.     }
  95.     /**
  96.      * 注文確認画面遷移時に呼び出される.
  97.      *
  98.      * リンク式は使用しない.
  99.      *
  100.      * @return PaymentResult|void
  101.      */
  102.     public function verify()
  103.     {
  104.         $result = new PaymentResult();
  105.         $result->setSuccess(true);
  106.         return $result;
  107.     }
  108.     /**
  109.      * 注文時に呼び出される.
  110.      *
  111.      * 決済サーバのカード入力画面へリダイレクトする.
  112.      *
  113.      * @return PaymentDispatcher
  114.      *
  115.      * @throws ShoppingException
  116.      * @throws Exception
  117.      */
  118.     public function apply()
  119.     {
  120.         $baseurl $_SERVER['HTTP_ORIGIN'];
  121.         // 受注ステータスを決済処理中へ変更
  122.         $OrderStatus $this->orderStatusRepository->find(OrderStatus::PENDING);
  123.         $this->Order->setOrderStatus($OrderStatus);
  124.         // 決済ステータスを未決済へ変更
  125.         $PaymentStatus $this->paymentStatusRepository->find(PaymentStatus::OUTSTANDING);
  126.         $this->Order->setCodeacLinepayPaymentStatus($PaymentStatus);
  127.         // purchaseFlow::prepareを呼び出し, 購入処理を進める.
  128.         $this->purchaseFlow->prepare($this->Order, new PurchaseContext());
  129.         //prepare payment link from linepay
  130.         $transaction_id "CODEAC-PAYPAY-" time() . $this->Order->getOrderNo();
  131.         $orderParams = [
  132.             "amount" => (int) $this->Order->getPaymentTotal(),
  133.             "currency" => 'JPY',
  134.             "orderId" => $this->Order->getOrderNo(),
  135.             "transId" => $transaction_id,
  136.             "redirectUrl" => $baseurl $this->router->generate('paypay_payment_complete') . "?orderId=" $this->Order->getOrderNo() . "&transactionId=" $transaction_id
  137.         ];
  138.         // 決済サーバのカード入力画面へリダイレクトする.
  139.         $url $this->createPayPayQR($orderParams);
  140.         if ($url === null) {
  141.             $url $this->router->generate('line_payment_back') . "?orderId=" $this->Order->getOrderNo();
  142.         }
  143.         $response = new RedirectResponse($url);
  144.         $dispatcher = new PaymentDispatcher();
  145.         $dispatcher->setResponse($response);
  146.         return $dispatcher;
  147.     }
  148.     /**
  149.      * 注文時に呼び出される.
  150.      * リンク式の場合, applyで決済サーバのカード入力画面へ遷移するため, checkoutは使用しない.
  151.      *
  152.      * @return PaymentResult
  153.      */
  154.     public function checkout()
  155.     {
  156.         $result = new PaymentResult();
  157.         $result->setSuccess(true);
  158.         return $result;
  159.     }
  160.     /**
  161.      * {@inheritdoc}
  162.      */
  163.     public function setFormType(FormInterface $form)
  164.     {
  165.         $this->form $form;
  166.     }
  167.     /**
  168.      * {@inheritdoc}
  169.      */
  170.     public function setOrder(Order $Order)
  171.     {
  172.         $this->Order $Order;
  173.     }
  174.     /**
  175.      * @throws Exception
  176.      */
  177.     private function createPayPayQR($orderParams)
  178.     {
  179.         $config $this->payconfigRepository->get(1);
  180.         try {
  181.             // $tpay = new TestPay();
  182.             // print_r($tpay->getMessage());exit();
  183.             $payPay = new PayClient([
  184.                 'API_KEY' => $config->getApiId(),
  185.                 'API_SECRET' => $config->getApiSecret(),
  186.                 'MERCHANT_ID' => $config->getMemberId()
  187.             ], (bool) $config->getProduction());
  188.             $CQCPayload = new PayCreateQR();
  189.             $CQCPayload->setMerchantPaymentId($orderParams['transId']);
  190.             $CQCPayload->setRequestedAt();
  191.             $CQCPayload->setCodeType("ORDER_QR");
  192.             // Provide order details for invoicing
  193.             $OrderItems = [];
  194.             $OrderItems[] = (new PayOrderItem())->setName('Codeac Paypay product')->setQuantity(1)->setUnitPrice(['amount' => $orderParams['amount'], 'currency' => $orderParams['currency']]);
  195.             $CQCPayload->setOrderItems($OrderItems);
  196.             $amount = [
  197.                 "amount" => $orderParams['amount'],
  198.                 "currency" => $orderParams['currency']
  199.             ];
  200.             $CQCPayload->setAmount($amount);
  201.             $CQCPayload->setRedirectType('WEB_LINK');
  202.             $CQCPayload->setRedirectUrl($orderParams['redirectUrl']);
  203.             // $payPay->code([
  204.             //     'amount'=>1000,
  205.             //     "codeType"=>"ORDER_QR",
  206.             //     "merchantPaymentId"=>"DEVELOPER-PANEL-DEMO-2f454328-".time(),
  207.             //     "orderDescription"=>""
  208.             // ]);
  209.             $response $payPay->code->createQRCode($CQCPayload);
  210.             $result $response['resultInfo'];
  211.             if ($result['code'] === 'SUCCESS'$data $response['data'];
  212.             else return null;
  213.             //store session data for line order
  214.             $this->session->set('payPayOrder', [
  215.                 'transactionId' => (string) $data["merchantPaymentId"],
  216.                 'params' => $data,
  217.             ]);
  218.             return $data['url'];
  219.         } catch (Exception $e) {
  220.             print_r($e->getMessage());
  221.             exit();
  222.         }
  223.     }
  224. }