class-wc-rest-payments-tos-controller.php
4.78 KB
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
<?php
/**
* Class WC_REST_Payments_Tos_Controller
*
* @package WooCommerce\Payments\Admin
*/
use WCPay\Exceptions\Rest_Request_Exception;
use WCPay\Logger;
defined( 'ABSPATH' ) || exit;
/**
* REST controller for Terms of Services routes.
*/
class WC_REST_Payments_Tos_Controller extends WC_Payments_REST_Controller {
/**
* Result codes for returning to the WCPay server API. They don't have any special meaning, but can will be logged
* and are therefore useful when debugging how we reacted to a webhook.
*/
const RESULT_SUCCESS = 'success';
const RESULT_BAD_REQUEST = 'bad_request';
const RESULT_ERROR = 'error';
/**
* Endpoint path.
*
* @var string
*/
protected $rest_base = 'payments/tos';
/**
* Instance of WC_Payment_Gateway_WCPay
*
* @var WC_Payment_Gateway_WCPay
*/
private $gateway;
/**
* WC Payments Account.
*
* @var WC_Payments_Account
*/
private $account;
/**
* WC_REST_Payments_Webhook_Controller constructor.
*
* @param WC_Payments_API_Client $api_client WC_Payments_API_Client instance.
* @param WC_Payment_Gateway_WCPay $gateway WC_Payment_Gateway_WCPay instance.
* @param WC_Payments_Account $account WC_Payments_Account instance.
*/
public function __construct( WC_Payments_API_Client $api_client, WC_Payment_Gateway_WCPay $gateway, WC_Payments_Account $account ) {
parent::__construct( $api_client );
$this->gateway = $gateway;
$this->account = $account;
}
/**
* Configure REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'handle_tos' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/reactivate',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'reactivate' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/stripe_track_connected',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'remove_stripe_connect_track' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
}
/**
* Record ToS acceptance.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_REST_Response
* @throws Rest_Request_Exception Throw if accept param is missing.
*/
public function handle_tos( $request ) {
$body = $request->get_json_params();
try {
if ( ! isset( $body['accept'] ) ) {
throw new Rest_Request_Exception( __( 'ToS accept parameter is missing', 'woocommerce-payments' ) );
}
$is_accepted = (bool) $body['accept'];
Logger::debug( sprintf( 'ToS acceptance request received. Accept: %s', $is_accepted ? 'yes' : 'no' ) );
if ( $is_accepted ) {
$this->handle_tos_accepted();
} else {
$this->handle_tos_declined();
}
} catch ( Rest_Request_Exception $e ) {
Logger::error( $e );
return new WP_REST_Response( [ 'result' => self::RESULT_BAD_REQUEST ], 400 );
} catch ( Exception $e ) {
Logger::error( $e );
return new WP_REST_Response( [ 'result' => self::RESULT_ERROR ], 500 );
}
return new WP_REST_Response( [ 'result' => self::RESULT_SUCCESS ] );
}
/**
* Process ToS accepted.
*/
private function handle_tos_accepted() {
$this->gateway->enable();
// Accessing directly, because a user must be already logged in.
$current_user = wp_get_current_user();
$user_name = $current_user->user_login;
$this->api_client->add_tos_agreement( 'settings-popup', $user_name );
$this->account->refresh_account_data();
}
/**
* Process ToS declined.
*/
private function handle_tos_declined() {
// TODO: maybe record ToS declined data.
$this->gateway->disable();
}
/**
* Activates the gateway again, after it's been disabled.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_REST_Response
*/
public function reactivate( $request ) {
try {
$this->gateway->enable();
Logger::debug( 'Gateway re-enabled after ToS decline.' );
} catch ( Exception $e ) {
Logger::error( $e );
return new WP_REST_Response( [ 'result' => self::RESULT_ERROR ], 500 );
}
return new WP_REST_Response( [ 'result' => self::RESULT_SUCCESS ] );
}
/**
* Deletes _wcpay_onboarding_stripe_connected option after KYC completion has been tracked.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_REST_Response
*/
public function remove_stripe_connect_track( $request ) {
delete_option( '_wcpay_onboarding_stripe_connected' );
return new WP_REST_Response( [ 'result' => self::RESULT_SUCCESS ] );
}
}