139 lines
5.3 KiB
PHP
139 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Ecommerce\Payment;
|
|
|
|
use App\Entities\PaymentEntity;
|
|
use App\Libraries\Adapter\Payment\CookiePayment as PaymentAdapter;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class CardController extends PaymentController
|
|
{
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
$this->_viewDatas['className'] = 'Card';
|
|
parent::initController($request, $response, $logger);
|
|
$this->_viewPath .= strtolower($this->_viewDatas['className']);
|
|
}
|
|
|
|
public function getFields(string $action = ""): array
|
|
{
|
|
switch ($action) {
|
|
case 'insert':
|
|
return ["card_quota", "card_number", "card_expiration", "email", "mobile"];
|
|
break;
|
|
default:
|
|
return [];
|
|
break;
|
|
}
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "order_uid":
|
|
$rules[$field] = 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]';
|
|
break;
|
|
case "card_quota":
|
|
$rules[$field] = 'required|in_list[00,01]';
|
|
break;
|
|
case "card_number":
|
|
$rules[$field] = 'required|regex_match[/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}/]';
|
|
break;
|
|
case "card_expiration":
|
|
$rules[$field] = 'required|regex_match[/^[1-12]{2}-[0-9]{4}/]';
|
|
break;
|
|
case "email":
|
|
$rules[$field] = 'required|valid_email';
|
|
break;
|
|
case "mobile":
|
|
$rules[$field] = 'required|regex_match[/^[0-9]{3}-[0-9]{4}-[0-9]{4}/]';
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getFieldFilters(): array
|
|
{
|
|
return ['card_quota'];
|
|
}
|
|
public function getFieldBatchFilters(): array
|
|
{
|
|
return parent::getFieldBatchFilters();
|
|
}
|
|
//Field별 Form Option용
|
|
public function getFieldFormOption(string $field): array
|
|
{
|
|
switch ($field) {
|
|
case 'card_quota':
|
|
$options = lang($this->_viewDatas['className'] . '.CARD_QUOTA');
|
|
break;
|
|
default:
|
|
$options = parent::getFieldFormOption($field);
|
|
break;
|
|
}
|
|
if (!is_array($options)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->_viewDatas['className']}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
//카드결제 Form
|
|
//Insert관련
|
|
protected function insert_form_process()
|
|
{
|
|
parent::insert_form_process();
|
|
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => ['order_uid' => $this->_viewDatas['order']->getPrimaryKey()]];
|
|
}
|
|
public function insert_form()
|
|
{
|
|
try {
|
|
$uid = $this->request->getVar('order_uid') ?: throw new \Exception("주문번호가 지정되지 않았습니다.");
|
|
$this->_viewDatas = $this->init(__FUNCTION__);
|
|
$this->_viewDatas['order'] = $this->getOrderModel()->getEntity([$this->getOrderModel()->getPrimaryKey() => $uid]);
|
|
$this->insert_form_process();
|
|
helper(['form']);
|
|
$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
|
return view($this->_viewPath . '/insert', ['viewDatas' => $this->_viewDatas]);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/")->with('return_message', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
//PG사 카드결제처리
|
|
protected function insert_process()
|
|
{
|
|
//결제후 정보저장
|
|
$adapter = new PaymentAdapter();
|
|
$adapter->setDatas($this->_viewDatas['fieldDatas']);
|
|
$response = $adapter->execute();
|
|
echo var_export($response, true);
|
|
$entity = $this->getPaymentModel()->create($response);
|
|
//아래는 Order Complete처리용
|
|
parent::insert_process();
|
|
return $entity;
|
|
}
|
|
//카드결제
|
|
public function insert()
|
|
{
|
|
$msg = "";
|
|
try {
|
|
$this->_viewDatas = $this->init(__FUNCTION__);
|
|
$this->insert_validate();
|
|
$entity = $this->insert_process();
|
|
$msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 " . __FUNCTION__ . " 완료하였습니다.";
|
|
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/");
|
|
} catch (\Exception $e) {
|
|
$msg = "{$this->_viewDatas['title']}에서 " . __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage();
|
|
log_message("error", $e->getMessage());
|
|
$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
|
return redirect()->back()->withInput();
|
|
} finally {
|
|
$this->_session->setFlashdata("return_message", $msg);
|
|
}
|
|
}
|
|
}
|