PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.59
Timetics – Appointment Booking Calendar & Scheduling v1.0.59
1.0.62 1.0.63 1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 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.19 1.0.2 1.0.20 1.0.21 1.0.22 All 64 releases
timetics / core / integrations / stripe / stripe-payment.php

stripe-payment.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.59, at core/integrations/stripe/stripe-payment.php

193 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class stripe payment
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Integrations\Stripe;
8
9 /**
10 * Class StripePayment
11 */
12 class StripePayment {
13 /**
14 * Store stripe paymentintent api url
15 *
16 * @var string
17 */
18 private $payment_intent_url = 'https://api.stripe.com/v1/payment_intents';
19
20 /**
21 * Create stripe paymentintent
22 *
23 * @param array $args Stripe payment details
24 *
25 * @return array
26 */
27 public function create_payment( $args = [] ) {
28 $defaults = [
29 'amount' => '',
30 'currency' => '',
31 'metadata' => [],
32 ];
33
34 $args = wp_parse_args( $args, $defaults );
35
36 $metadata = is_array( $args['metadata'] ) ? $args['metadata'] : [];
37 unset( $args['metadata'] );
38
39 $url = $this->payment_intent_url;
40 $secret = timetics_get_option( 'stripe_secret_key' );
41 $body = build_query( $args ) . '&automatic_payment_methods[enabled]=true';
42
43 foreach ( $metadata as $meta_key => $meta_value ) {
44 if ( $meta_value === '' || $meta_value === null ) {
45 continue;
46 }
47 $body .= '&metadata[' . rawurlencode( (string) $meta_key ) . ']=' . rawurlencode( (string) $meta_value );
48 }
49
50 $params = [
51 'headers' => [
52 'Authorization' => 'Bearer ' . $secret,
53 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
54 ],
55 'body' => $body,
56 ];
57
58 $response = wp_remote_post( $url, $params );
59
60 if ( ! is_wp_error( $response ) ) {
61 return json_decode( wp_remote_retrieve_body( $response ), true );
62 }
63
64 /**
65 * Added temporary for leagacy sass. It will remove in future.
66 */
67 do_action( 'timetics/integrations/stripe/create_payment', $response );
68
69 return $response;
70 }
71
72 /**
73 * Update an existing Stripe PaymentIntent's metadata server-side.
74 * Used to bind a PaymentIntent to a booking after the booking is created
75 * but before the customer confirms the payment.
76 *
77 * @param string $intent_id PaymentIntent id (pi_...).
78 * @param array $metadata Key/value pairs to set on the intent.
79 *
80 * @return array|\WP_Error Decoded Stripe response, or WP_Error on failure.
81 */
82 public function update_payment_intent( $intent_id, $metadata = [] ) {
83 $intent_id = is_string( $intent_id ) ? trim( $intent_id ) : '';
84
85 if ( '' === $intent_id || strpos( $intent_id, 'pi_' ) !== 0 ) {
86 return new \WP_Error( 'timetics_stripe_invalid_intent', __( 'Invalid Stripe payment intent id.', 'timetics' ) );
87 }
88
89 $secret = timetics_get_option( 'stripe_secret_key' );
90
91 if ( empty( $secret ) ) {
92 return new \WP_Error( 'timetics_stripe_missing_secret', __( 'Stripe secret key is not configured.', 'timetics' ) );
93 }
94
95 $body_parts = [];
96 foreach ( (array) $metadata as $meta_key => $meta_value ) {
97 if ( $meta_value === '' || $meta_value === null ) {
98 continue;
99 }
100 $body_parts[] = 'metadata[' . rawurlencode( (string) $meta_key ) . ']=' . rawurlencode( (string) $meta_value );
101 }
102
103 if ( empty( $body_parts ) ) {
104 return new \WP_Error( 'timetics_stripe_empty_metadata', __( 'No metadata provided.', 'timetics' ) );
105 }
106
107 $response = wp_remote_post(
108 $this->payment_intent_url . '/' . rawurlencode( $intent_id ),
109 [
110 'headers' => [
111 'Authorization' => 'Bearer ' . $secret,
112 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
113 ],
114 'body' => implode( '&', $body_parts ),
115 'timeout' => 15,
116 ]
117 );
118
119 if ( is_wp_error( $response ) ) {
120 return $response;
121 }
122
123 $code = (int) wp_remote_retrieve_response_code( $response );
124 $body = json_decode( wp_remote_retrieve_body( $response ), true );
125
126 if ( $code < 200 || $code >= 300 ) {
127 $message = is_array( $body ) && ! empty( $body['error']['message'] )
128 ? $body['error']['message']
129 : __( 'Stripe payment intent update failed.', 'timetics' );
130 return new \WP_Error( 'timetics_stripe_update_failed', $message, [ 'status' => $code ] );
131 }
132
133 if ( ! is_array( $body ) ) {
134 return new \WP_Error( 'timetics_stripe_invalid_response', __( 'Unexpected Stripe response.', 'timetics' ) );
135 }
136
137 return $body;
138 }
139
140 /**
141 * Retrieve a Stripe PaymentIntent server-side for verification.
142 *
143 * @param string $intent_id PaymentIntent id (pi_...).
144 *
145 * @return array|\WP_Error Decoded Stripe response, or WP_Error on transport / API failure.
146 */
147 public function retrieve_payment_intent( $intent_id ) {
148 $intent_id = is_string( $intent_id ) ? trim( $intent_id ) : '';
149
150 if ( '' === $intent_id || strpos( $intent_id, 'pi_' ) !== 0 ) {
151 return new \WP_Error( 'timetics_stripe_invalid_intent', __( 'Invalid Stripe payment intent id.', 'timetics' ) );
152 }
153
154 $secret = timetics_get_option( 'stripe_secret_key' );
155
156 if ( empty( $secret ) ) {
157 return new \WP_Error( 'timetics_stripe_missing_secret', __( 'Stripe secret key is not configured.', 'timetics' ) );
158 }
159
160 $url = $this->payment_intent_url . '/' . rawurlencode( $intent_id );
161
162 $response = wp_remote_get(
163 $url,
164 [
165 'headers' => [
166 'Authorization' => 'Bearer ' . $secret,
167 ],
168 'timeout' => 15,
169 ]
170 );
171
172 if ( is_wp_error( $response ) ) {
173 return $response;
174 }
175
176 $code = (int) wp_remote_retrieve_response_code( $response );
177 $body = json_decode( wp_remote_retrieve_body( $response ), true );
178
179 if ( $code < 200 || $code >= 300 ) {
180 $message = is_array( $body ) && ! empty( $body['error']['message'] )
181 ? $body['error']['message']
182 : __( 'Stripe payment intent retrieval failed.', 'timetics' );
183 return new \WP_Error( 'timetics_stripe_retrieve_failed', $message, [ 'status' => $code ] );
184 }
185
186 if ( ! is_array( $body ) ) {
187 return new \WP_Error( 'timetics_stripe_invalid_response', __( 'Unexpected Stripe response.', 'timetics' ) );
188 }
189
190 return $body;
191 }
192 }
193