108 lines
4.7 KiB
PHP
108 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Front\Order\Payment;
|
|
|
|
use App\Controllers\Front\Order\OrderController;
|
|
use App\Entities\BillingEntity;
|
|
use App\Entities\OrderEntity;
|
|
use App\Models\BillingModel;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class PaymentController extends OrderController
|
|
{
|
|
protected $_billingModel = null;
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
}
|
|
|
|
final protected function getBillingModel(): BillingModel
|
|
{
|
|
return $this->_billingModel = $this->_billingModel ?: new BillingModel();
|
|
}
|
|
|
|
//Update관련 (결제처리용)
|
|
protected function update_form_process($entity)
|
|
{
|
|
$entity = parent::update_form_process($entity);
|
|
$this->_viewDatas['user'] = $this->getUserModel()->getEntity([$this->getUserModel()->getPrimaryKey() => $this->_viewDatas['auth'][AUTH_FIELDS['ID']]]);
|
|
return $entity;
|
|
}
|
|
final public function update_form($uid)
|
|
{
|
|
try {
|
|
$entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->_product = $this->getProductModel()->getEntity([$this->_model->getPrimaryKey() => $entity->product_uid]);
|
|
$this->_viewDatas = $this->init(__FUNCTION__);
|
|
$this->_viewDatas['entity'] = $this->update_form_process($entity);
|
|
helper(['form']);
|
|
$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
|
return view($this->_viewPath . '/update', ['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());
|
|
}
|
|
}
|
|
final public function update($uid)
|
|
{
|
|
$msg = "";
|
|
try {
|
|
$entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->_product = $this->getProductModel()->getEntity([$this->_model->getPrimaryKey() => $entity->product_uid]);
|
|
$this->_viewDatas = $this->init(__FUNCTION__);
|
|
$this->update_validate($entity);
|
|
//Transaction 시작
|
|
$this->_model->transStart();
|
|
$entity = $this->update_process($entity);
|
|
//Transaction Commit
|
|
$this->_model->transComplete();
|
|
$msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 결제가 완료하였습니다.";
|
|
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/");
|
|
} catch (\Exception $e) {
|
|
//Transaction Rollback
|
|
$this->_model->transRollback();
|
|
$msg = "{$this->_viewDatas['title']}에서 결제를 실패하였습니다.\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);
|
|
}
|
|
}
|
|
|
|
//청구서관련
|
|
final protected function createBilling(OrderEntity $entity, string $subject, string $html, $response = ''): BillingEntity
|
|
{
|
|
//청구서파일 생성
|
|
$fileName = sprintf("%s_%s", $entity->getPrimaryKey(), date('Y-m-d_Hm') . '.xlsx');
|
|
$writer = $this->spreadSheet($html);
|
|
$writer->save(PATHS['BILLING'] . '/' . $fileName);
|
|
//메일발송
|
|
$this->sendBilling($this->_viewDatas['fieldDatas']['email'], $subject, $html);
|
|
$fieldDatas = [
|
|
'order_uid' => $entity->getPrimaryKey(),
|
|
'user_uid' => $this->_viewDatas['auth'][AUTH_FIELDS['ID']],
|
|
'email' => $this->_viewDatas['fieldDatas']['email'],
|
|
'phone' => $this->_viewDatas['fieldDatas']['phone'],
|
|
'title' => $subject,
|
|
'content' => $html,
|
|
'upload_file' => sprintf("%s%s%s", $this->_viewDatas['className'] . '.xlsx', DEFAULTS['DELIMITER_FILE'], $fileName),
|
|
'response' => $response,
|
|
'status' => DEFAULTS['STATUS']
|
|
];
|
|
return $this->getBillingModel()->create($fieldDatas);
|
|
}
|
|
final protected function sendBilling($email, string $subject, string $html): bool
|
|
{
|
|
$mail = \Config\Services::email();
|
|
$mail->setFrom(MALLS['support'], MALLS['title'], MALLS['master']);
|
|
$mail->setTo($email);
|
|
$mail->setCC(MALLS['master']);
|
|
$mail->setSubject($subject);
|
|
$mail->setMessage($html);
|
|
return $mail->send();
|
|
}
|
|
}
|