WithdrawService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Services;
  3. use App\Interfaces\WithdrawInterface;
  4. use App\Services\Msg\MailTemplateService;
  5. use Carbon\Carbon;
  6. class WithdrawService implements WithdrawInterface
  7. {
  8. private $callApiService;
  9. private $mailTemplateService;
  10. private $gateToken;
  11. public function __construct(CallApiService $callApiService, MailTemplateService $mailTemplateService)
  12. {
  13. $this->callApiService = $callApiService;
  14. $this->mailTemplateService = $mailTemplateService;
  15. }
  16. public function execute($gateToken)
  17. {
  18. $this->gateToken['main'] = $gateToken;
  19. $this->sendEmail();
  20. $this->memberFinish();
  21. }
  22. private function memberFinish()
  23. {
  24. $withdrawMemberFinish = $this->callApiService->callApi([
  25. 'url' => 'withdraw-member-finish',
  26. 'data' => [
  27. 'req' => ''
  28. ],
  29. 'headers' => [
  30. 'GateToken' => $this->getGateToken('main')
  31. ]
  32. ]);
  33. if ($this->callApiService->verifyApiError($withdrawMemberFinish)) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. private function sendEmail()
  39. {
  40. $setup = $this->getSetup();
  41. if ($this->callApiService->verifyApiError($setup)) {
  42. return false;
  43. }
  44. $dt = Carbon::now();
  45. // 예를 들어 withdraw_on에 탈톼일: 3월30일 들어가있고 3일전에 메일을 보내려고하면,
  46. // 오늘날짜가 만약 3월27일이라면 메일을 보내야하니 오늘날짜 + 3해서 시작시간부터 끝시간까지 가져옴
  47. $dt->addDays($setup['ActiveNoticeDays']);
  48. $formTimestamp = $dt->startOfDay()->timestamp;
  49. $toTimestamp = $dt->endOfDay()->timestamp;
  50. $memberPage = $this->callApiService->callApi([
  51. 'url' => 'member-page',
  52. 'data' => [
  53. 'PageVars' => [
  54. 'Query' => "withdraw_on between $formTimestamp and $toTimestamp",
  55. 'Asc' => 'withdraw_on',
  56. 'Limit' => 999999
  57. ]
  58. ],
  59. 'headers' => [
  60. 'GateToken' => $this->getGateToken('main')
  61. ]
  62. ]);
  63. if ($this->callApiService->verifyApiError($memberPage)) {
  64. return false;
  65. }
  66. foreach ($memberPage['Page'] ?? [] as $member) {
  67. $this->mailTemplateService->send('msg.dabory.pro.ko_KR.email.auth.withdraw-inform-1', [ 'C11' => $member['Email'], 'C12' => Carbon::createFromTimestamp($member['WithdrawOn'])->format('Y-m-d'), 'C13' => route('member-login') ],
  68. $member['Email'], sprintf('[%s] 탈퇴 계정 전환 안내입니다.', config('app.name')), $this->getGateToken('main'));
  69. }
  70. return true;
  71. }
  72. private function getGateToken($type)
  73. {
  74. if ($type === 'all') {
  75. return $this->gateToken;
  76. }
  77. return $this->gateToken[$type ?: 'main'];
  78. }
  79. private function getSetup()
  80. {
  81. return $this->callApiService->callApi([
  82. 'url' => 'setup-get',
  83. 'data' => [
  84. 'SetupCode' => 'withdraw-default',
  85. ],
  86. 'headers' => [
  87. 'GateToken' => $this->gateToken['main']
  88. ]
  89. ]);
  90. }
  91. }