본문으로 건너뛰기

쿠폰 웹훅 기능 추가

PHP 8 · GODO26
변경 파일 수: 2개
변경 파일 목록
총 2개
변경 파일 코드
추가삭제
Component/Coupon/CouponAdmin.php+61-1
변경 전CouponAdmin.php
use Origin\Service\Coupon\CopyCouponService;
use Origin\Service\Coupon\UpdateCouponService;
use Origin\Service\Member\ManagerLoginService;
 
use Request;
use Session;
use UserFilePath;
$result = $this->db->set_update_db(DB_COUPON, $arrBind['param'], 'couponNo = ?', $arrBind['bind'], false);
if ($result > 0) {
echo json_encode(['result' => 'ok', 'msg' => __('쿠폰 발급상태가 변경되었습니다.')]);
 
} else {
echo json_encode(['result' => 'fail', 'msg' => __('다시 시도해 주세요.')]);
}
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**
* 쿠폰 삭제하기
*
$couponField = ['imageStorage', 'couponImage'];
$deletedCouponData = $this->getCouponInfo($couponNo, gd_implode(',', $couponField));
 
 
 
 
$arrBind = [
'i',
$couponNo,
];
// --- 삭제
$this->db->set_delete_db(DB_COUPON, 'couponNo = ?', $arrBind);
 
 
 
 
// 이미지 삭제
if (gd_isset($deletedCouponData['imageStorage']) && $deletedCouponData['imageStorage'] == 'obs') {
 
throw new \Exception(__('쿠폰 등록에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
 
break;
}
case 'modify' : {
throw new \Exception(__('쿠폰 수정에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
 
 
// 이미지를 새롭게 등록하는 경우, 기존 OBS 이미지 삭제
if ($isImageUploaded && gd_isset($couponDataBeforeUpdate['imageStorage']) && gd_isset($couponDataBeforeUpdate['couponImage'])) {
$this->deleteCouponImage($couponDataBeforeUpdate['imageStorage'], $couponDataBeforeUpdate['couponImage']);
 
throw new \Exception(__('오프라인 쿠폰 등록에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
 
break;
}
case 'modify' : {
throw new \Exception(__('오프라인 쿠폰 수정에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
 
 
// 이미지를 새롭게 등록하는 경우, 기존 OBS 이미지 삭제
if ($isImageUploaded && gd_isset($couponDataBeforeUpdate['imageStorage']) && gd_isset($couponDataBeforeUpdate['couponImage'])) {
$this->deleteCouponImage($couponDataBeforeUpdate['imageStorage'], $couponDataBeforeUpdate['couponImage']);
 
변경 후CouponAdmin.php
use Origin\Service\Coupon\CopyCouponService;
use Origin\Service\Coupon\UpdateCouponService;
use Origin\Service\Member\ManagerLoginService;
use Origin\Service\WebHook\PromotionWebHookService;
use Request;
use Session;
use UserFilePath;
$result = $this->db->set_update_db(DB_COUPON, $arrBind['param'], 'couponNo = ?', $arrBind['bind'], false);
if ($result > 0) {
echo json_encode(['result' => 'ok', 'msg' => __('쿠폰 발급상태가 변경되었습니다.')]);
$this->sendCouponSavedWebHook($arrData['couponNo'], 'UPDATED');
} else {
echo json_encode(['result' => 'fail', 'msg' => __('다시 시도해 주세요.')]);
}
}
 
/**
* 쿠폰 등록/수정 웹훅 발송
*
* @param int|string $couponNo 쿠폰번호
* @param string $actionType 'CREATED' | 'UPDATED'
*
* @return void
*/
protected function sendCouponSavedWebHook($couponNo, string $actionType)
{
try {
$couponData = $this->getCouponInfo($couponNo, 'couponNm, couponKind, couponSaveType, couponType, couponUseType');
$promotionWebHookService = \App::getInstance(PromotionWebHookService::class);
$promotionWebHookService->sendCouponUpdatedWebHook((int)$couponNo, $actionType, $couponData);
} catch (\Throwable $e) {
\App::getInstance('logger')->channel('coupon')->warning(__METHOD__ . ' 쿠폰 등록/수정 웹훅 발송 실패', [
'couponNo' => $couponNo,
'actionType' => $actionType,
'exception' => $e,
]);
}
}
 
/**
* 쿠폰 삭제 웹훅 발송
*
* @param int|string $couponNo 쿠폰번호
* @param array $deletedCouponData 삭제 전 조회한 쿠폰 정보
*
* @return void
*/
protected function sendCouponDeletedWebHook($couponNo, $deletedCouponData)
{
try {
$promotionWebHookService = \App::getInstance(PromotionWebHookService::class);
$promotionWebHookService->sendCouponUpdatedWebHook((int)$couponNo, 'DELETED', $deletedCouponData);
} catch (\Throwable $e) {
\App::getInstance('logger')->channel('coupon')->warning(__METHOD__ . ' 쿠폰 삭제 웹훅 발송 실패', [
'couponNo' => $couponNo,
'exception' => $e,
]);
}
}
 
/**
* 쿠폰 삭제하기
*
$couponField = ['imageStorage', 'couponImage'];
$deletedCouponData = $this->getCouponInfo($couponNo, gd_implode(',', $couponField));
 
// 삭제 후에는 행이 없어 웹훅 발송용 정보를 삭제 전에 조회
$couponWebHookData = $this->getCouponInfo($couponNo, 'couponNm, couponKind, couponSaveType, couponType, couponUseType');
 
$arrBind = [
'i',
$couponNo,
];
// --- 삭제
$deletedCount = $this->db->set_delete_db(DB_COUPON, 'couponNo = ?', $arrBind);
if ($deletedCount > 0) {
$this->sendCouponDeletedWebHook($couponNo, $couponWebHookData);
}
 
// 이미지 삭제
if (gd_isset($deletedCouponData['imageStorage']) && $deletedCouponData['imageStorage'] == 'obs') {
 
throw new \Exception(__('쿠폰 등록에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
$this->sendCouponSavedWebHook($couponNo, 'CREATED');
break;
}
case 'modify' : {
throw new \Exception(__('쿠폰 수정에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
$this->sendCouponSavedWebHook($couponNo, 'UPDATED');
 
// 이미지를 새롭게 등록하는 경우, 기존 OBS 이미지 삭제
if ($isImageUploaded && gd_isset($couponDataBeforeUpdate['imageStorage']) && gd_isset($couponDataBeforeUpdate['couponImage'])) {
$this->deleteCouponImage($couponDataBeforeUpdate['imageStorage'], $couponDataBeforeUpdate['couponImage']);
 
throw new \Exception(__('오프라인 쿠폰 등록에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
$this->sendCouponSavedWebHook($couponNo, 'CREATED');
break;
}
case 'modify' : {
throw new \Exception(__('오프라인 쿠폰 수정에 실패하였습니다. 고객센터에 문의해주세요.'));
}
 
$this->sendCouponSavedWebHook($couponNo, 'UPDATED');
 
// 이미지를 새롭게 등록하는 경우, 기존 OBS 이미지 삭제
if ($isImageUploaded && gd_isset($couponDataBeforeUpdate['imageStorage']) && gd_isset($couponDataBeforeUpdate['couponImage'])) {
$this->deleteCouponImage($couponDataBeforeUpdate['imageStorage'], $couponDataBeforeUpdate['couponImage']);
 
Component/Coupon/Coupon.php+36
변경 전Coupon.php
use Globals;
use Request;
use Session;
 
 
/**
* Coupon Class
$this->db->bind_param_push($arrBind['bind'], 'i', $arrData['couponNo']);
$this->db->set_update_db(DB_COUPON, $arrBind['param'], 'couponNo = ?', $arrBind['bind'], false);
$logger->info(sprintf(__('쿠폰 사용가능 상태 변경 couponNo [%1$s] date [%2$s] '), $arrData['couponNo'], $now) . Request::getReferer());
 
}
if(!$return && $couponType != 'f') {
$arrData['couponType'] = 'f';
$this->db->bind_param_push($arrBind['bind'], 'i', $arrData['couponNo']);
$this->db->set_update_db(DB_COUPON, $arrBind['param'], 'couponNo = ?', $arrBind['bind'], false);
$logger->info(sprintf(__('쿠폰 발급종료 상태 변경 [%1$s] couponNo [%2$s] date [%3$s] '), $msg, $arrData['couponNo'], $now) . Request::getReferer());
 
if ($couponIssuedQuantityCheck) {
$return = true;
}
return $return;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
public function checkCouponTypeArr($couponNoArr)
{
if(!is_array($couponNoArr)) $couponNoArr = explode(INT_DIVISION, $couponNoArr);
 
변경 후Coupon.php
use Globals;
use Request;
use Session;
use Origin\Service\WebHook\PromotionWebHookService;
 
/**
* Coupon Class
$this->db->bind_param_push($arrBind['bind'], 'i', $arrData['couponNo']);
$this->db->set_update_db(DB_COUPON, $arrBind['param'], 'couponNo = ?', $arrBind['bind'], false);
$logger->info(sprintf(__('쿠폰 사용가능 상태 변경 couponNo [%1$s] date [%2$s] '), $arrData['couponNo'], $now) . Request::getReferer());
$this->sendCouponStatusChangedWebHook($couponNo, $coupon, $arrData['couponType']);
}
if(!$return && $couponType != 'f') {
$arrData['couponType'] = 'f';
$this->db->bind_param_push($arrBind['bind'], 'i', $arrData['couponNo']);
$this->db->set_update_db(DB_COUPON, $arrBind['param'], 'couponNo = ?', $arrBind['bind'], false);
$logger->info(sprintf(__('쿠폰 발급종료 상태 변경 [%1$s] couponNo [%2$s] date [%3$s] '), $msg, $arrData['couponNo'], $now) . Request::getReferer());
$this->sendCouponStatusChangedWebHook($couponNo, $coupon, $arrData['couponType']);
if ($couponIssuedQuantityCheck) {
$return = true;
}
return $return;
}
 
/**
* 발급상태 자동 전환 웹훅 발송
*
* checkCouponType() 은 현재 상태와 무관하게 UPDATE 를 재실행하므로
* 실제로 값이 바뀌는 경우에만 발송해 프론트 요청마다 중복 발행되는 것을 막는다.
* 웹훅 필드는 checkCouponType() 의 공용 조회에 섞지 않고 상태가 바뀐 때만 따로 조회하며,
* 웹훅 실패가 발급 가능 여부 판정을 막지 않도록 예외는 로그만 남긴다.
*
* @param int|string $couponNo 쿠폰번호
* @param array|mixed $coupon 변경 전 쿠폰 정보 (발급상태 비교용)
* @param string|mixed $changedCouponType 변경된 발급상태
*
* @return void
*/
protected function sendCouponStatusChangedWebHook($couponNo, $coupon, $changedCouponType)
{
try {
$beforeCouponType = is_array($coupon) ? ($coupon['couponType'] ?? '') : '';
if ($beforeCouponType === (string)$changedCouponType) {
return;
}
 
$couponData = $this->getCouponInfo($couponNo, 'couponNm, couponKind, couponSaveType, couponType, couponUseType');
$promotionWebHookService = \App::getInstance(PromotionWebHookService::class);
$promotionWebHookService->sendCouponUpdatedWebHook((int)$couponNo, 'UPDATED', $couponData);
} catch (\Throwable $e) {
\App::getInstance('logger')->channel('coupon')->warning(__METHOD__ . ' 쿠폰 발급상태 변경 웹훅 발송 실패', [
'couponNo' => $couponNo,
'exception' => $e,
]);
}
}
 
public function checkCouponTypeArr($couponNoArr)
{
if(!is_array($couponNoArr)) $couponNoArr = explode(INT_DIVISION, $couponNoArr);