-
Notifications
You must be signed in to change notification settings - Fork 216
[WIP] Stripe request #4748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Mayisha
wants to merge
6
commits into
dev/jetpack-onboarding-class
Choose a base branch
from
try/stripe-requests
base: dev/jetpack-onboarding-class
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[WIP] Stripe request #4748
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a9ebfe
retrieve request
Mayisha ccf42ee
Merge try/extract-transact-proxy-api-logic and refactor to use the ne…
diegocurbelo 401f965
Improve cache handling
diegocurbelo bac85b8
Fix tests
diegocurbelo af8d883
Consolidate WPCOM constants in WC_Stripe_Transact_API
diegocurbelo 9a5b847
Remove body json_enconde when making a transact_api call
diegocurbelo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <?php | ||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * WC_Stripe_Transact_API class. | ||
| * | ||
| * Handles Transact API requests. | ||
| */ | ||
| class WC_Stripe_Transact_API { | ||
|
|
||
| /** | ||
| * The API version for the proxy endpoint. | ||
| * | ||
| * @var int | ||
| */ | ||
| private const WPCOM_PROXY_ENDPOINT_API_VERSION = 2; | ||
|
|
||
| /** | ||
| * The timeout for requests to the WPCOM proxy endpoint. | ||
| * | ||
| * @var int | ||
| */ | ||
| private const WPCOM_PROXY_REQUEST_TIMEOUT = 60; | ||
|
|
||
| /** | ||
| * The base for the proxy REST endpoint. | ||
| * | ||
| * @var string | ||
| */ | ||
| private const WPCOM_PROXY_REST_BASE = 'transact/stripe/proxy/v1'; | ||
|
|
||
| /** | ||
| * Instance of WC_Stripe_Transact_API. | ||
| * | ||
| * @var WC_Stripe_Transact_API | ||
| */ | ||
| private static $instance = null; | ||
|
|
||
| /** | ||
| * Get instance of WC_Stripe_Transact_API. | ||
| * | ||
| * @return WC_Stripe_Transact_API | ||
| */ | ||
| public static function get_instance(): self { | ||
| if ( ! self::$instance ) { | ||
| self::$instance = new self(); | ||
| } | ||
| return self::$instance; | ||
| } | ||
|
|
||
| /** | ||
| * Check if the Transact API is enabled. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function is_transact_api_enabled(): bool { | ||
|
|
||
| // TODO: Add a feature flag and check. | ||
|
|
||
| $jetpack_connection_manager = new \Automattic\Jetpack\Connection\Manager( 'woocommerce' ); | ||
|
|
||
| if ( ! $jetpack_connection_manager->is_connected() ) { | ||
| return false; | ||
| } | ||
|
|
||
| $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); | ||
| if ( ! $stripe_settings || ! isset( $stripe_settings['transact_onboarding_complete'] ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| return 'yes' === $stripe_settings['transact_onboarding_complete']; | ||
| } | ||
|
|
||
| /** | ||
| * Send a request to the Transact platform. | ||
| * | ||
| * @param string $method The HTTP method to use. | ||
| * @param string $endpoint The endpoint to request. | ||
| * @param array $headers The headers to send. | ||
| * @param array $request_body The request body. | ||
| * | ||
| * @return array|null The API response body, or null if the request fails. | ||
| */ | ||
| public function send_wpcom_proxy_request( $method, $endpoint, $headers = [], $request_body = [] ) { | ||
| $site_id = \Jetpack_Options::get_option( 'id' ); | ||
| if ( ! $site_id ) { | ||
| WC_Stripe_Logger::error( sprintf( 'Site ID not found. Cannot send request to %s.', $endpoint ) ); | ||
| throw new Exception( 'Site ID not found. Cannot send proxy request.' ); | ||
| } | ||
|
|
||
| if ( isset( $headers['Authorization'] ) ) { | ||
| $headers['Stripe-Authorization'] = $headers['Authorization']; | ||
| unset( $headers['Authorization'] ); | ||
| } | ||
|
|
||
| $response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog( | ||
| sprintf( '/sites/%d/%s/%s', $site_id, self::WPCOM_PROXY_REST_BASE, $endpoint ), | ||
| self::WPCOM_PROXY_ENDPOINT_API_VERSION, | ||
| [ | ||
| 'headers' => $headers, | ||
| 'method' => $method, | ||
| 'timeout' => 70, | ||
| ], | ||
| 'GET' === $method ? null : wp_json_encode( $request_body ), | ||
| 'wpcom' | ||
| ); | ||
|
|
||
| return $response; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to add a check for
$this->is_transact_api_enabled(), just to make sure we fail quickly if those conditions are not met.