diff --git a/packages/Webkul/Admin/src/Listeners/Order.php b/packages/Webkul/Admin/src/Listeners/Order.php index 52887d8e7..15f6470eb 100755 --- a/packages/Webkul/Admin/src/Listeners/Order.php +++ b/packages/Webkul/Admin/src/Listeners/Order.php @@ -2,191 +2,30 @@ namespace Webkul\Admin\Listeners; -use Illuminate\Support\Facades\Mail; -use Webkul\Admin\Mail\NewAdminNotification; -use Webkul\Admin\Mail\NewOrderNotification; -use Webkul\Admin\Mail\NewRefundNotification; -use Webkul\Admin\Mail\NewInvoiceNotification; -use Webkul\Admin\Mail\CancelOrderNotification; -use Webkul\Admin\Mail\NewShipmentNotification; -use Webkul\Admin\Mail\OrderCommentNotification; -use Webkul\Admin\Mail\CancelOrderAdminNotification; -use Webkul\Admin\Mail\NewInventorySourceNotification; +use Webkul\Admin\Traits\Mails; +use Webkul\Paypal\Payment\SmartButton; +use PayPalCheckoutSdk\Orders\OrdersGetRequest; +use PayPalCheckoutSdk\Orders\OrdersCaptureRequest; +use PayPalCheckoutSdk\Orders\OrdersAuthorizeRequest; +use PayPalCheckoutSdk\Payments\AuthorizationsCaptureRequest; class Order { - /** - * Send new order Mail to the customer and admin - * - * @param \Webkul\Sales\Contracts\Order $order - * @return void - */ - public function sendNewOrderMail($order) + use Mails; + + public function refundOrder($data) { - $customerLocale = $this->getLocale($order); + $orderID = $data['order']->payment->additional['orderID']; + $payerID = $data['order']->payment->additional['payerID']; + // dd($orderID, $payerID, $data['order']->payment->additional); - try { - /* email to customer */ - $configKey = 'emails.general.notifications.emails.general.notifications.new-order'; - if (core()->getConfigData($configKey)) { - $this->prepareMail($customerLocale, new NewOrderNotification($order)); - } + $smartButton = new SmartButton(); + $client = $smartButton->client(); - /* email to admin */ - $configKey = 'emails.general.notifications.emails.general.notifications.new-admin'; - if (core()->getConfigData($configKey)) { - $this->prepareMail(config('app.locale'), new NewAdminNotification($order)); - } - } catch (\Exception $e) { - report($e); - } - } + /* get order */ + // $response = $client->execute(new OrdersGetRequest($orderID)); + // dd($response); - /** - * Send new invoice mail to the customer - * - * @param \Webkul\Sales\Contracts\Invoice $invoice - * @return void - */ - public function sendNewInvoiceMail($invoice) - { - $customerLocale = $this->getLocale($invoice); - - try { - if ($invoice->email_sent) { - return; - } - - /* email to customer */ - $configKey = 'emails.general.notifications.emails.general.notifications.new-invoice'; - if (core()->getConfigData($configKey)) { - $this->prepareMail($customerLocale, new NewInvoiceNotification($invoice)); - } - } catch (\Exception $e) { - report($e); - } - } - - /** - * Send new refund mail to the customer - * - * @param \Webkul\Sales\Contracts\Refund $refund - * @return void - */ - public function sendNewRefundMail($refund) - { - $customerLocale = $this->getLocale($refund); - - try { - /* email to customer */ - $configKey = 'emails.general.notifications.emails.general.notifications.new-refund'; - if (core()->getConfigData($configKey)) { - $this->prepareMail($customerLocale, new NewRefundNotification($refund)); - } - } catch (\Exception $e) { - report($e); - } - } - - /** - * Send new shipment mail to the customer - * - * @param \Webkul\Sales\Contracts\Shipment $shipment - * @return void - */ - public function sendNewShipmentMail($shipment) - { - $customerLocale = $this->getLocale($shipment); - - try { - if ($shipment->email_sent) { - return; - } - - /* email to customer */ - $configKey = 'emails.general.notifications.emails.general.notifications.new-shipment'; - if (core()->getConfigData($configKey)) { - $this->prepareMail($customerLocale, new NewShipmentNotification($shipment)); - } - - /* email to admin */ - $configKey = 'emails.general.notifications.emails.general.notifications.new-inventory-source'; - if (core()->getConfigData($configKey)) { - $this->prepareMail(config('app.locale'), new NewInventorySourceNotification($shipment)); - } - } catch (\Exception $e) { - report($e); - } - } - - /** - * @param \Webkul\Sales\Contracts\Order $order - * @return void - */ - public function sendCancelOrderMail($order) - { - $customerLocale = $this->getLocale($order); - - try { - /* email to customer */ - $configKey = 'emails.general.notifications.emails.general.notifications.cancel-order'; - if (core()->getConfigData($configKey)) { - $this->prepareMail($customerLocale, new CancelOrderNotification($order)); - } - - /* email to admin */ - $configKey = 'emails.general.notifications.emails.general.notifications.new-admin'; - if (core()->getConfigData($configKey)) { - $this->prepareMail(config('app.locale'), new CancelOrderAdminNotification($order)); - } - } catch (\Exception $e) { - report($e); - } - } - - /** - * @param \Webkul\Sales\Contracts\OrderComment $comment - * @return void - */ - public function sendOrderCommentMail($comment) - { - $customerLocale = $this->getLocale($comment); - - if (! $comment->customer_notified) { - return; - } - - try { - /* email to customer */ - $this->prepareMail($customerLocale, new OrderCommentNotification($comment)); - } catch (\Exception $e) { - report($e); - } - } - - /** - * Get the locale of the customer if somehow item name changes then the english locale will pe provided. - * - * @param object \Webkul\Sales\Contracts\Order|\Webkul\Sales\Contracts\Invoice|\Webkul\Sales\Contracts\Refund|\Webkul\Sales\Contracts\Shipment|\Webkul\Sales\Contracts\OrderComment - * @return string - */ - private function getLocale($object) - { - if ($object instanceof \Webkul\Sales\Contracts\OrderComment) { - $object = $object->order; - } - - $objectFirstItem = $object->items->first(); - return isset($objectFirstItem->additional['locale']) ? $objectFirstItem->additional['locale'] : 'en'; - } - - /** - * Prepare Mail. - * @return void - */ - private function prepareMail($locale, $notification) - { - app()->setLocale($locale); - Mail::queue($notification); + // dd($smartButton->refundOrder($orderID, "{}")); } } diff --git a/packages/Webkul/Admin/src/Providers/EventServiceProvider.php b/packages/Webkul/Admin/src/Providers/EventServiceProvider.php index a890f9de2..6f78c994c 100755 --- a/packages/Webkul/Admin/src/Providers/EventServiceProvider.php +++ b/packages/Webkul/Admin/src/Providers/EventServiceProvider.php @@ -22,12 +22,14 @@ class EventServiceProvider extends ServiceProvider Event::listen('sales.shipment.save.after', 'Webkul\Admin\Listeners\Order@sendNewShipmentMail'); - Event::listen('sales.order.cancel.after','Webkul\Admin\Listeners\Order@sendCancelOrderMail'); + Event::listen('sales.order.cancel.after', 'Webkul\Admin\Listeners\Order@sendCancelOrderMail'); - Event::listen('sales.refund.save.after','Webkul\Admin\Listeners\Order@sendNewRefundMail'); + Event::listen('sales.refund.save.before', 'Webkul\Admin\Listeners\Order@refundOrder'); - Event::listen('sales.order.comment.create.after','Webkul\Admin\Listeners\Order@sendOrderCommentMail'); + Event::listen('sales.refund.save.after', 'Webkul\Admin\Listeners\Order@sendNewRefundMail'); - Event::listen('core.channel.update.after','Webkul\Admin\Listeners\ChannelSettingsChange@checkForMaintenaceMode'); + Event::listen('sales.order.comment.create.after', 'Webkul\Admin\Listeners\Order@sendOrderCommentMail'); + + Event::listen('core.channel.update.after', 'Webkul\Admin\Listeners\ChannelSettingsChange@checkForMaintenaceMode'); } } \ No newline at end of file diff --git a/packages/Webkul/Admin/src/Traits/Mails.php b/packages/Webkul/Admin/src/Traits/Mails.php new file mode 100644 index 000000000..33e142e42 --- /dev/null +++ b/packages/Webkul/Admin/src/Traits/Mails.php @@ -0,0 +1,197 @@ +getLocale($order); + + try { + /* email to customer */ + $configKey = 'emails.general.notifications.emails.general.notifications.new-order'; + if (core()->getConfigData($configKey)) { + $this->prepareMail($customerLocale, new NewOrderNotification($order)); + } + + /* email to admin */ + $configKey = 'emails.general.notifications.emails.general.notifications.new-admin'; + if (core()->getConfigData($configKey)) { + $this->prepareMail(config('app.locale'), new NewAdminNotification($order)); + } + } catch (\Exception $e) { + report($e); + } + } + + /** + * Send new invoice mail to the customer. + * + * @param \Webkul\Sales\Contracts\Invoice $invoice + * @return void + */ + public function sendNewInvoiceMail($invoice) + { + $customerLocale = $this->getLocale($invoice); + + try { + if ($invoice->email_sent) { + return; + } + + /* email to customer */ + $configKey = 'emails.general.notifications.emails.general.notifications.new-invoice'; + if (core()->getConfigData($configKey)) { + $this->prepareMail($customerLocale, new NewInvoiceNotification($invoice)); + } + } catch (\Exception $e) { + report($e); + } + } + + /** + * Send new refund mail to the customer. + * + * @param \Webkul\Sales\Contracts\Refund $refund + * @return void + */ + public function sendNewRefundMail($refund) + { + $customerLocale = $this->getLocale($refund); + + try { + /* email to customer */ + $configKey = 'emails.general.notifications.emails.general.notifications.new-refund'; + if (core()->getConfigData($configKey)) { + $this->prepareMail($customerLocale, new NewRefundNotification($refund)); + } + } catch (\Exception $e) { + report($e); + } + } + + /** + * Send new shipment mail to the customer. + * + * @param \Webkul\Sales\Contracts\Shipment $shipment + * @return void + */ + public function sendNewShipmentMail($shipment) + { + $customerLocale = $this->getLocale($shipment); + + try { + if ($shipment->email_sent) { + return; + } + + /* email to customer */ + $configKey = 'emails.general.notifications.emails.general.notifications.new-shipment'; + if (core()->getConfigData($configKey)) { + $this->prepareMail($customerLocale, new NewShipmentNotification($shipment)); + } + + /* email to admin */ + $configKey = 'emails.general.notifications.emails.general.notifications.new-inventory-source'; + if (core()->getConfigData($configKey)) { + $this->prepareMail(config('app.locale'), new NewInventorySourceNotification($shipment)); + } + } catch (\Exception $e) { + report($e); + } + } + + /** + * Send cancel order mail. + * + * @param \Webkul\Sales\Contracts\Order $order + * @return void + */ + public function sendCancelOrderMail($order) + { + $customerLocale = $this->getLocale($order); + + try { + /* email to customer */ + $configKey = 'emails.general.notifications.emails.general.notifications.cancel-order'; + if (core()->getConfigData($configKey)) { + $this->prepareMail($customerLocale, new CancelOrderNotification($order)); + } + + /* email to admin */ + $configKey = 'emails.general.notifications.emails.general.notifications.new-admin'; + if (core()->getConfigData($configKey)) { + $this->prepareMail(config('app.locale'), new CancelOrderAdminNotification($order)); + } + } catch (\Exception $e) { + report($e); + } + } + + /** + * Send order comment mail. + * + * @param \Webkul\Sales\Contracts\OrderComment $comment + * @return void + */ + public function sendOrderCommentMail($comment) + { + $customerLocale = $this->getLocale($comment); + + if (! $comment->customer_notified) { + return; + } + + try { + /* email to customer */ + $this->prepareMail($customerLocale, new OrderCommentNotification($comment)); + } catch (\Exception $e) { + report($e); + } + } + + /** + * Get the locale of the customer if somehow item name changes then the english locale will pe provided. + * + * @param object \Webkul\Sales\Contracts\Order|\Webkul\Sales\Contracts\Invoice|\Webkul\Sales\Contracts\Refund|\Webkul\Sales\Contracts\Shipment|\Webkul\Sales\Contracts\OrderComment + * @return string + */ + private function getLocale($object) + { + if ($object instanceof \Webkul\Sales\Contracts\OrderComment) { + $object = $object->order; + } + + $objectFirstItem = $object->items->first(); + return isset($objectFirstItem->additional['locale']) ? $objectFirstItem->additional['locale'] : 'en'; + } + + /** + * Prepare Mail. + * + * @return void + */ + private function prepareMail($locale, $notification) + { + app()->setLocale($locale); + Mail::queue($notification); + } +} \ No newline at end of file diff --git a/packages/Webkul/Paypal/src/Payment/SmartButton.php b/packages/Webkul/Paypal/src/Payment/SmartButton.php index c2315cc4f..0866e9063 100644 --- a/packages/Webkul/Paypal/src/Payment/SmartButton.php +++ b/packages/Webkul/Paypal/src/Payment/SmartButton.php @@ -5,6 +5,7 @@ namespace Webkul\Paypal\Payment; use PayPalCheckoutSdk\Core\PayPalHttpClient; use PayPalCheckoutSdk\Core\SandboxEnvironment; use PayPalCheckoutSdk\Core\ProductionEnvironment; +use PayPalCheckoutSdk\Payments\CapturesRefundRequest; class SmartButton extends Paypal { @@ -56,6 +57,19 @@ class SmartButton extends Paypal return new PayPalHttpClient($this->environment()); } + /** + * Refund order. + */ + public function refundOrder($captureId, $body = []) + { + /* generating request */ + $request = new CapturesRefundRequest($captureId); + $request->body = $body; + + /* requesting refund */ + return $this->client()->execute($request); + } + /** * Set up and return PayPal PHP SDK environment with PayPal access credentials. * This sample uses SandboxEnvironment. In production, use LiveEnvironment. diff --git a/packages/Webkul/Sales/src/Repositories/RefundRepository.php b/packages/Webkul/Sales/src/Repositories/RefundRepository.php index a494590c2..d1786b60c 100644 --- a/packages/Webkul/Sales/src/Repositories/RefundRepository.php +++ b/packages/Webkul/Sales/src/Repositories/RefundRepository.php @@ -85,10 +85,16 @@ class RefundRepository extends Repository DB::beginTransaction(); try { - Event::dispatch('sales.refund.save.before', $data); - $order = $this->orderRepository->find($data['order_id']); + /* listener will check for first element */ + Event::dispatch('sales.refund.save.before', [ + [ + 'order' => $order, + 'additional' => $data + ] + ]); + $totalQty = array_sum($data['refund']['items']); $refund = parent::create([