PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.3.1
Subscriptions for WooCommerce with Stripe Recurring Payments v1.3.1
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Illuminate / Stripe.php

Stripe.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.3.1, at includes/Illuminate/Stripe.php

190 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Illuminate;
4
5 /**
6 * Class Stripe
7 *
8 * @package SpringDevs\SubscriptionPro\Illuminate
9 */
10 class Stripe extends \WC_Stripe_Payment_Gateway {
11
12 /**
13 * Initialize the class
14 */
15 public function __construct() {
16 add_action( 'subscrpt_after_create_renew_order', array( $this, 'after_create_renew_order' ), 10, 3 );
17 add_filter( 'wc_stripe_payment_metadata', array( $this, 'add_payment_metadata' ), 10, 2 );
18 }
19
20 /**
21 * Process stripe auto renewal process.
22 *
23 * @param \WC_Order $new_order New Order.
24 * @param \WC_Order $old_order Old Order.
25 * @param int $subscription_id Subscription ID.
26 */
27 public function after_create_renew_order( $new_order, $old_order, $subscription_id ) {
28 $is_auto_renew = get_post_meta( $subscription_id, '_subscrpt_auto_renew', true );
29 $stripe_enabled = ( 'stripe' === $old_order->get_payment_method() && in_array( $is_auto_renew, array( 1, '1' ), true ) && subscrpt_is_auto_renew_enabled() && '1' === get_option( 'subscrpt_stripe_auto_renew', '1' ) );
30
31 if ( ! $stripe_enabled ) {
32 return;
33 }
34
35 $this->pay_renew_order( $new_order );
36 }
37
38 /**
39 * Pay renewal Order
40 *
41 * @param \WC_Order $renewal_order Renewal order.
42 * @throws \WC_Stripe_Exception $e excepttion.
43 */
44 public function pay_renew_order( $renewal_order ) {
45
46 try {
47 $this->validate_minimum_order_amount( $renewal_order );
48
49 $amount = $renewal_order->get_total();
50 $order_id = $renewal_order->get_id();
51
52 // Get source from order.
53 $prepared_source = $this->prepare_order_source( $renewal_order );
54 if ( ! $prepared_source->customer ) {
55 return new \WP_Error( 'stripe_error', __( 'Customer not found', 'sdevs_subscrpt' ) );
56 }
57
58 \WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" );
59
60 $intent = $this->create_intent( $renewal_order, $prepared_source );
61
62 if ( empty( $intent->error ) ) {
63 $this->lock_order_payment( $renewal_order, $intent );
64 $intent = $this->confirm_intent( $intent, $renewal_order, $prepared_source );
65 }
66
67 if ( ! empty( $intent->error ) ) {
68 $this->maybe_remove_non_existent_customer( $intent->error, $renewal_order );
69
70 $this->unlock_order_payment( $renewal_order );
71 $this->throw_localized_message( $intent, $renewal_order );
72 }
73
74 if ( ! empty( $intent ) ) {
75 // Use the last charge within the intent to proceed.
76 $response = $this->get_latest_charge_from_intent( $intent );
77 $this->process_response( $response, $renewal_order );
78 }
79 $this->unlock_order_payment( $renewal_order );
80 } catch ( \WC_Stripe_Exception $e ) {
81 \WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
82 do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order );
83 }
84 }
85
86 /**
87 * Generates the request when creating a new payment intent.
88 *
89 * @param \WC_Order $order The order that is being paid for.
90 * @param object $prepared_source The source that is used for the payment.
91 * @return array The arguments for the request.
92 */
93 public function generate_create_intent_request( $order, $prepared_source ) {
94 // The request for a charge contains metadata for the intent.
95 $full_request = $this->generate_payment_request( $order, $prepared_source );
96
97 $payment_method_types = array( 'card' );
98 if ( isset( $prepared_source->source_object->type ) ) {
99 $payment_method_types = array( $prepared_source->source_object->type );
100 }
101
102 $currency = strtolower( $order->get_currency() );
103
104 $request = array(
105 'amount' => \WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ),
106 'currency' => $currency,
107 'description' => $full_request['description'],
108 'metadata' => $full_request['metadata'],
109 'capture_method' => ( 'true' === $full_request['capture'] ) ? 'automatic' : 'manual',
110 'payment_method_types' => $payment_method_types,
111 );
112
113 $request = \WC_Stripe_Helper::add_payment_method_to_request_array( $prepared_source->source, $request );
114
115 $force_save_source = apply_filters( 'wc_stripe_force_save_source', false, $prepared_source->source );
116
117 if ( $this->save_payment_method_requested() || $this->has_subscription( $order->get_id() ) || $force_save_source ) {
118 $request['setup_future_usage'] = 'off_session';
119 $request['metadata']['save_payment_method'] = 'true';
120 }
121
122 if ( $prepared_source->customer ) {
123 $request['customer'] = $prepared_source->customer;
124 }
125
126 if ( isset( $full_request['statement_descriptor_suffix'] ) ) {
127 $request['statement_descriptor_suffix'] = $full_request['statement_descriptor_suffix'];
128 }
129
130 if ( isset( $full_request['shipping'] ) ) {
131 $request['shipping'] = $full_request['shipping'];
132 }
133
134 if ( isset( $full_request['receipt_email'] ) ) {
135 $request['receipt_email'] = $full_request['receipt_email'];
136 }
137
138 /**
139 * Filter the return value of the WC_Payment_Gateway_CC::generate_create_intent_request.
140 *
141 * @since 3.1.0
142 * @param array $request
143 * @param WC_Order $order
144 * @param object $source
145 */
146 return apply_filters( 'wc_stripe_generate_create_intent_request', $request, $order, $prepared_source );
147 }
148
149 /**
150 * Add metadata to stripe payment.
151 *
152 * @param array $metadata Metadata.
153 * @param \WC_Order $order Order.
154 *
155 * @return array
156 */
157 public function add_payment_metadata( array $metadata, \WC_Order $order ): array {
158
159 if ( ! subscrpt_is_auto_renew_enabled() ) {
160 return $metadata;
161 }
162
163 global $wpdb;
164 $recurring = false;
165 foreach ( $order->get_items() as $order_item ) {
166 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
167 // @phpcs:ignore
168 $relation = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE order_id=%d AND order_item_id=%d', array( $table_name, $order->get_id(), $order_item->get_id() ) ) );
169
170 if ( 0 < count( $relation ) ) {
171 $relation = $relation[0];
172 $is_auto_renew = get_post_meta( (int) $relation->subscription_id, '_subscrpt_auto_renew', true );
173
174 if ( in_array( $is_auto_renew, array( 1, '1' ), true ) && in_array( $relation->type, array( 'early-renew', 'renew' ), true ) ) {
175 $recurring = true;
176 break;
177 }
178 }
179 }
180
181 if ( $recurring ) {
182 $metadata += array(
183 'payment_type' => 'recurring',
184 );
185 }
186
187 return $metadata;
188 }
189 }
190