PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
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 / Services / Payments / PaymentHelper.php

PaymentHelper.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at app/Services/Payments/PaymentHelper.php

247 lines 10.0 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\Services\Payments;
4
5 use FluentCart\Api\StoreSettings;
6 use FluentCart\App\Helpers\Helper;
7 use FluentCart\App\Helpers\Status;
8 use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager;
9 use FluentCart\App\Services\URL;
10 use FluentCart\Framework\Support\Arr;
11
12 class PaymentHelper
13 {
14 public string $slug = '';
15
16 public function __construct($slug)
17 {
18 $this->slug = $slug;
19 }
20
21 public function listenerUrl($args = [])
22 {
23 $listener = '?fct_payment_listener=1&method=' . $this->slug;
24 $data = apply_filters_deprecated('fluent_cart_ipn_url_' . $this->slug, [
25 ['listener_url' => site_url($listener)]
26 ], '1.3.16', 'fluent_cart/ipn_url_' . $this->slug, 'Use fluent_cart/ipn_url_' . $this->slug . ' instead of fluent_cart_ipn_url_' . $this->slug . '.');
27
28 return apply_filters('fluent_cart/ipn_url_' . $this->slug, $data);
29 }
30
31 public function successUrl($uuid, $args = null)
32 {
33 $queryArgs = array_merge(
34 array(
35 'method' => $this->slug,
36 'trx_hash' => $uuid,
37 'fct_redirect' => 'yes'
38 ),
39 is_array($args) ? $args : []
40 );
41
42 $receiptUrl = (new StoreSettings())->getReceiptPage();
43
44 if (empty($receiptUrl)) {
45 $receiptUrl = site_url();
46 }
47
48 $context = [
49 'transaction_hash' => $uuid,
50 'args' => $args,
51 'payment_method' => $this->slug ?? ''
52 ];
53 $url = apply_filters_deprecated('fluentcart/payment/success_url', [add_query_arg($queryArgs, $receiptUrl), $context], '1.3.16', 'fluent_cart/payment/success_url', 'Use fluent_cart/payment/success_url instead of fluentcart/payment/success_url.');
54
55 return apply_filters('fluent_cart/payment/success_url', $url, $context);
56 }
57
58 public static function getCustomPaymentLink($orderHash): string
59 {
60 return wp_sanitize_redirect(
61 URL::appendQueryParams(home_url('/?fluent-cart=custom_checkout'), [
62 'order_hash' => $orderHash,
63 ]));
64 }
65
66 /*
67 *This will be hooked from basePaymentMethod
68 * validate payment method is active for checkout items, before order creation
69 */
70 public static function validateAndGetPayMethod($cartCheckoutHelper, $orderData, $extraCharge = 0)
71 {
72 $paymentMethod = Arr::get($orderData, 'others._fct_pay_method');
73 $isZeroPayment = $cartCheckoutHelper->getItemsAmountTotal(false, false) + $extraCharge <= 0;
74 if ($isZeroPayment && $cartCheckoutHelper->getCart()->getEstimatedRecurringTotal() <= 0) {
75 $paymentMethod = apply_filters('fluent_cart/default_payment_method_for_zero_payment', 'offline_payment', []);
76 }
77
78 if (!GatewayManager::has($paymentMethod)) {
79 wp_send_json([
80 'status' => 'failed',
81 'message' => __('No valid payment method found!', 'fluent-cart'),
82 'data' => []
83 ], 423
84 );
85 };
86
87 $gateway = GatewayManager::getInstance($paymentMethod);
88 $status = $gateway->validatePaymentMethod([
89 'isValid' => false,
90 'reason' => __('No payment method found!', 'fluent-cart'),
91 'isZeroPayment' => $isZeroPayment
92 ]);
93
94 if (!Arr::get($status, 'isValid')) {
95 wp_send_json(
96 [
97 'status' => 'failed',
98 'message' => Arr::get($status, 'reason'),
99 'data' => []
100 ], 423
101 );
102 }
103
104 return $paymentMethod;
105 }
106
107 public static function updateTransactionRefundedTotal($parentTransaction, $refundedAmount)
108 {
109 // update the transaction. only update refunded_total, in meta, if exist add
110 $meta = $parentTransaction->meta;
111 $alreadyRefunded = Arr::get($meta, 'refunded_total', 0);
112 $meta['refunded_total'] = $alreadyRefunded + $refundedAmount;
113 $parentTransaction->meta = $meta;
114 $parentTransaction->save();
115 }
116
117 // public static function updateOrderRefundedTotal($order, $refundedAmount, &$type): void
118 // {
119 // // update order data
120 // $netOrderPaidAmount = $order->total_paid - $order->total_refunded;
121 // $isFullRefund = $refundedAmount == $netOrderPaidAmount;
122 //
123 // if ($isFullRefund) {
124 // $order->payment_status = Status::PAYMENT_REFUNDED;
125 // $type = 'full';
126 // } else {
127 // $order->payment_status = Status::PAYMENT_PARTIALLY_REFUNDED;
128 // $type = 'partial';
129 // }
130 // $order->total_refund += $refundedAmount;
131 // $order->save();
132 // }
133
134 /**
135 * @param string $paymentMethod
136 * @param array $paymentMethodDetails
137 * @param array $additionalData
138 * @return array
139 * parse payment method details and return a common format
140 * filter hook: 'fluent_cart/payments/parse_payment_method_details'
141 */
142 public static function parsePaymentMethodDetails($paymentGateway, $paymentMethodDetails, $additionalData = []): array
143 {
144 $billingInfo = [
145 'method' => $paymentGateway,
146 'type' => null,
147 'details' => [],
148 'billing_details' => [
149 'name' => null,
150 'email' => null,
151 'phone' => null,
152 'address' => [
153 'country' => null,
154 'postal_code' => null,
155 'line1' => null,
156 'line2' => null,
157 'city' => null,
158 'state' => null,
159 ]
160 ]
161 ];
162
163 if ($paymentGateway === 'stripe') {
164 $type = Arr::get($paymentMethodDetails, 'type', 'card');
165 $billingInfo['type'] = $type;
166
167 if ($type === 'card') {
168 $billingInfo['details'] = [
169 'type' => 'card',
170 'brand' => sanitize_text_field(Arr::get($paymentMethodDetails, 'card.brand')),
171 'last_4' => sanitize_text_field(Arr::get($paymentMethodDetails, 'card.last4')),
172 'exp_month' => sanitize_text_field(Arr::get($paymentMethodDetails, 'card.exp_month')),
173 'exp_year' => sanitize_text_field(Arr::get($paymentMethodDetails, 'card.exp_year')),
174 'fingerprint' => sanitize_text_field(Arr::get($paymentMethodDetails, 'card.fingerprint')),
175 'payment_method_id' => sanitize_text_field(Arr::get($paymentMethodDetails, 'id'))
176 ];
177 } else {
178 // For other Stripe payment methods (ACH, SEPA, etc.)
179 $billingInfo['details'] = [
180 'payment_method_id' => sanitize_text_field(Arr::get($paymentMethodDetails, 'id')),
181 'type' => Arr::get($paymentMethodDetails, $type, [])
182 ];
183 }
184
185 // Common billing details for Stripe
186 $billingInfo['billing_details'] = [
187 'name' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.name')),
188 'email' => sanitize_email(Arr::get($paymentMethodDetails, 'billing_details.email', '')),
189 'phone' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.phone')),
190 'address' => [
191 'country' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.address.country')),
192 'postal_code' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.address.postal_code')),
193 'line1' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.address.line1')),
194 'line2' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.address.line2')),
195 'city' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.address.city')),
196 'state' => sanitize_text_field(Arr::get($paymentMethodDetails, 'billing_details.address.state')),
197 ]
198 ];
199
200 } elseif ($paymentGateway === 'paypal') {
201 $billingInfo['type'] = 'standard';
202 $billingInfo['details'] = [
203 'email' => sanitize_email(Arr::get($paymentMethodDetails, 'email', '')),
204 'payer_id' => sanitize_text_field(Arr::get($paymentMethodDetails, 'payer_id')),
205 ];
206
207 // PayPal billing details
208 $billingInfo['billing_details'] = [
209 'name' => sanitize_text_field(Arr::get($paymentMethodDetails, 'name')),
210 'email' => sanitize_email(Arr::get($paymentMethodDetails, 'email', '')),
211 'phone' => sanitize_text_field(Arr::get($paymentMethodDetails, 'phone')),
212 'address' => [
213 'country' => sanitize_text_field(Arr::get($paymentMethodDetails, 'address.country_code')),
214 'postal_code' => sanitize_text_field(Arr::get($paymentMethodDetails, 'address.postal_code')),
215 'line1' => sanitize_text_field(Arr::get($paymentMethodDetails, 'address.address_line_1')),
216 'line2' => sanitize_text_field(Arr::get($paymentMethodDetails, 'address.address_line_2')),
217 ]
218 ];
219 }
220
221 return $billingInfo;
222 }
223
224
225 public static function getIntervalDays($interval = ''): int
226 {
227 if ($interval === 'yearly') {
228 $days = 365;
229 } elseif ($interval === 'monthly') {
230 $days = 30;
231 } elseif ($interval === 'weekly') {
232 $days = 7;
233 } elseif ($interval === 'quarterly') {
234 $days = 90;
235 } elseif ($interval === 'half_yearly') {
236 $days = 182;
237 } else {
238 $days = 1;
239 }
240
241 return apply_filters('fluent_cart/subscription_interval_in_days', $days, [
242 'interval' => $interval
243 ]);
244 }
245
246 }
247