PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.5.1
Subscriptions for WooCommerce with Stripe Recurring Payments v1.5.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.5.1, at includes/Illuminate/Stripe.php

192 lines 6.4 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 wp_subscrpt_write_debug_log( "Processing renewal order #{$renewal_order->get_id()} for payment." );
46
47 try {
48 $this->validate_minimum_order_amount( $renewal_order );
49
50 $amount = $renewal_order->get_total();
51 $order_id = $renewal_order->get_id();
52
53 // Get source from order.
54 $prepared_source = $this->prepare_order_source( $renewal_order );
55 if ( ! $prepared_source->customer ) {
56 return new \WP_Error( 'stripe_error', __( 'Customer not found', 'wp_subscription' ) );
57 }
58
59 \WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" );
60
61 $intent = $this->create_intent( $renewal_order, $prepared_source );
62
63 if ( empty( $intent->error ) ) {
64 $this->lock_order_payment( $renewal_order, $intent );
65 $intent = $this->confirm_intent( $intent, $renewal_order, $prepared_source );
66 }
67
68 if ( ! empty( $intent->error ) ) {
69 $this->maybe_remove_non_existent_customer( $intent->error, $renewal_order );
70
71 $this->unlock_order_payment( $renewal_order );
72 $this->throw_localized_message( $intent, $renewal_order );
73 }
74
75 if ( ! empty( $intent ) ) {
76 // Use the last charge within the intent to proceed.
77 $response = $this->get_latest_charge_from_intent( $intent );
78 $this->process_response( $response, $renewal_order );
79 }
80 $this->unlock_order_payment( $renewal_order );
81 } catch ( \WC_Stripe_Exception $e ) {
82 \WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
83 wp_subscrpt_write_debug_log( "Error processing renewal order #{$renewal_order->get_id()}: " . $e->getMessage() );
84 do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order );
85 }
86 }
87
88 /**
89 * Generates the request when creating a new payment intent.
90 *
91 * @param \WC_Order $order The order that is being paid for.
92 * @param object $prepared_source The source that is used for the payment.
93 * @return array The arguments for the request.
94 */
95 public function generate_create_intent_request( $order, $prepared_source ) {
96 // The request for a charge contains metadata for the intent.
97 $full_request = $this->generate_payment_request( $order, $prepared_source );
98
99 $payment_method_types = array( 'card' );
100 if ( isset( $prepared_source->source_object->type ) ) {
101 $payment_method_types = array( $prepared_source->source_object->type );
102 }
103
104 $currency = strtolower( $order->get_currency() );
105
106 $request = array(
107 'amount' => \WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ),
108 'currency' => $currency,
109 'description' => $full_request['description'],
110 'metadata' => $full_request['metadata'],
111 'capture_method' => ( 'true' === $full_request['capture'] ) ? 'automatic' : 'manual',
112 'payment_method_types' => $payment_method_types,
113 );
114
115 $request = \WC_Stripe_Helper::add_payment_method_to_request_array( $prepared_source->source, $request );
116
117 $force_save_source = apply_filters( 'wc_stripe_force_save_source', false, $prepared_source->source );
118
119 if ( $this->save_payment_method_requested() || $this->has_subscription( $order->get_id() ) || $force_save_source ) {
120 $request['setup_future_usage'] = 'off_session';
121 $request['metadata']['save_payment_method'] = 'true';
122 }
123
124 if ( $prepared_source->customer ) {
125 $request['customer'] = $prepared_source->customer;
126 }
127
128 if ( isset( $full_request['statement_descriptor_suffix'] ) ) {
129 $request['statement_descriptor_suffix'] = $full_request['statement_descriptor_suffix'];
130 }
131
132 if ( isset( $full_request['shipping'] ) ) {
133 $request['shipping'] = $full_request['shipping'];
134 }
135
136 if ( isset( $full_request['receipt_email'] ) ) {
137 $request['receipt_email'] = $full_request['receipt_email'];
138 }
139
140 /**
141 * Filter the return value of the WC_Payment_Gateway_CC::generate_create_intent_request.
142 *
143 * @since 3.1.0
144 * @param array $request
145 * @param WC_Order $order
146 * @param object $source
147 */
148 return apply_filters( 'wc_stripe_generate_create_intent_request', $request, $order, $prepared_source );
149 }
150
151 /**
152 * Add metadata to stripe payment.
153 *
154 * @param array $metadata Metadata.
155 * @param \WC_Order $order Order.
156 *
157 * @return array
158 */
159 public function add_payment_metadata( array $metadata, \WC_Order $order ): array {
160
161 if ( ! subscrpt_is_auto_renew_enabled() ) {
162 return $metadata;
163 }
164
165 global $wpdb;
166 $recurring = false;
167 foreach ( $order->get_items() as $order_item ) {
168 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
169 // @phpcs:ignore
170 $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() ) ) );
171
172 if ( 0 < count( $relation ) ) {
173 $relation = $relation[0];
174 $is_auto_renew = get_post_meta( (int) $relation->subscription_id, '_subscrpt_auto_renew', true );
175
176 if ( in_array( $is_auto_renew, array( 1, '1' ), true ) && in_array( $relation->type, array( 'early-renew', 'renew' ), true ) ) {
177 $recurring = true;
178 break;
179 }
180 }
181 }
182
183 if ( $recurring ) {
184 $metadata += array(
185 'payment_type' => 'recurring',
186 );
187 }
188
189 return $metadata;
190 }
191 }
192