shoppingmallv2/app/Controllers/Front/PaymentController.php
2023-08-11 10:28:12 +09:00

153 lines
6.0 KiB
PHP

<?php
namespace App\Controllers\Front;
use App\Entities\PaymentEntity;
use App\Libraries\Adapter\Payment\CookiePayment as PaymentAdapter;
use App\Models\OrderModel;
use App\Models\PaymentModel;
use App\Models\UserModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class PaymentController extends FrontController
{
private $_userModel = null;
private $_orderModel = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->_model = new PaymentModel($this->getFields());
parent::initController($request, $response, $logger);
$this->_viewPath .= strtolower($this->_model->getClassName());
//Default 회원정보 Category
$this->_category = DEFAULTS['PAYMENT_CATEGORY'];
$this->isRole('index');
}
private function getUserModel()
{
return $this->_userModel = $this->_userModel ?: new UserModel();
}
private function getOrderModel()
{
return $this->_orderModel = $this->_orderModel ?: new OrderModel();
}
final public function getFields(string $action = ""): array
{
switch ($action) {
case 'insert':
return ["TID", "ORDERNO", "AMOUNT", "QUOTA", "ACCEPTNO", "ACCEPTDATE", "RESULTCODE", "created_at"];
break;
case "index":
case "excel":
case "view":
return ["ORDERNO", "AMOUNT", "QUOTA", "ACCEPTNO", "ACCEPTDATE", "RESULTCODE", "created_at"];
break;
default:
return [];
break;
}
}
final public function getFieldFilters(): array
{
return [];
}
final public function getFieldBatchFilters(): array
{
return [];
}
//추가기능
//결제(uid -> order_uid)
private function card_init()
{
$this->_viewDatas['fields'] = [
"card_quota", "card_number", "card_expiration", "card_email", "card_mobile"
];
$this->_viewDatas['fieldRules'] = [
'card_quota' => 'required|in_list[00,01]',
'card_number' => 'required|regex_match[/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}/]',
'card_expiration' => 'required|regex_match[/^[1-12]{2}-[0-9]{4}/]',
'card_email' => 'required|valid_email',
'card_mobile' => 'required|regex_match[/^^[0-9]{3}-[0-9]{4}-[0-9]{4}/]',
];
$this->_viewDatas['fieldFilters'] = ['card_quota'];
$this->_viewDatas['fieldFormOptions']['card_quota'] = lang($this->_model->getClassName() . '.CARD_QUOTA');
}
public function card_form($uid)
{
try {
$this->card_init();
$this->_viewDatas['user'] = $this->getUserModel()->getEntity([$this->getUserModel()->getPrimaryKey() => $this->_viewDatas['auth'][AUTH_FIELDS['ID']]]);
$this->_viewDatas['order'] = $this->getOrderModel()->getEntity([$this->getOrderModel()->getPrimaryKey() => $uid]);
helper(['form']);
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
return view($this->_viewPath . '/card', ['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());
}
}
protected function card_validate()
{
//fieldData Rule 검사
if (!$this->validate($this->_viewDatas['fieldRules'])) {
throw new \Exception("결제정보의 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
}
//fieldData 적용
$this->_viewDatas['fieldDatas'] = array();
foreach ($this->_viewDatas['fields'] as $field) {
$this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field);
}
}
//처리
private function card_process(): PaymentEntity
{
//결제후 정보저장
$adapter = new PaymentAdapter();
$response = $adapter->execute();
echo var_export($response, true);
return $this->_model->create($response);
}
public function card($uid)
{
$msg = "";
try {
$this->card_init();
$this->card_validate();
$entity = $this->card_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);
}
}
public function deposit_form($uid)
{
try {
$this->_viewDatas['bank'] = [
"name" => getenv("payment.deposit.bank.name") ?: "은행명",
"account" => getenv("payment.deposit.bank.account") ?: "계좌번호",
"holder" => getenv("payment.deposit.bank.holder") ?: "예금주"
];
$this->_viewDatas['order'] = $this->getOrderModel()->getEntity([$this->getOrderModel()->getPrimaryKey() => $uid]);
$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
return view($this->_viewPath . '/deposit', ['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());
}
}
}