<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* https://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CodeacLinepay\Service\Method;
use Eccube\Entity\Master\OrderStatus;
use Eccube\Entity\Order;
use Eccube\Exception\ShoppingException;
use Eccube\Repository\Master\OrderStatusRepository;
use Eccube\Service\Payment\PaymentDispatcher;
use Eccube\Service\Payment\PaymentMethodInterface;
use Eccube\Service\Payment\PaymentResult;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Exception;
use Plugin\CodeacLinepay\Entity\PaymentStatus;
use Plugin\CodeacLinepay\Repository\ConfigRepository;
use Plugin\CodeacLinepay\Repository\PaymentStatusRepository;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Plugin\CodeacLinepay\Util\PayClient;
use Plugin\CodeacLinepay\Repository\PayPayConfigRepository;
use Plugin\CodeacLinepay\Util\PayCreateQR;
use Plugin\CodeacLinepay\Util\PayOrderItem;
/**
* クレジットカード(リンク式)の決済処理を行う
* @method redirectToRoute(string $string)
*/
class PayPay implements PaymentMethodInterface
{
private $router;
/**
* @var Order
*/
public $Order;
/**
* @var FormInterface
*/
public $form;
/**
* @var OrderStatusRepository
*/
public $orderStatusRepository;
/**
* @var PaymentStatusRepository
*/
public $paymentStatusRepository;
/**
* @var PurchaseFlow
*/
public $purchaseFlow;
private $session;
/**
* @var ConfigRepository
*/
public $configRepository;
/**
* @var PayPayConfigRepository
*/
public $payconfigRepository;
/**
* LinkCreditCard constructor.
*
* @param OrderStatusRepository $orderStatusRepository
* @param PaymentStatusRepository $paymentStatusRepository
* @param PurchaseFlow $shoppingPurchaseFlow
*/
public function __construct(
OrderStatusRepository $orderStatusRepository,
PaymentStatusRepository $paymentStatusRepository,
PurchaseFlow $shoppingPurchaseFlow,
RouterInterface $router,
SessionInterface $session,
ConfigRepository $configRepository,
PayPayConfigRepository $payconfigRepository
) {
$this->orderStatusRepository = $orderStatusRepository;
$this->paymentStatusRepository = $paymentStatusRepository;
$this->purchaseFlow = $shoppingPurchaseFlow;
$this->router = $router;
$this->session = $session;
$this->configRepository = $configRepository;
$this->payconfigRepository = $payconfigRepository;
}
/**
* 注文確認画面遷移時に呼び出される.
*
* リンク式は使用しない.
*
* @return PaymentResult|void
*/
public function verify()
{
$result = new PaymentResult();
$result->setSuccess(true);
return $result;
}
/**
* 注文時に呼び出される.
*
* 決済サーバのカード入力画面へリダイレクトする.
*
* @return PaymentDispatcher
*
* @throws ShoppingException
* @throws Exception
*/
public function apply()
{
$baseurl = $_SERVER['HTTP_ORIGIN'];
// 受注ステータスを決済処理中へ変更
$OrderStatus = $this->orderStatusRepository->find(OrderStatus::PENDING);
$this->Order->setOrderStatus($OrderStatus);
// 決済ステータスを未決済へ変更
$PaymentStatus = $this->paymentStatusRepository->find(PaymentStatus::OUTSTANDING);
$this->Order->setCodeacLinepayPaymentStatus($PaymentStatus);
// purchaseFlow::prepareを呼び出し, 購入処理を進める.
$this->purchaseFlow->prepare($this->Order, new PurchaseContext());
//prepare payment link from linepay
$transaction_id = "CODEAC-PAYPAY-" . time() . $this->Order->getOrderNo();
$orderParams = [
"amount" => (int) $this->Order->getPaymentTotal(),
"currency" => 'JPY',
"orderId" => $this->Order->getOrderNo(),
"transId" => $transaction_id,
"redirectUrl" => $baseurl . $this->router->generate('paypay_payment_complete') . "?orderId=" . $this->Order->getOrderNo() . "&transactionId=" . $transaction_id
];
// 決済サーバのカード入力画面へリダイレクトする.
$url = $this->createPayPayQR($orderParams);
if ($url === null) {
$url = $this->router->generate('line_payment_back') . "?orderId=" . $this->Order->getOrderNo();
}
$response = new RedirectResponse($url);
$dispatcher = new PaymentDispatcher();
$dispatcher->setResponse($response);
return $dispatcher;
}
/**
* 注文時に呼び出される.
* リンク式の場合, applyで決済サーバのカード入力画面へ遷移するため, checkoutは使用しない.
*
* @return PaymentResult
*/
public function checkout()
{
$result = new PaymentResult();
$result->setSuccess(true);
return $result;
}
/**
* {@inheritdoc}
*/
public function setFormType(FormInterface $form)
{
$this->form = $form;
}
/**
* {@inheritdoc}
*/
public function setOrder(Order $Order)
{
$this->Order = $Order;
}
/**
* @throws Exception
*/
private function createPayPayQR($orderParams)
{
$config = $this->payconfigRepository->get(1);
try {
// $tpay = new TestPay();
// print_r($tpay->getMessage());exit();
$payPay = new PayClient([
'API_KEY' => $config->getApiId(),
'API_SECRET' => $config->getApiSecret(),
'MERCHANT_ID' => $config->getMemberId()
], (bool) $config->getProduction());
$CQCPayload = new PayCreateQR();
$CQCPayload->setMerchantPaymentId($orderParams['transId']);
$CQCPayload->setRequestedAt();
$CQCPayload->setCodeType("ORDER_QR");
// Provide order details for invoicing
$OrderItems = [];
$OrderItems[] = (new PayOrderItem())->setName('Codeac Paypay product')->setQuantity(1)->setUnitPrice(['amount' => $orderParams['amount'], 'currency' => $orderParams['currency']]);
$CQCPayload->setOrderItems($OrderItems);
$amount = [
"amount" => $orderParams['amount'],
"currency" => $orderParams['currency']
];
$CQCPayload->setAmount($amount);
$CQCPayload->setRedirectType('WEB_LINK');
$CQCPayload->setRedirectUrl($orderParams['redirectUrl']);
// $payPay->code([
// 'amount'=>1000,
// "codeType"=>"ORDER_QR",
// "merchantPaymentId"=>"DEVELOPER-PANEL-DEMO-2f454328-".time(),
// "orderDescription"=>""
// ]);
$response = $payPay->code->createQRCode($CQCPayload);
$result = $response['resultInfo'];
if ($result['code'] === 'SUCCESS') $data = $response['data'];
else return null;
//store session data for line order
$this->session->set('payPayOrder', [
'transactionId' => (string) $data["merchantPaymentId"],
'params' => $data,
]);
return $data['url'];
} catch (Exception $e) {
print_r($e->getMessage());
exit();
}
}
}