123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Http\Services;
- use App\Models\PromoCode;
- use App\Models\StoreOrder;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\View;
- use App\Models\User;
- class EmailService
- {
- protected $fromEmailAddress;
- protected $emailFromName;
- protected $secret;
- public $appUrl;
- public $appInternalName;
- public $stringMappingConfig;
- public function __construct()
- {
- $this->secret = 'Superman2022@';
- $this->fromEmailAddress = config('app.fromEmailAddress');
- $this->emailFromName = config('app.emailFromName');
- $this->appUrl = config('app.url');
- $this->appInternalName = config('app.internalName');
- $this->stringMappingConfig = config('constants.' . $this->appInternalName);
- }
- protected function callJava($endPoint, $data, $sessionKey)
- {
- $data['secret'] = $this->secret;
- $url = config('app.backendUrl') . $endPoint;
- $response = Http::asForm()
- ->withHeaders([
- 'sessionKey' => $sessionKey
- ])
- ->post($url, $data)
- ->body();
- return json_decode($response, true);
- }
- //Emails
- public function sendUserPasswordResetEmail(User $user)
- {
- if(!$user->getEmail()) return;
- $appInternalName = $this->appInternalName;
- $stringMappingConfig = $this->stringMappingConfig;
- $appUrl = $this->appUrl;
- $emailFromName = $this->emailFromName;
- $html = (string) view('emails.templates.user-reset-password', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
- $plainText = (string) view('emails.templates.user-reset-password-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
- $params = [
- 'fromEmailAddress' => $this->fromEmailAddress,
- 'fromName' => $this->emailFromName,
- 'toEmailAddress' => $user->getEmail(),
- 'subject' => 'Reset Password',
- 'contentHtml' => $html,
- 'contentText' => $plainText,
- 'entityType' => 'USER',
- 'entityUid' => $user->uid,
- ];
- $response = $this->callJava('/api/email/send', $params, null);
- }
- public function sendOrderInvoice(StoreOrder $storeOrder)
- {
- $user = $storeOrder->user;
- if(!$user) return;
- if(!@$user->getEmail()) return;
- $appInternalName = $this->appInternalName;
- $stringMappingConfig = $this->stringMappingConfig;
- $appUrl = $this->appUrl;
- $emailFromName = $this->emailFromName;
- $html = (string) view('emails.templates.invoice', compact('user', 'storeOrder', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
- $plainText = (string) view('emails.templates.invoice-txt', compact('user', 'storeOrder', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
- $params = [
- 'fromEmailAddress' => $this->fromEmailAddress,
- 'fromName' => $this->emailFromName,
- 'toEmailAddress' => $user->getEmail(),
- 'subject' => 'Invoice from ' . $stringMappingConfig['name'] . ' #' . $storeOrder->iid ?? $storeOrder->id,
- 'contentHtml' => $html,
- 'contentText' => $plainText,
- 'entityType' => 'USER',
- 'entityUid' => $user->uid,
- ];
- $response = $this->callJava('/api/email/send', $params, null);
- }
- public function notifyUserOnFailedTransaction(User $user)
- {
- if(!@$user->getEmail()) return;
- $appInternalName = $this->appInternalName;
- $stringMappingConfig = $this->stringMappingConfig;
- $appUrl = $this->appUrl;
- $emailFromName = $this->emailFromName;
- $html = (string) view('emails.templates.user-failed-order-charge', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
- $plainText = (string) view('emails.templates.user-failed-order-charge-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
- $params = [
- 'fromEmailAddress' => $this->fromEmailAddress,
- 'fromName' => $this->emailFromName,
- 'toEmailAddress' => $user->getEmail(),
- 'subject' => 'Failed to process card payment',
- 'contentHtml' => $html,
- 'contentText' => $plainText,
- 'entityType' => 'USER',
- 'entityUid' => $user->uid,
- ];
- $response = $this->callJava('/api/email/send', $params, null);
- }
- public function notifyUserOnShippedOrder(User $user, $shippingDetails)
- {
- if(!@$user->getEmail()) return;
- $appInternalName = $this->appInternalName;
- $stringMappingConfig = $this->stringMappingConfig;
- $appUrl = $this->appUrl;
- $emailFromName = $this->emailFromName;
- $html = (string) view('emails.templates.user-order-shipped', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'shippingDetails'));
- $plainText = (string) view('emails.templates.user-order-shipped-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'shippingDetails'));
- $params = [
- 'fromEmailAddress' => $this->fromEmailAddress,
- 'fromName' => $this->emailFromName,
- 'toEmailAddress' => $user->getEmail(),
- 'subject' => 'Your order is on the way',
- 'contentHtml' => $html,
- 'contentText' => $plainText,
- 'entityType' => 'USER',
- 'entityUid' => $user->uid,
- ];
- $response = $this->callJava('/api/email/send', $params, null);
- }
- public function sendUserWelcomeEmail(User $user, $temporaryPassword = null)
- {
- if (!$user->getEmail()) return;
- $appInternalName = $this->appInternalName;
- $stringMappingConfig = $this->stringMappingConfig;
- $appUrl = $this->appUrl;
- $emailFromName = $this->emailFromName;
- $html = (string) view('emails.templates.user-welcome', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'temporaryPassword'));
- $plainText = (string) view('emails.templates.user-welcome-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'temporaryPassword'));
- $params = [
- 'fromEmailAddress' => $this->fromEmailAddress,
- 'fromName' => $this->emailFromName,
- 'toEmailAddress' => $user->getEmail(),
- 'subject' => 'Welcome!',
- 'contentHtml' => $html,
- 'contentText' => $plainText,
- 'entityType' => 'USER',
- 'entityUid' => $user->uid,
- ];
- $response = $this->callJava('/api/email/send', $params, null);
- }
- }
|