Request
in package
PayPal Request Class
Handles PayPal API requests for creating orders, authorizing/capturing payments, and fetching PayPal order details using the Orders v2 API.
Tags
Table of Contents
- WPCOM_PROXY_CLIENT_ID_ENDPOINT = 'client_id'
- WPCOM_PROXY_ENDPOINT_API_VERSION = '2'
- The API version for the proxy endpoint.
- WPCOM_PROXY_ORDER_ENDPOINT = 'order'
- Proxy REST endpoints.
- WPCOM_PROXY_PAYMENT_AUTHORIZE_ENDPOINT = 'payment/authorize'
- WPCOM_PROXY_PAYMENT_CAPTURE_AUTH_ENDPOINT = 'payment/capture_auth'
- WPCOM_PROXY_PAYMENT_CAPTURE_ENDPOINT = 'payment/capture'
- WPCOM_PROXY_REST_BASE = 'transact/paypal_standard/proxy'
- The base for the proxy REST endpoint.
- $gateway : WC_Gateway_Paypal
- The PayPal gateway instance.
- __construct() : mixed
- Constructor.
- authorize_or_capture_payment() : void
- Authorize or capture a PayPal payment using the Orders v2 API.
- capture_authorized_payment() : void
- Capture a PayPal payment that has been authorized.
- create_paypal_order() : array<string|int, mixed>|null
- Create a PayPal order using the Orders v2 API.
- fetch_paypal_client_id() : string|null
- Fetch the PayPal client-id from the Transact platform.
- get_paypal_order_details() : array<string|int, mixed>
- Get PayPal order details.
- get_paypal_order_purchase_unit_amount() : array<string|int, mixed>
- Get the amount data for the PayPal order purchase unit field.
- get_approve_link() : string|null
- Get the approve link from the response data.
- get_authorization_id_for_capture() : string|null
- Get the authorization ID for the PayPal payment.
- get_latest_transaction_data() : array<string|int, mixed>|null
- Get the latest item from the authorizations or captures array based on update_time.
- get_paypal_create_order_request_params() : array<string|int, mixed>
- Build the request parameters for the PayPal create-order request.
- get_paypal_order_custom_id() : string
- Build the custom ID for the PayPal order. The custom ID will be used by the proxy for webhook forwarding, and by later steps to identify the order.
- get_paypal_order_intent() : string
- Get the value for the intent field in the create-order request.
- get_paypal_order_item_amount() : float
- Get the amount for a specific order item.
- get_paypal_order_items() : array<string|int, mixed>
- Get the order items for the PayPal create-order request.
- get_paypal_order_items_subtotal() : float
- Get the subtotal for all items, before discounts.
- get_paypal_order_shipping() : array<string|int, mixed>|null
- Get the shipping information for the PayPal create-order request.
- get_paypal_shipping_preference() : string
- Get the shipping preference for the PayPal create-order request.
- limit_length() : string
- Limit length of an arg.
- normalize_paypal_order_shipping_country_code() : string|null
- Normalize PayPal order shipping country code.
- normalize_url_for_paypal() : string
- Normalize a URL for PayPal. PayPal requires absolute URLs with protocol.
- send_wpcom_proxy_request() : array<string|int, mixed>|WP_Error
- Send a request to the API proxy.
Constants
WPCOM_PROXY_CLIENT_ID_ENDPOINT
private
mixed
WPCOM_PROXY_CLIENT_ID_ENDPOINT
= 'client_id'
WPCOM_PROXY_ENDPOINT_API_VERSION
The API version for the proxy endpoint.
private
string
WPCOM_PROXY_ENDPOINT_API_VERSION
= '2'
WPCOM_PROXY_ORDER_ENDPOINT
Proxy REST endpoints.
private
string
WPCOM_PROXY_ORDER_ENDPOINT
= 'order'
WPCOM_PROXY_PAYMENT_AUTHORIZE_ENDPOINT
private
mixed
WPCOM_PROXY_PAYMENT_AUTHORIZE_ENDPOINT
= 'payment/authorize'
WPCOM_PROXY_PAYMENT_CAPTURE_AUTH_ENDPOINT
private
mixed
WPCOM_PROXY_PAYMENT_CAPTURE_AUTH_ENDPOINT
= 'payment/capture_auth'
WPCOM_PROXY_PAYMENT_CAPTURE_ENDPOINT
private
mixed
WPCOM_PROXY_PAYMENT_CAPTURE_ENDPOINT
= 'payment/capture'
WPCOM_PROXY_REST_BASE
The base for the proxy REST endpoint.
private
string
WPCOM_PROXY_REST_BASE
= 'transact/paypal_standard/proxy'
Properties
$gateway
The PayPal gateway instance.
private
WC_Gateway_Paypal
$gateway
Methods
__construct()
Constructor.
public
__construct(WC_Gateway_Paypal $gateway) : mixed
Parameters
- $gateway : WC_Gateway_Paypal
-
Paypal gateway object.
Return values
mixed —authorize_or_capture_payment()
Authorize or capture a PayPal payment using the Orders v2 API.
public
authorize_or_capture_payment(WC_Order|null $order, string|null $action_url[, string $action = PayPalConstants::PAYMENT_ACTION_CAPTURE ]) : void
This method authorizes or captures a PayPal payment and updates the order status.
Parameters
- $order : WC_Order|null
-
Order object.
- $action_url : string|null
-
The URL to authorize or capture the payment.
- $action : string = PayPalConstants::PAYMENT_ACTION_CAPTURE
-
The action to perform. Either 'authorize' or 'capture'.
Tags
Return values
void —capture_authorized_payment()
Capture a PayPal payment that has been authorized.
public
capture_authorized_payment(WC_Order|null $order) : void
Parameters
- $order : WC_Order|null
-
Order object.
Tags
Return values
void —create_paypal_order()
Create a PayPal order using the Orders v2 API.
public
create_paypal_order(WC_Order $order[, string $payment_source = PayPalConstants::PAYMENT_SOURCE_PAYPAL ][, array<string|int, mixed> $js_sdk_params = array() ]) : array<string|int, mixed>|null
This method creates a PayPal order and returns the order details including the approval URL where customers will be redirected to complete payment.
Parameters
- $order : WC_Order
-
Order object.
- $payment_source : string = PayPalConstants::PAYMENT_SOURCE_PAYPAL
-
The payment source.
- $js_sdk_params : array<string|int, mixed> = array()
-
Extra parameters for a PayPal JS SDK (Buttons) request.
Tags
Return values
array<string|int, mixed>|null —fetch_paypal_client_id()
Fetch the PayPal client-id from the Transact platform.
public
fetch_paypal_client_id() : string|null
Tags
Return values
string|null — The PayPal client-id, or null if the request fails.get_paypal_order_details()
Get PayPal order details.
public
get_paypal_order_details(string $paypal_order_id) : array<string|int, mixed>
Parameters
- $paypal_order_id : string
-
The ID of the PayPal order.
Tags
Return values
array<string|int, mixed> —get_paypal_order_purchase_unit_amount()
Get the amount data for the PayPal order purchase unit field.
public
get_paypal_order_purchase_unit_amount(WC_Order|null $order) : array<string|int, mixed>
Parameters
- $order : WC_Order|null
-
Order object.
Return values
array<string|int, mixed> —get_approve_link()
Get the approve link from the response data.
private
get_approve_link(int|string $http_code, array<string|int, mixed> $response_data) : string|null
Parameters
- $http_code : int|string
-
The HTTP code of the response.
- $response_data : array<string|int, mixed>
-
The response data.
Return values
string|null —get_authorization_id_for_capture()
Get the authorization ID for the PayPal payment.
private
get_authorization_id_for_capture(WC_Order $order) : string|null
Parameters
- $order : WC_Order
-
Order object.
Return values
string|null —get_latest_transaction_data()
Get the latest item from the authorizations or captures array based on update_time.
private
get_latest_transaction_data(array<string|int, mixed> $items) : array<string|int, mixed>|null
Parameters
- $items : array<string|int, mixed>
-
Array of authorizations or captures.
Return values
array<string|int, mixed>|null — The latest authorization or capture or null if array is empty or no valid update_time found.get_paypal_create_order_request_params()
Build the request parameters for the PayPal create-order request.
private
get_paypal_create_order_request_params(WC_Order $order, string $payment_source, array<string|int, mixed> $js_sdk_params) : array<string|int, mixed>
Parameters
- $order : WC_Order
-
Order object.
- $payment_source : string
-
The payment source.
- $js_sdk_params : array<string|int, mixed>
-
Extra parameters for a PayPal JS SDK (Buttons) request.
Tags
Return values
array<string|int, mixed> —get_paypal_order_custom_id()
Build the custom ID for the PayPal order. The custom ID will be used by the proxy for webhook forwarding, and by later steps to identify the order.
private
get_paypal_order_custom_id(WC_Order $order) : string
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
string —get_paypal_order_intent()
Get the value for the intent field in the create-order request.
private
get_paypal_order_intent() : string
Return values
string —get_paypal_order_item_amount()
Get the amount for a specific order item.
private
get_paypal_order_item_amount(WC_Order $order, WC_Order_Item $item) : float
Parameters
- $order : WC_Order
-
Order object.
- $item : WC_Order_Item
-
Order item.
Return values
float —get_paypal_order_items()
Get the order items for the PayPal create-order request.
private
get_paypal_order_items(WC_Order $order) : array<string|int, mixed>
Returns an empty array if any of the items (amount, quantity) are invalid.
Parameters
- $order : WC_Order
-
Order object.
Return values
array<string|int, mixed> —get_paypal_order_items_subtotal()
Get the subtotal for all items, before discounts.
private
get_paypal_order_items_subtotal(WC_Order $order) : float
Parameters
- $order : WC_Order
-
Order object.
Return values
float —get_paypal_order_shipping()
Get the shipping information for the PayPal create-order request.
private
get_paypal_order_shipping(WC_Order $order) : array<string|int, mixed>|null
Parameters
- $order : WC_Order
-
Order object.
Return values
array<string|int, mixed>|null — Returns null if the shipping is not required, or the address is not set, or is incomplete.get_paypal_shipping_preference()
Get the shipping preference for the PayPal create-order request.
private
get_paypal_shipping_preference(WC_Order $order) : string
Parameters
- $order : WC_Order
-
Order object.
Return values
string —limit_length()
Limit length of an arg.
private
limit_length(string $text[, int $limit = 127 ]) : string
Parameters
- $text : string
-
Text to limit.
- $limit : int = 127
-
Limit size in characters.
Return values
string —normalize_paypal_order_shipping_country_code()
Normalize PayPal order shipping country code.
private
normalize_paypal_order_shipping_country_code(string $country_code) : string|null
Parameters
- $country_code : string
-
Country code to normalize.
Return values
string|null —normalize_url_for_paypal()
Normalize a URL for PayPal. PayPal requires absolute URLs with protocol.
private
normalize_url_for_paypal(string $url) : string
Parameters
- $url : string
-
The URL to check.
Return values
string — Normalized URL.send_wpcom_proxy_request()
Send a request to the API proxy.
private
send_wpcom_proxy_request(string $method, string $endpoint, array<string|int, mixed> $request_body) : array<string|int, mixed>|WP_Error
Parameters
- $method : string
-
The HTTP method to use.
- $endpoint : string
-
The endpoint to request.
- $request_body : array<string|int, mixed>
-
The request body.
