-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathclass-wc-payments-redirect-service.php
225 lines (190 loc) · 6.73 KB
/
class-wc-payments-redirect-service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Class WC_Payments_Redirect_Service
*
* @package WooCommerce\Payments
*/
use WCPay\Core\Server\Request\Get_Account_Capital_Link;
use WCPay\Core\Server\Request\Get_Account_Login_Data;
use WCPay\Exceptions\API_Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class handling redirects business logic.
*/
class WC_Payments_Redirect_Service {
/**
* Client for making requests to the WooCommerce Payments API
*
* @var WC_Payments_API_Client
*/
private $payments_api_client;
/**
* Constructor for WC_Payments_Session_Service.
*
* @param WC_Payments_API_Client $payments_api_client - WooCommerce Payments API client.
*/
public function __construct(
WC_Payments_API_Client $payments_api_client
) {
$this->payments_api_client = $payments_api_client;
}
/**
* Calls wp_safe_redirect and exit.
*
* This method will end the execution immediately after the redirection.
*
* @param string $location The URL to redirect to.
*/
public function redirect_to( string $location ): void {
wp_safe_redirect( $location );
exit;
}
/**
* Redirects to a wcpay-connect URL which then handles the next step for the onboarding flow.
*
* This is a sure way to ensure that the user is redirected to the correct URL to continue their onboarding.
*
* @param string $from Source of the redirect.
* @param array $additional_params Optional. Additional URL params to add to the redirect URL.
*/
public function redirect_to_wcpay_connect( string $from = '', array $additional_params = [] ): void {
// Take the user to the 'wcpay-connect' URL.
// We handle creating and redirecting to the account link there.
$params = [
'wcpay-connect' => '1',
'_wpnonce' => wp_create_nonce( 'wcpay-connect' ),
];
$params = array_merge( $params, $additional_params );
if ( '' !== $from ) {
$params['from'] = $from;
}
$connect_url = add_query_arg(
$params,
admin_url( 'admin.php' )
);
$this->redirect_to( $connect_url );
}
/**
* Redirects to the capital view offer page or overview page with error message.
*/
public function redirect_to_capital_view_offer_page(): void {
$return_url = WC_Payments_Account::get_overview_page_url();
$refresh_url = add_query_arg( [ 'wcpay-loan-offer' => '' ], admin_url( 'admin.php' ) );
try {
$request = Get_Account_Capital_Link::create();
$type = 'capital_financing_offer';
$request->set_type( $type );
$request->set_return_url( $return_url );
$request->set_refresh_url( $refresh_url );
$capital_link = $request->send();
$this->redirect_to( $capital_link['url'] );
} catch ( Exception $e ) {
$this->redirect_to_overview_page_with_error( [ 'wcpay-loan-offer-error' => '1' ] );
}
}
/**
* Function to immediately redirect to the account link.
*
* @param array $args The arguments to be sent with the link request.
*/
public function redirect_to_account_link( array $args ): void {
try {
$link = $this->payments_api_client->get_link( $args );
if ( isset( $args['type'] ) && 'complete_kyc_link' === $args['type'] && isset( $link['state'] ) ) {
set_transient( 'wcpay_stripe_onboarding_state', $link['state'], DAY_IN_SECONDS );
}
$this->redirect_to( $link['url'] );
} catch ( API_Exception $e ) {
$this->redirect_to_overview_page_with_error( [ 'wcpay-server-link-error' => '1' ] );
}
}
/**
* Immediately redirect to the Connect page.
*
* Note that this function immediately ends the execution.
*
* @param string|null $error_message Optional. Error message to show in a notice.
* @param string|null $from Optional. Source of the redirect.
* @param array $additional_params Optional. Additional URL params to add to the redirect URL.
*/
public function redirect_to_connect_page( ?string $error_message = null, ?string $from = null, array $additional_params = [] ): void {
$params = [
'page' => 'wc-admin',
'path' => '/payments/connect',
];
if ( count( $params ) === count( array_intersect_assoc( $_GET, $params ) ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
// We are already on the Connect page. Do nothing.
return;
}
// If we were given an error message, store it in a very short-lived transient to show it on the page.
if ( ! empty( $error_message ) ) {
set_transient( WC_Payments_Account::ERROR_MESSAGE_TRANSIENT, $error_message, 30 );
}
$params = array_merge( $params, $additional_params );
if ( ! empty( $from ) ) {
$params['from'] = $from;
}
$this->redirect_to( admin_url( add_query_arg( $params, 'admin.php' ) ) );
}
/**
* Immediately redirect to the onboarding wizard.
*
* Note that this function immediately ends the execution.
*
* @param string|null $from Optional. Source of the redirect.
* @param array $additional_params Optional. Additional URL params to add to the redirect URL.
*/
public function redirect_to_onboarding_wizard( ?string $from = null, array $additional_params = [] ): void {
$params = [
'page' => 'wc-admin',
'path' => '/payments/onboarding',
];
if ( count( $params ) === count( array_intersect_assoc( $_GET, $params ) ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
// We are already in the onboarding wizard. Do nothing.
return;
}
$params = array_merge( $params, $additional_params );
if ( ! empty( $from ) ) {
$params['from'] = $from;
}
$this->redirect_to( admin_url( add_query_arg( $params, 'admin.php' ) ) );
}
/**
* Redirect to the overview page.
*
* @param string $from Optional. Source of the redirect.
* @param array $additional_params Optional. Additional URL params to add to the redirect URL.
* */
public function redirect_to_overview_page( string $from = '', array $additional_params = [] ): void {
$params = $additional_params;
if ( '' !== $from ) {
$params['from'] = $from;
}
$this->redirect_to( add_query_arg( $params, WC_Payments_Account::get_overview_page_url() ) );
}
/**
* Redirect to the overview page with an error message.
*
* @param array $error The error data to show.
*/
public function redirect_to_overview_page_with_error( array $error ): void {
$overview_url_with_error = add_query_arg(
$error,
WC_Payments_Account::get_overview_page_url()
);
$this->redirect_to( $overview_url_with_error );
}
/**
* For the connected account, fetches the login url from the API and redirects to it.
*/
public function redirect_to_login(): void {
$redirect_url = WC_Payments_Account::get_overview_page_url();
$request = Get_Account_Login_Data::create();
$request->set_redirect_url( $redirect_url );
$response = $request->send();
$login_data = $response->to_array();
$this->redirect_to( $login_data['url'] );
}
}