blob: 471ec3b80fe628a1845bf58fc640a68f26757806 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5}
6
7if ( ! class_exists( 'WC_Stripe_Connect' ) ) {
8 /**
9 * Stripe Connect class.
10 */
11 class WC_Stripe_Connect {
12
13 const SETTINGS_OPTION = 'woocommerce_stripe_settings';
14
15 /**
16 * Stripe connect api.
17 *
18 * @var object $api
19 */
20 private $api;
21
22 /**
23 * Constructor.
24 *
25 * @param WC_Stripe_Connect_API $api stripe connect api.
26 */
27 public function __construct( WC_Stripe_Connect_API $api ) {
28 $this->api = $api;
29
30 add_action( 'admin_init', [ $this, 'maybe_handle_redirect' ] );
31 }
32
33 /**
34 * Gets the OAuth URL for Stripe onboarding flow
35 *
36 * @param string $return_url url to return to after oauth flow.
37 *
38 * @return string|WP_Error
39 */
40 public function get_oauth_url( $return_url = '' ) {
41
42 if ( empty( $return_url ) ) {
43 $return_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe&panel=settings' );
44 }
45
46 if ( substr( $return_url, 0, 8 ) !== 'https://' ) {
47 return new WP_Error( 'invalid_url_protocol', __( 'Your site must be served over HTTPS in order to connect your Stripe account automatically.', 'woocommerce-gateway-stripe' ) );
48 }
49
50 $result = $this->api->get_stripe_oauth_init( $return_url );
51
52 if ( is_wp_error( $result ) ) {
53 return $result;
54 }
55
56 return $result->oauthUrl; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
57 }
58
59 /**
60 * Initiate OAuth connection request to Connect Server
61 *
62 * @param bool $state Stripe onboarding state.
63 * @param int $code OAuth code.
64 *
65 * @return string|WP_Error
66 */
67 public function connect_oauth( $state, $code ) {
68
69 $response = $this->api->get_stripe_oauth_keys( $code );
70
71 if ( is_wp_error( $response ) ) {
72 return $response;
73 }
74
75 return $this->save_stripe_keys( $response );
76 }
77
78 /**
79 * Handle redirect back from oauth-init or credentials reset
80 */
81 public function maybe_handle_redirect() {
82 if ( ! is_admin() ) {
83 return;
84 }
85
86 // redirect from oauth-init
87 if ( isset( $_GET['wcs_stripe_code'], $_GET['wcs_stripe_state'] ) ) {
88
89 $response = $this->connect_oauth( wc_clean( wp_unslash( $_GET['wcs_stripe_state'] ) ), wc_clean( wp_unslash( $_GET['wcs_stripe_code'] ) ) );
90 wp_safe_redirect( esc_url_raw( remove_query_arg( [ 'wcs_stripe_state', 'wcs_stripe_code' ] ) ) );
91 exit;
92 }
93 }
94
95 /**
96 * Saves stripe keys after OAuth response
97 *
98 * @param array $result OAuth response result.
99 *
100 * @return array|WP_Error
101 */
102 private function save_stripe_keys( $result ) {
103
104 if ( ! isset( $result->publishableKey, $result->secretKey ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
105 return new WP_Error( 'Invalid credentials received from WooCommerce Connect server' );
106 }
107
108 $is_test = false !== strpos( $result->publishableKey, '_test_' ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
109 $prefix = $is_test ? 'test_' : '';
110 $default_options = $this->get_default_stripe_config();
111 $options = array_merge( $default_options, get_option( self::SETTINGS_OPTION, [] ) );
112 $options['enabled'] = 'yes';
113 $options['testmode'] = $is_test ? 'yes' : 'no';
114 $options[ $prefix . 'publishable_key' ] = $result->publishableKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
115 $options[ $prefix . 'secret_key' ] = $result->secretKey; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
116
117 // While we are at it, let's also clear the account_id and
118 // test_account_id if present.
119 unset( $options['account_id'] );
120 unset( $options['test_account_id'] );
121
122 update_option( self::SETTINGS_OPTION, $options );
123
124 return $result;
125 }
126
127 /**
128 * Clears keys for test or production (whichever is presently enabled).
129 */
130 private function clear_stripe_keys() {
131
132 $options = get_option( self::SETTINGS_OPTION, [] );
133
134 if ( 'yes' === $options['testmode'] ) {
135 $options['test_publishable_key'] = '';
136 $options['test_secret_key'] = '';
137 // clear test_account_id if present
138 unset( $options['test_account_id'] );
139 } else {
140 $options['publishable_key'] = '';
141 $options['secret_key'] = '';
142 // clear account_id if present
143 unset( $options['account_id'] );
144 }
145
146 update_option( self::SETTINGS_OPTION, $options );
147
148 }
149
150 /**
151 * Gets default Stripe settings
152 */
153 private function get_default_stripe_config() {
154
155 $result = [];
156 $gateway = new WC_Gateway_Stripe();
157 foreach ( $gateway->form_fields as $key => $value ) {
158 if ( isset( $value['default'] ) ) {
159 $result[ $key ] = $value['default'];
160 }
161 }
162
163 return $result;
164 }
165
166 public function is_connected() {
167
168 $options = get_option( self::SETTINGS_OPTION, [] );
169
170 if ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) {
171 return isset( $options['test_publishable_key'], $options['test_secret_key'] ) && trim( $options['test_publishable_key'] ) && trim( $options['test_secret_key'] );
172 } else {
173 return isset( $options['publishable_key'], $options['secret_key'] ) && trim( $options['publishable_key'] ) && trim( $options['secret_key'] );
174 }
175 }
176 }
177}