swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'WC_Stripe_Connect_REST_Oauth_Connect_Controller' ) ) { |
| 8 | /** |
| 9 | * Stripe Connect Oauth Connect controller class. |
| 10 | */ |
| 11 | class WC_Stripe_Connect_REST_Oauth_Connect_Controller extends WC_Stripe_Connect_REST_Controller { |
| 12 | |
| 13 | /** |
| 14 | * REST base. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $rest_base = 'connect/stripe/oauth/connect'; |
| 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 | * OAuth Connection 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->connect_oauth( $data['state'], $data['code'] ); |
| 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 | 'account_id' => $response->accountId, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 66 | ]; |
| 67 | } |
| 68 | } |
| 69 | } |