EmailService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Http\Services;
  3. use App\Models\PromoCode;
  4. use App\Models\StoreOrder;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\View;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Models\User;
  9. class EmailService
  10. {
  11. protected $fromEmailAddress;
  12. protected $emailFromName;
  13. protected $secret;
  14. public $appUrl;
  15. public $appInternalName;
  16. public $stringMappingConfig;
  17. public function __construct()
  18. {
  19. $this->secret = 'Superman2022@';
  20. $this->fromEmailAddress = config('app.fromEmailAddress');
  21. $this->emailFromName = config('app.emailFromName');
  22. $this->appUrl = config('app.url');
  23. $this->appInternalName = config('app.internalName');
  24. $this->stringMappingConfig = config('constants.' . $this->appInternalName);
  25. }
  26. protected function callJava($endPoint, $data, $sessionKey, $attachment = null)
  27. {
  28. $data['secret'] = $this->secret;
  29. $url = config('app.backendUrl') . $endPoint;
  30. if($attachment){
  31. $response = Http::asMultipart()
  32. ->attach(
  33. $attachment['fileName'],
  34. $attachment['contents'],
  35. $attachment['name']
  36. )
  37. ->withHeaders([
  38. 'sessionKey' => $sessionKey
  39. ])
  40. ->post($url, $data)
  41. ->body();
  42. }else{
  43. $response = Http::asForm()
  44. ->withHeaders([
  45. 'sessionKey' => $sessionKey
  46. ])
  47. ->post($url, $data)
  48. ->body();
  49. }
  50. return json_decode($response, true);
  51. }
  52. //Emails
  53. public function sendUserPasswordResetEmail(User $user)
  54. {
  55. if(!$user->getEmail()) return;
  56. $appInternalName = $this->appInternalName;
  57. $stringMappingConfig = $this->stringMappingConfig;
  58. $appUrl = $this->appUrl;
  59. $emailFromName = $this->emailFromName;
  60. $html = (string) view('emails.templates.user-reset-password', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  61. $plainText = (string) null;
  62. $params = [
  63. 'fromEmailAddress' => $this->fromEmailAddress,
  64. 'fromName' => $this->emailFromName,
  65. 'toEmailAddress' => $user->getEmail(),
  66. 'subject' => 'Reset Password',
  67. 'contentHtml' => $html,
  68. 'contentText' => $plainText,
  69. 'entityType' => 'USER',
  70. 'entityUid' => $user->uid,
  71. ];
  72. $response = $this->callJava('/api/email/send', $params, null);
  73. }
  74. public function sendOrderInvoice(StoreOrder $storeOrder)
  75. {
  76. $user = $storeOrder->user;
  77. if(!$user) return;
  78. if(!@$user->getEmail()) return;
  79. $appInternalName = $this->appInternalName;
  80. $stringMappingConfig = $this->stringMappingConfig;
  81. $appUrl = $this->appUrl;
  82. $emailFromName = $this->emailFromName;
  83. $html = (string) view('emails.templates.invoice', compact('user', 'storeOrder', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  84. $plainText = (string) view('emails.templates.invoice-txt', compact('user', 'storeOrder', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  85. $params = [
  86. 'fromEmailAddress' => $this->fromEmailAddress,
  87. 'fromName' => $this->emailFromName,
  88. 'toEmailAddress' => $user->getEmail(),
  89. 'subject' => 'Invoice from ' . $stringMappingConfig['name'] . ' #' . $storeOrder->iid ?? $storeOrder->id,
  90. 'contentHtml' => $html,
  91. 'contentText' => $plainText,
  92. 'entityType' => 'USER',
  93. 'entityUid' => $user->uid,
  94. ];
  95. $response = $this->callJava('/api/email/send', $params, null);
  96. }
  97. public function notifyUserOnFailedTransaction(User $user, StoreOrder $storeOrder)
  98. {
  99. if(!@$user->getEmail()) return;
  100. $appInternalName = $this->appInternalName;
  101. $stringMappingConfig = $this->stringMappingConfig;
  102. $appUrl = $this->appUrl;
  103. $emailFromName = $this->emailFromName;
  104. $html = (string) view('emails.templates.user-failed-order-charge', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  105. $plainText = (string) view('emails.templates.user-failed-order-charge-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  106. $params = [
  107. 'fromEmailAddress' => $this->fromEmailAddress,
  108. 'fromName' => $this->emailFromName,
  109. 'toEmailAddress' => $user->getEmail(),
  110. 'subject' => 'Failed to process card payment',
  111. 'contentHtml' => $html,
  112. 'contentText' => $plainText,
  113. 'entityType' => 'USER',
  114. 'entityUid' => $user->uid,
  115. ];
  116. $response = $this->callJava('/api/email/send', $params, null);
  117. }
  118. public function notifyUserOnShippedOrder(User $user, $shippingDetails)
  119. {
  120. if(!@$user->getEmail()) return;
  121. $appInternalName = $this->appInternalName;
  122. $stringMappingConfig = $this->stringMappingConfig;
  123. $appUrl = $this->appUrl;
  124. $emailFromName = $this->emailFromName;
  125. $html = (string) view('emails.templates.user-order-shipped', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'shippingDetails'));
  126. $plainText = (string) view('emails.templates.user-order-shipped-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'shippingDetails'));
  127. $params = [
  128. 'fromEmailAddress' => $this->fromEmailAddress,
  129. 'fromName' => $this->emailFromName,
  130. 'toEmailAddress' => $user->getEmail(),
  131. 'subject' => 'Your order is on the way',
  132. 'contentHtml' => $html,
  133. 'contentText' => $plainText,
  134. 'entityType' => 'USER',
  135. 'entityUid' => $user->uid,
  136. ];
  137. $response = $this->callJava('/api/email/send', $params, null);
  138. }
  139. public function sendUserWelcomeEmail(User $user, $temporaryPassword = null)
  140. {
  141. if (!$user->getEmail()) return;
  142. $appInternalName = $this->appInternalName;
  143. $stringMappingConfig = $this->stringMappingConfig;
  144. $appUrl = $this->appUrl;
  145. $emailFromName = $this->emailFromName;
  146. $html = (string) view('emails.templates.user-welcome', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'temporaryPassword'));
  147. $plainText = (string) view('emails.templates.user-welcome-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'temporaryPassword'));
  148. $params = [
  149. 'fromEmailAddress' => $this->fromEmailAddress,
  150. 'fromName' => $this->emailFromName,
  151. 'toEmailAddress' => $user->getEmail(),
  152. 'subject' => 'Welcome!',
  153. 'contentHtml' => $html,
  154. 'contentText' => $plainText,
  155. 'entityType' => 'USER',
  156. 'entityUid' => $user->uid,
  157. ];
  158. $response = $this->callJava('/api/email/send', $params, null);
  159. }
  160. public function sendUserOrderChargeSuccessful(User $user, StoreOrder $storeOrder)
  161. {
  162. if (!$user->getEmail()) return;
  163. $appInternalName = $this->appInternalName;
  164. $stringMappingConfig = $this->stringMappingConfig;
  165. $appUrl = $this->appUrl;
  166. $emailFromName = $this->emailFromName;
  167. $html = (string) view('emails.templates.user-payment-successful', compact('storeOrder', 'user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  168. $plainText = (string) '';
  169. $params = [
  170. 'fromEmailAddress' => $this->fromEmailAddress,
  171. 'fromName' => $this->emailFromName,
  172. 'toEmailAddress' => $user->getEmail(),
  173. 'subject' => 'Order Charged successfully!',
  174. 'contentHtml' => $html,
  175. 'contentText' => $plainText,
  176. 'entityType' => 'USER',
  177. 'entityUid' => $user->uid,
  178. ];
  179. $response = $this->callJava('/api/email/send', $params, null);
  180. }
  181. public function sendUserCustomEmail(User $user, array $emailData)
  182. {
  183. if (!@$emailData['to']) return;
  184. $appInternalName = $this->appInternalName;
  185. $stringMappingConfig = $this->stringMappingConfig;
  186. $appUrl = $this->appUrl;
  187. $emailFromName = $this->emailFromName;
  188. $html = (string) view('emails.templates.user-custom-email', compact('user', 'emailData', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  189. $plainText = (string) '';
  190. $params = [
  191. 'fromEmailAddress' => $this->fromEmailAddress,
  192. 'fromName' => $this->emailFromName,
  193. 'toEmailAddress' => @$emailData['to'],
  194. 'subject' => @$emailData['subject'],
  195. 'contentHtml' => $html,
  196. 'contentText' => $plainText,
  197. 'entityType' => 'USER',
  198. 'entityUid' => $user->uid,
  199. ];
  200. $response = $this->callJava('/api/email/send', $params, null);
  201. }
  202. public function sendEmailWithAttachment($params)
  203. {
  204. if (!@$params['toEmail']) return;
  205. $appInternalName = $this->appInternalName;
  206. $stringMappingConfig = $this->stringMappingConfig;
  207. $appUrl = $this->appUrl;
  208. $emailFromName = $this->emailFromName;
  209. $html = (string) view('emails.templates.attachment', compact('params', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
  210. $plainText = (string) '';
  211. // $attachment = $this->getAttachmentFromStoragePath($params['attachmentPath'], 'attachment1');
  212. $emailParams = [
  213. 'fromEmailAddress' => $this->fromEmailAddress,
  214. 'fromName' => $this->emailFromName,
  215. 'toEmailAddress' => @$params['toEmail'],
  216. 'subject' => @$params['subject'],
  217. 'contentHtml' => $html,
  218. 'contentText' => $plainText,
  219. 'entityType' => null,
  220. 'entityUid' => null
  221. // 'attachment1' => $attachment
  222. ];
  223. $response = $this->callJava('/api/email/send', $emailParams, null);
  224. }
  225. protected function getAttachmentFromStoragePath($path, $fileName){
  226. if (Storage::disk('custom')->exists($path)) {
  227. return [
  228. 'fileName' => $fileName,
  229. 'name' => basename($path),
  230. 'contents' => Storage::disk('custom')->get($path),
  231. 'mime' => Storage::disk('custom')->mimeType($path),
  232. ];
  233. }
  234. return null;
  235. }
  236. }