swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | /** |
| 3 | * Class WC_REST_Stripe_Account_Controller |
| 4 | */ |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | /** |
| 9 | * REST controller for retrieving Stripe's account data. |
| 10 | * |
| 11 | * @since 5.6.0 |
| 12 | */ |
| 13 | class WC_REST_Stripe_Account_Controller extends WC_Stripe_REST_Base_Controller { |
| 14 | /** |
| 15 | * Endpoint path. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $rest_base = 'wc_stripe/account'; |
| 20 | |
| 21 | /** |
| 22 | * The account data utility. |
| 23 | * |
| 24 | * @var WC_Stripe_Account |
| 25 | */ |
| 26 | private $account; |
| 27 | |
| 28 | /** |
| 29 | * Stripe payment gateway. |
| 30 | * |
| 31 | * @var WC_Gateway_Stripe |
| 32 | */ |
| 33 | private $gateway; |
| 34 | |
| 35 | public function __construct( WC_Gateway_Stripe $gateway, WC_Stripe_Account $account ) { |
| 36 | $this->gateway = $gateway; |
| 37 | $this->account = $account; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Configure REST API routes. |
| 42 | */ |
| 43 | public function register_routes() { |
| 44 | register_rest_route( |
| 45 | $this->namespace, |
| 46 | '/' . $this->rest_base, |
| 47 | [ |
| 48 | 'methods' => WP_REST_Server::READABLE, |
| 49 | 'callback' => [ $this, 'get_account' ], |
| 50 | 'permission_callback' => [ $this, 'check_permission' ], |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | register_rest_route( |
| 55 | $this->namespace, |
| 56 | '/' . $this->rest_base . '/summary', |
| 57 | [ |
| 58 | 'methods' => WP_REST_Server::READABLE, |
| 59 | 'callback' => [ $this, 'get_account_summary' ], |
| 60 | 'permission_callback' => [ $this, 'check_permission' ], |
| 61 | ] |
| 62 | ); |
| 63 | |
| 64 | register_rest_route( |
| 65 | $this->namespace, |
| 66 | '/' . $this->rest_base . '/webhook-status-message', |
| 67 | [ |
| 68 | 'methods' => WP_REST_Server::READABLE, |
| 69 | 'callback' => [ $this, 'get_webhook_status_message' ], |
| 70 | 'permission_callback' => [ $this, 'check_permission' ], |
| 71 | ] |
| 72 | ); |
| 73 | |
| 74 | register_rest_route( |
| 75 | $this->namespace, |
| 76 | '/' . $this->rest_base . '/refresh', |
| 77 | [ |
| 78 | 'methods' => WP_REST_Server::EDITABLE, |
| 79 | 'callback' => [ $this, 'refresh_account' ], |
| 80 | 'permission_callback' => [ $this, 'check_permission' ], |
| 81 | ] |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Retrieve the Stripe account information. |
| 87 | * |
| 88 | * @return WP_REST_Response |
| 89 | */ |
| 90 | public function get_account() { |
| 91 | return new WP_REST_Response( |
| 92 | [ |
| 93 | 'account' => $this->account->get_cached_account_data(), |
| 94 | 'testmode' => WC_Stripe_Webhook_State::get_testmode(), |
| 95 | 'webhook_status_message' => WC_Stripe_Webhook_State::get_webhook_status_message(), |
| 96 | 'webhook_url' => WC_Stripe_Helper::get_webhook_url(), |
| 97 | ] |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return a summary of Stripe account details. |
| 103 | * |
| 104 | * @return WP_REST_Response |
| 105 | */ |
| 106 | public function get_account_summary() { |
| 107 | $account = $this->account->get_cached_account_data(); |
| 108 | |
| 109 | // Use statement descriptor from settings, falling back to Stripe account statement descriptor if needed. |
| 110 | $statement_descriptor = WC_Stripe_Helper::clean_statement_descriptor( $this->gateway->get_option( 'statement_descriptor' ) ); |
| 111 | if ( empty( $statement_descriptor ) ) { |
| 112 | $statement_descriptor = $account['settings']['payments']['statement_descriptor']; |
| 113 | } |
| 114 | if ( empty( $statement_descriptor ) ) { |
| 115 | $statement_descriptor = null; |
| 116 | } |
| 117 | |
| 118 | return new WP_REST_Response( |
| 119 | [ |
| 120 | 'has_pending_requirements' => $this->account->has_pending_requirements(), |
| 121 | 'has_overdue_requirements' => $this->account->has_overdue_requirements(), |
| 122 | 'current_deadline' => $account['requirements']['current_deadline'] ?? null, |
| 123 | 'status' => $this->account->get_account_status(), |
| 124 | 'statement_descriptor' => $statement_descriptor, |
| 125 | 'store_currencies' => [ |
| 126 | 'default' => $account['default_currency'] ?? get_woocommerce_currency(), |
| 127 | 'supported' => $this->account->get_supported_store_currencies(), |
| 128 | ], |
| 129 | 'country' => $account['country'] ?? WC()->countries->get_base_country(), |
| 130 | 'is_live' => $account['charges_enabled'] ?? false, |
| 131 | 'test_mode' => WC_Stripe_Webhook_State::get_testmode(), |
| 132 | ] |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Retrieve the webhook status information. |
| 138 | * |
| 139 | * @return WP_REST_Response |
| 140 | */ |
| 141 | public function get_webhook_status_message() { |
| 142 | return new WP_REST_Response( WC_Stripe_Webhook_State::get_webhook_status_message() ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Clears the cached account data and returns the updated one. |
| 147 | * |
| 148 | * @return WP_REST_Response |
| 149 | */ |
| 150 | public function refresh_account() { |
| 151 | $this->account->clear_cache(); |
| 152 | |
| 153 | // calling the same "get" method, so that the data format is the same. |
| 154 | return $this->get_account(); |
| 155 | } |
| 156 | } |