blob: 606ffb807920acd542cee993b189b5977c2f5f6e [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_REST_Oauth_Init_Controller' ) ) {
8 /**
9 * Stripe Connect Oauth Init controller class.
10 */
11 class WC_Stripe_Connect_REST_Oauth_Init_Controller extends WC_Stripe_Connect_REST_Controller {
12
13 /**
14 * REST base.
15 *
16 * @var string
17 */
18 protected $rest_base = 'connect/stripe/oauth/init';
19
20 /**
21 * Stripe Connect.
22 *
23 * @var WC_Stripe_Connect
24 */
25 protected $connect;
26
27 /**
28 * Constructor.
29 *
30 * @param WC_Stripe_Connect $connect stripe connect.
31 * @param WC_Stripe_Connect_API $api stripe connect api.
32 */
33 public function __construct( WC_Stripe_Connect $connect, WC_Stripe_Connect_API $api ) {
34
35 parent::__construct( $api );
36
37 $this->connect = $connect;
38 }
39
40 /**
41 * Initiate OAuth flow.
42 *
43 * @param array $request POST request.
44 *
45 * @return array|WP_Error
46 */
47 public function post( $request ) {
48
49 $data = $request->get_json_params();
50 $response = $this->connect->get_oauth_url( isset( $data['returnUrl'] ) ? $data['returnUrl'] : '' );
51
52 if ( is_wp_error( $response ) ) {
53
54 WC_Stripe_Logger::log( $response, __CLASS__ );
55
56 return new WP_Error(
57 $response->get_error_code(),
58 $response->get_error_message(),
59 [ 'status' => 400 ]
60 );
61 }
62
63 return [
64 'success' => true,
65 'oauthUrl' => $response,
66 ];
67 }
68 }
69}