PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.3
Booktics – Appointment Booking Calendar for Service Businesses v1.0.3
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / module / stripe-addon / service / stripe-payment-method.php

stripe-payment-method.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.3, at core/module/stripe-addon/service/stripe-payment-method.php

167 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Module\Stripe_Addon\Service;
4
5 use Booktics\Models\Order_Model;
6 use Booktics\Module\Stripe_Addon\Handler\Stripe_Handler;
7 use Booktics\Payment\Payment_Methods\Abstract_Payment_Method;
8 use Exception;
9 use Stripe\PaymentIntent;
10 use Stripe\Refund;
11 use Stripe\Stripe;
12
13 class Stripe_Payment_Method extends Abstract_Payment_Method {
14
15 /**
16 * Initialize the Stripe SDK with the secret key.
17 *
18 * @return void
19 */
20 private function init_stripe_sdk() {
21 Stripe::setApiKey( Stripe_Handler::get_stripe_secret_key() );
22 }
23
24 /**
25 * Get the Stripe connect account URL.
26 *
27 * @return string The connect URL
28 */
29 public static function connect_account( $type = 'test' ) {
30 return ( new Stripe_Handler() )->get_connect_url( $type );
31 }
32
33 /**
34 * Get the Stripe account ID from settings.
35 *
36 * @return string|null The account ID
37 */
38 public static function account_id() {
39 return booktics_get_option( 'stripe_account_id' );
40 }
41
42 /**
43 * Get the Stripe account type from settings.
44 *
45 * @return string|null The account type
46 */
47 public static function account_type() {
48 return booktics_get_option( 'stripe_account_type', 'test');
49 }
50
51 /**
52 * Process a payment through Stripe.
53 *
54 * @param Order_Model $order The order to process payment for
55 *
56 * @return array The payment processing result
57 */
58 public function process_payment( $order ): array {
59 $this->init_stripe_sdk();
60
61 $amount = $order->total ?? 0;
62 $currency = $order->currency ?? 'usd';
63 try {
64 $intent = PaymentIntent::create( [
65 'amount' => $amount * 100,
66 'currency' => $currency,
67 'description' => $order->order_no ?? '',
68 'metadata' => [
69 'order_id' => $order->id,
70 ],
71 ] );
72
73 return [
74 'status' => 'success',
75 'payment_intent' => $intent->id,
76 'client_secret' => $intent->client_secret,
77 ];
78 } catch ( Exception $e ) {
79 return [
80 'status' => 'error',
81 'message' => $e->getMessage(),
82 ];
83 }
84 }
85
86 /**
87 * Validate the payment data.
88 *
89 * @return array Validation result
90 */
91 public function validate_payment_data(): array {
92 $client_secret = $this->request_data['client_secret'] ?? null;
93
94 if ( empty( $client_secret ) ) {
95 return [
96 'status' => 'error',
97 'message' => __( 'Missing client_secret for Stripe payment.', 'booktics' )
98 ];
99 }
100
101 return [ 'status' => 'success' ];
102 }
103
104 /**
105 * Get the payment status from Stripe.
106 *
107 * @param Order_Model $order The order to check status for
108 *
109 * @return string The payment status
110 */
111 public function get_payment_status( $order ): string {
112 $this->init_stripe_sdk();
113
114 $payment_intent_id = $order->payment_intent_id ?? null;
115
116 if ( ! $payment_intent_id ) {
117 return 'unknown';
118 }
119
120 try {
121 $intent = PaymentIntent::retrieve( $payment_intent_id );
122
123 return $intent->status;
124 } catch ( Exception $e ) {
125 return 'error';
126 }
127 }
128
129 /**
130 * Process a refund through Stripe.
131 *
132 * @param Order_Model $order The order to process refund for
133 *
134 * @return array The refund processing result
135 */
136 public function process_refund( $order ) {
137 $this->init_stripe_sdk();
138 $payment_intent_id = $order->payment_intent_id ?? null;
139
140 if ( ! $payment_intent_id ) {
141 return [
142 'status' => 'error',
143 'message' => __( 'Missing payment_intent_id for Stripe payment.', 'booktics' )
144 ];
145 }
146 $amount = $order->total ?? 0;
147 try {
148 $refund = Refund::create( [
149 'payment_intent' => $payment_intent_id,
150 'amount' => $amount ? intval( $amount * 100 ) : null,
151 'stripe_account' => self::account_id(),
152 ] );
153
154 return [
155 'status' => 'success',
156 'refund_id' => $refund->id,
157 'refund_status' => $refund->status,
158 'message' => __( 'Refund initiated successfully.', 'booktics' ),
159 ];
160 } catch ( \Exception $e ) {
161 return [
162 'status' => 'error',
163 'message' => $e->getMessage(),
164 ];
165 }
166 }
167 }