/wp-content/plugins/woocommerce-gateway-stripe/includes/connect/class-wc-stripe-connect-api.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 241 lines · 163 code · 34 blank · 44 comment · 15 complexity · efe14055aa1719ea0476c00e90c7a00a MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. if ( ! defined( 'WOOCOMMERCE_CONNECT_SERVER_URL' ) ) {
  6. define( 'WOOCOMMERCE_CONNECT_SERVER_URL', 'https://api.woocommerce.com/' );
  7. }
  8. if ( ! class_exists( 'WC_Stripe_Connect_API' ) ) {
  9. /**
  10. * Stripe Connect API class.
  11. */
  12. class WC_Stripe_Connect_API {
  13. const WOOCOMMERCE_CONNECT_SERVER_API_VERSION = '3';
  14. /**
  15. * Send request to Connect Server to initiate Stripe OAuth
  16. *
  17. * @param string $return_url return address.
  18. *
  19. * @return array
  20. */
  21. public function get_stripe_oauth_init( $return_url ) {
  22. $current_user = wp_get_current_user();
  23. $business_data = [];
  24. $business_data['url'] = get_site_url();
  25. $business_data['business_name'] = html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES );
  26. $business_data['first_name'] = $current_user->user_firstname;
  27. $business_data['last_name'] = $current_user->user_lastname;
  28. $business_data['phone'] = '';
  29. $business_data['currency'] = get_woocommerce_currency();
  30. $wc_countries = WC()->countries;
  31. if ( method_exists( $wc_countries, 'get_base_address' ) ) {
  32. $business_data['country'] = $wc_countries->get_base_country();
  33. $business_data['street_address'] = $wc_countries->get_base_address();
  34. $business_data['city'] = $wc_countries->get_base_city();
  35. $business_data['state'] = $wc_countries->get_base_state();
  36. $business_data['zip'] = $wc_countries->get_base_postcode();
  37. } else {
  38. $base_location = wc_get_base_location();
  39. $business_data['country'] = $base_location['country'];
  40. $business_data['street_address'] = '';
  41. $business_data['city'] = '';
  42. $business_data['state'] = $base_location['state'];
  43. $business_data['zip'] = '';
  44. }
  45. $request = [
  46. 'returnUrl' => $return_url,
  47. 'businessData' => $business_data,
  48. ];
  49. return $this->request( 'POST', '/stripe/oauth-init', $request );
  50. }
  51. /**
  52. * Send request to Connect Server for Stripe keys
  53. *
  54. * @param string $code OAuth server code.
  55. *
  56. * @return array
  57. */
  58. public function get_stripe_oauth_keys( $code ) {
  59. $request = [ 'code' => $code ];
  60. return $this->request( 'POST', '/stripe/oauth-keys', $request );
  61. }
  62. /**
  63. * General OAuth request method.
  64. *
  65. * @param string $method request method.
  66. * @param string $path path for request.
  67. * @param array $body request body.
  68. *
  69. * @return array|WP_Error
  70. */
  71. protected function request( $method, $path, $body = [] ) {
  72. if ( ! is_array( $body ) ) {
  73. return new WP_Error(
  74. 'request_body_should_be_array',
  75. __( 'Unable to send request to WooCommerce Connect server. Body must be an array.', 'woocommerce-gateway-stripe' )
  76. );
  77. }
  78. $url = trailingslashit( WOOCOMMERCE_CONNECT_SERVER_URL );
  79. $url = apply_filters( 'wc_connect_server_url', $url );
  80. $url = trailingslashit( $url ) . ltrim( $path, '/' );
  81. // Add useful system information to requests that contain bodies.
  82. if ( in_array( $method, [ 'POST', 'PUT' ], true ) ) {
  83. $body = $this->request_body( $body );
  84. $body = wp_json_encode( apply_filters( 'wc_connect_api_client_body', $body ) );
  85. if ( ! $body ) {
  86. return new WP_Error(
  87. 'unable_to_json_encode_body',
  88. __( 'Unable to encode body for request to WooCommerce Connect server.', 'woocommerce-gateway-stripe' )
  89. );
  90. }
  91. }
  92. $headers = $this->request_headers();
  93. if ( is_wp_error( $headers ) ) {
  94. return $headers;
  95. }
  96. $http_timeout = 60; // 1 minute
  97. wc_set_time_limit( $http_timeout + 10 );
  98. $args = [
  99. 'headers' => $headers,
  100. 'method' => $method,
  101. 'body' => $body,
  102. 'redirection' => 0,
  103. 'compress' => true,
  104. 'timeout' => $http_timeout,
  105. ];
  106. $args = apply_filters( 'wc_connect_request_args', $args );
  107. $response = wp_remote_request( $url, $args );
  108. $response_code = wp_remote_retrieve_response_code( $response );
  109. $content_type = wp_remote_retrieve_header( $response, 'content-type' );
  110. if ( false === strpos( $content_type, 'application/json' ) ) {
  111. if ( 200 !== $response_code ) {
  112. return new WP_Error(
  113. 'wcc_server_error',
  114. sprintf(
  115. // Translators: HTTP error code.
  116. __( 'Error: The WooCommerce Connect server returned HTTP code: %d', 'woocommerce-gateway-stripe' ),
  117. $response_code
  118. )
  119. );
  120. } else {
  121. return new WP_Error(
  122. 'wcc_server_error_content_type',
  123. sprintf(
  124. // Translators: content-type error code.
  125. __( 'Error: The WooCommerce Connect server returned an invalid content-type: %s.', 'woocommerce-gateway-stripe' ),
  126. $content_type
  127. )
  128. );
  129. }
  130. }
  131. $response_body = wp_remote_retrieve_body( $response );
  132. if ( ! empty( $response_body ) ) {
  133. $response_body = json_decode( $response_body );
  134. }
  135. if ( 200 !== $response_code ) {
  136. if ( empty( $response_body ) ) {
  137. return new WP_Error(
  138. 'wcc_server_empty_response',
  139. sprintf(
  140. // Translators: HTTP error code.
  141. __( 'Error: The WooCommerce Connect server returned ( %d ) and an empty response body.', 'woocommerce-gateway-stripe' ),
  142. $response_code
  143. )
  144. );
  145. }
  146. $error = property_exists( $response_body, 'error' ) ? $response_body->error : '';
  147. $message = property_exists( $response_body, 'message' ) ? $response_body->message : '';
  148. $data = property_exists( $response_body, 'data' ) ? $response_body->data : '';
  149. return new WP_Error(
  150. 'wcc_server_error_response',
  151. sprintf(
  152. /* translators: %1$s: error code, %2$s: error message, %3$d: HTTP response code */
  153. __( 'Error: The WooCommerce Connect server returned: %1$s %2$s ( %3$d )', 'woocommerce-gateway-stripe' ),
  154. $error,
  155. $message,
  156. $response_code
  157. ),
  158. $data
  159. );
  160. }
  161. return $response_body;
  162. }
  163. /**
  164. * Adds useful WP/WC/WCC information to request bodies.
  165. *
  166. * @param array $initial_body body of initial request.
  167. *
  168. * @return array
  169. */
  170. protected function request_body( $initial_body = [] ) {
  171. $default_body = [
  172. 'settings' => [],
  173. ];
  174. $body = array_merge( $default_body, $initial_body );
  175. // Add interesting fields to the body of each request.
  176. $body['settings'] = wp_parse_args(
  177. $body['settings'],
  178. [
  179. 'base_city' => WC()->countries->get_base_city(),
  180. 'base_country' => WC()->countries->get_base_country(),
  181. 'base_state' => WC()->countries->get_base_state(),
  182. 'base_postcode' => WC()->countries->get_base_postcode(),
  183. 'currency' => get_woocommerce_currency(),
  184. 'stripe_version' => WC_STRIPE_VERSION,
  185. 'wc_version' => WC()->version,
  186. 'wp_version' => get_bloginfo( 'version' ),
  187. ]
  188. );
  189. return $body;
  190. }
  191. /**
  192. * Generates headers for request to the WooCommerce Connect Server.
  193. *
  194. * @return array|WP_Error
  195. */
  196. protected function request_headers() {
  197. $headers = [];
  198. $locale = strtolower( str_replace( '_', '-', get_locale() ) );
  199. $locale_elements = explode( '-', $locale );
  200. $lang = $locale_elements[0];
  201. $headers['Accept-Language'] = $locale . ',' . $lang;
  202. $headers['Content-Type'] = 'application/json; charset=utf-8';
  203. $headers['Accept'] = 'application/vnd.woocommerce-connect.v' . self::WOOCOMMERCE_CONNECT_SERVER_API_VERSION;
  204. return $headers;
  205. }
  206. }
  207. }