PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.1
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 trunk All 48 releases
fluent-cart / app / Modules / PaymentMethods / StripeGateway / UpdateCustomerPaymentMethod.php

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

107 lines 3.8 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\StripeGateway;
4
5 use FluentCart\App\Models\Subscription;
6 use FluentCart\App\Models\SubscriptionMeta;
7 use FluentCart\App\Modules\PaymentMethods\StripeGateway\API\API;
8 use FluentCart\App\Services\Payments\PaymentHelper;
9 use FluentCart\Framework\Support\Arr;
10
11 class UpdateCustomerPaymentMethod
12 {
13 private SubscriptionsManager $subscriptions;
14
15 public function __construct()
16 {
17 $this->subscriptions = new SubscriptionsManager();
18 }
19
20 /**
21 * @throws \Exception
22 */
23 public function update($data, $subscriptionId)
24 {
25 $vendorSubscriptionId = Arr::get($data, 'vendor_subscription_id');
26 $newPaymentMethod = Arr::get($data, 'newPaymentMethod');
27 $verificationStatus = Arr::get($data, 'verification_status', '');
28
29 if (!$vendorSubscriptionId) {
30 return;
31 }
32
33 // get customer id
34 $subscription = Subscription::query()->where('vendor_subscription_id', $vendorSubscriptionId)->first();
35 $customerId = $subscription->vendor_customer_id;
36
37 $newPaymentMethodId = Arr::get($newPaymentMethod, 'id');
38
39 if ('verify' === $verificationStatus) {
40 // verify the payment method
41 $this->subscriptions::verifyPaymentMethod($newPaymentMethodId, $customerId, true);
42 }
43
44
45 $response = (new API())->createStripeObject('payment_methods/' . $newPaymentMethodId . '/attach', ['customer' => $customerId]);
46
47 if (is_wp_error($response)) {
48 wp_send_json([
49 'status' => 'failed',
50 'message' => $response->get_error_message()
51 ], 423);
52 }
53
54 // now update the subscription's default payment method ,
55 // ensures that the subscription will use the new payment method for the next invoice and all subsequent invoices
56 static::updateSubscriptionDefaultPaymentMethod($vendorSubscriptionId, $newPaymentMethodId, $newPaymentMethod, $customerId, $subscriptionId);
57
58 }
59
60
61 public static function updateSubscriptionDefaultPaymentMethod($vendorSubscriptionId, $newPaymentMethodId, $paymentMethod, $customerId, $subscriptionId, $customerDefault = false)
62 {
63 // attach payment method to customer and make it the default payment method for the subscription
64 $subscriptionData = [
65 'default_payment_method' => $newPaymentMethodId
66 ];
67
68 $response = (new API())->createStripeObject('subscriptions/' . $vendorSubscriptionId, $subscriptionData);
69
70 if (is_wp_error($response)) {
71 wp_send_json([
72 'status' => 'failed',
73 'message' => $response->get_error_message()
74 ], 423);
75 }
76
77 if ($customerDefault) {
78 $response = (new API())->createStripeObject('customers/' . $customerId, ['invoice_settings' => ['default_payment_method' => $newPaymentMethodId]]);
79 if (is_wp_error($response)) {
80 wp_send_json([
81 'status' => 'failed',
82 'message' => $response->get_error_message()
83 ], 423);
84 }
85 }
86
87 // update active payment method subscription meta
88 $billingInfo = PaymentHelper::parsePaymentMethodDetails('stripe', $paymentMethod);
89
90 SubscriptionMeta::updateOrCreate([
91 'subscription_id' => $subscriptionId,
92 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
93 'meta_key' => 'active_payment_method'
94 ], [
95 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
96 'meta_value' => json_encode($billingInfo)
97 ]);
98
99 wp_send_json([
100 'status' => 'success',
101 'message' => __('Payment method updated successfully', 'fluent-cart'),
102 'action' => 'none',
103 'data' => $billingInfo
104 ], 200);
105 }
106 }
107