PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.4
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Modules / PaymentMethods / Cod / Cod.php

Cod.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.5.4, at app/Modules/PaymentMethods/Cod/Cod.php

228 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\PaymentMethods\Cod;
4
5 use FluentCart\Api\Resource\OrderResource;
6 use FluentCart\App\Events\Order\OrderStatusUpdated;
7 use FluentCart\App\Services\DateTime\DateTime;
8 use FluentCart\App\Helpers\Status;
9 use FluentCart\App\Models\Subscription;
10 use FluentCart\App\Modules\PaymentMethods\Core\AbstractPaymentGateway;
11 use FluentCart\App\Modules\PaymentMethods\Core\BaseGatewaySettings;
12 use FluentCart\App\Services\Payments\PaymentInstance;
13 use FluentCart\App\Vite;
14 use FluentCart\Framework\Support\Arr;
15
16
17 class Cod extends AbstractPaymentGateway
18 {
19 public array $supportedFeatures = ['payment', 'refund', 'subscriptions'];
20
21 public BaseGatewaySettings $settings;
22
23 public function boot()
24 {
25 add_action('fluent_cart/payment_paid', [$this, 'handlePaymentPaid'], 10, 1);
26 add_filter('fluent_cart/payment_method_list_class', [$this, 'getPaymentMethodClass'], 10, 2);
27 }
28
29 public function getPaymentMethodClass($class, $data): string
30 {
31 $route = Arr::get($data, 'route');
32 return $route === 'offline_payment'?'no-padding' : '';
33 }
34
35 public function meta(): array
36 {
37 return [
38 'title' => __('Cash', 'fluent-cart'),
39 'route' => 'offline_payment',
40 'slug' => 'offline_payment',
41 'description' => esc_html__('Pay with cash upon delivery', 'fluent-cart'),
42 'logo' => Vite::getAssetUrl("images/payment-methods/offline-payment.svg"),
43 'logo_light' => Vite::getAssetUrl("images/payment-methods/offline-payment-light.svg"),
44 'icon' => Vite::getAssetUrl("images/payment-methods/cod-icon.svg"),
45 'brand_color' => '#136196',
46 'upcoming' => false,
47 'status' => $this->settings->get('is_active') === 'yes',
48 ];
49 }
50
51 public function __construct()
52 {
53 parent::__construct(
54 new CodSettingsBase(),
55 new CodSubscriptions()
56 );
57 }
58
59 public function makePaymentFromPaymentInstance(PaymentInstance $paymentInstance)
60 {
61 try {
62 return [
63 'status' => 'success',
64 'message' => __('Order has been placed successfully', 'fluent-cart'),
65 'redirect_to' => (new CodHandler($this))->handlePayment($paymentInstance)
66 ];
67
68 } catch (\Exception $e) {
69 return [
70 'status' => 'failed',
71 'message' => $e->getMessage(),
72 ];
73 }
74 }
75
76
77 public function handlePaymentPaid($params)
78 {
79 $orderId = Arr::get($params, 'order.id');
80 // get the order with the id
81 $order = OrderResource::getQuery()->find($orderId);
82
83 if (!$order) {
84 return;
85 }
86
87 $orderStatus = Arr::get($params, 'order.status');
88 $fulfillmentType = Arr::get($params, 'order.fulfillment_type');
89 $paymentMethod = Arr::get($params, 'order.payment_method');
90
91 //if new status is processing and payment_method is offline_payment and fulfillment_type is digital then make it completed
92 if ($orderStatus === Status::ORDER_PROCESSING && $paymentMethod === 'offline_payment' && $fulfillmentType === 'digital') {
93 $order->status = Status::ORDER_COMPLETED;
94 $order->completed_at = DateTime::gmtNow();
95 $order->save();
96
97 $actionActivity = [
98 'title' => __('Order status updated', 'fluent-cart'),
99 'content' => sprintf(
100 /* translators: 1: old status, 2: new status */
101 __('Order status has been updated from %1$s to %2$s', 'fluent-cart'),
102 $orderStatus,
103 $order->status
104 )
105 ];
106
107 (new OrderStatusUpdated($order, $orderStatus, Status::ORDER_COMPLETED, (bool) true, $actionActivity, 'order_status'))->dispatch();
108
109 $this->maybeUpdateSubscription($params);
110
111 } else if ($orderStatus === Status::ORDER_PROCESSING && $paymentMethod === 'offline_payment' && $fulfillmentType === 'physical') {
112 $this->maybeUpdateSubscription($params);
113 }
114
115 return;
116 }
117
118 public function maybeUpdateSubscription($params)
119 {
120 $order = Arr::get($params, 'order');
121 $orderId = Arr::get($params, 'order.id');
122 $orderStatus = Arr::get($params, 'order.status');
123
124 if (!$order) {
125 return;
126 }
127
128 if (!in_array($orderStatus, [Status::ORDER_PROCESSING, Status::ORDER_COMPLETED])) {
129 return;
130 }
131
132 // just search in subscription table with order id get every subscription
133 $subscriptions = Subscription::query()->where('parent_order_id', $orderId)->get();
134
135 // update subscription status
136 foreach ($subscriptions as $subscription) {
137 if ($subscription->status === 'active') {
138 continue;
139 }
140 $subscription->status = 'active';
141 $subscription->save();
142 }
143 }
144
145 public function getEnqueueScriptSrc($hasSubscription = 'no'): array
146 {
147 return [
148 [
149 'handle' => 'fluent-cart-checkout-handler-cod',
150 'src' => Vite::getEnqueuePath('public/payment-methods/cod-checkout.js'),
151 ]
152 ];
153 }
154
155 public function getLocalizeData(): array
156 {
157 return [
158 'fct_cod_data' => [
159 'translations' => [
160 'Cash upon delivery, bank transfer or other manual process.' => __('Cash upon delivery, bank transfer or other manual process.', 'fluent-cart'),
161 ]
162 ]
163 ];
164 }
165
166 public function fields()
167 {
168 return array(
169 'cod_description' => array(
170 'value' =>
171 wp_kses(sprintf(
172 "<div class='pt-4'>
173 <p>%s</p>
174 </div>",
175 __('�
176 Customers can pay for their orders by cash upon delivery. You may receive using bank transfer or other manual process as well.
177 You may remind them to have the exact amount ready for our delivery personnel!', 'fluent-cart')
178 ),
179 [
180 'p' => [],
181 'div' => ['class' => true],
182 'i' => [],
183 ]),
184 'label' => __('Webhook URL', 'fluent-cart'),
185 'type' => 'html_attr'
186 ),
187 );
188
189 }
190
191 public static function validateSettings($data): array
192 {
193 return [
194 'status' => 'success',
195 'message' => __('Settings saved successfully', 'fluent-cart')
196 ];
197 }
198
199 public function maybeUpdatePayments($orderHash)
200 {
201 $updateData = [
202 'status' => Status::PAYMENT_PENDING
203 ];
204 $order = OrderResource::getOrderByHash($orderHash);
205 $this->updateOrderDataByOrder($order, $updateData, null);
206 }
207
208 public function refund($refundInfo, $orderData, $transaction)
209 {
210 // cod does not have any refund process
211 }
212
213 public function webHookPaymentMethodName()
214 {
215 return $this->getMeta('route');
216 }
217
218 public function handleIPN()
219 {
220 // cod does not have any ipn
221 }
222
223 public function getOrderInfo(array $data)
224 {
225 // TODO: Implement getOrderInfo() method.
226 }
227 }
228