WooCommerce Code Reference

WC_Gateway_Paypal_Request
in package

Generates requests to send to PayPal.

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.
$endpoint  : string
Endpoint for requests to PayPal.
$gateway  : WC_Gateway_Paypal
Pointer to gateway making the request.
$line_items  : array<string|int, mixed>
Stores line items to send to PayPal.
$notify_url  : string
Endpoint for requests from PayPal.
__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_request_url()  : string
Get the PayPal request URL for an order.
add_line_item()  : mixed
Add PayPal Line Item.
currency_has_decimals()  : bool
Check if currency has decimals.
delete_line_items()  : mixed
Remove all line items.
fix_request_length()  : array<string|int, mixed>
If the default request with line items is too long, generate a new one with only one line item.
get_line_item_args()  : array<string|int, mixed>
Get line item args for paypal request.
get_line_item_args_single_item()  : array<string|int, mixed>
Get line item args for paypal request as a single line item.
get_line_items()  : mixed
Return all line items.
get_order_item_name()  : string
Get order item names as a string.
get_order_item_names()  : string
Get order item names as a string.
get_paypal_args()  : array<string|int, mixed>
Get PayPal Args for passing to PP.
get_paypal_state()  : string
Get the state to send to paypal.
get_phone_number_args()  : array<string|int, mixed>
Get phone number args for paypal request.
get_shipping_args()  : array<string|int, mixed>
Get shipping args for paypal request.
get_shipping_cost_line_item()  : array<string|int, mixed>
Get shipping cost line item args for paypal request.
get_transaction_args()  : array<string|int, mixed>
Get transaction args for paypal request, except for line item args.
limit_length()  : string
Limit length of an arg.
line_items_valid()  : bool
Check if the order has valid line items to use for PayPal request.
number_format()  : string
Format prices.
prepare_line_items()  : mixed
Get line items to send to paypal.
round()  : float
Round prices.
get_approve_link()  : string|null
Get the approve link from the response data.
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.
normalize_paypal_order_shipping_country_code()  : string|null
Normalize PayPal order shipping country code.
send_wpcom_proxy_request()  : array<string|int, mixed>|null
Send a request to the API proxy.

Constants

Properties

Methods

authorize_or_capture_payment()

Authorize or capture a PayPal payment using the Orders v2 API.

public authorize_or_capture_payment(WC_Order $order, string $action_url[, string $action = WC_Gateway_Paypal_Constants::PAYMENT_ACTION_CAPTURE ]) : void

This method authorizes or captures a PayPal payment and updates the order status.

Parameters
$order : WC_Order

Order object.

$action_url : string

The URL to authorize or capture the payment.

$action : string = WC_Gateway_Paypal_Constants::PAYMENT_ACTION_CAPTURE

The action to perform. Either 'authorize' or 'capture'.

Tags
throws
Exception

If the PayPal payment authorization or capture fails.

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 = WC_Gateway_Paypal_Constants::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 = WC_Gateway_Paypal_Constants::PAYMENT_SOURCE_PAYPAL

The payment source.

$js_sdk_params : array<string|int, mixed> = array()

Extra parameters for a PayPal JS SDK (Buttons) request.

Tags
throws
Exception

If the PayPal order creation fails.

Return values
array<string|int, mixed>|null

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
throws
Exception

If the PayPal order details request fails.

throws
Exception

If the PayPal order details are not found.

Return values
array<string|int, mixed>

add_line_item()

Add PayPal Line Item.

protected add_line_item(string $item_name[, int $quantity = 1 ][, float $amount = 0.0 ][, string $item_number = '' ]) : mixed
Parameters
$item_name : string

Item name.

$quantity : int = 1

Item quantity.

$amount : float = 0.0

Amount.

$item_number : string = ''

Item number.

Return values
mixed

fix_request_length()

If the default request with line items is too long, generate a new one with only one line item.

protected fix_request_length(WC_Order $order, array<string|int, mixed> $paypal_args) : array<string|int, mixed>

If URL is longer than 2,083 chars, ignore line items and send cart to Paypal as a single item. One item's name can only be 127 characters long, so the URL should not be longer than limit. URL character limit via: https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer.

Parameters
$order : WC_Order

Order to be sent to Paypal.

$paypal_args : array<string|int, mixed>

Arguments sent to Paypal in the request.

Return values
array<string|int, mixed>

get_shipping_cost_line_item()

Get shipping cost line item args for paypal request.

protected get_shipping_cost_line_item(WC_Order $order, bool $force_one_line_item) : array<string|int, mixed>
Parameters
$order : WC_Order

Order object.

$force_one_line_item : bool

Whether one line item was forced by validation or URL length.

Return values
array<string|int, mixed>

Get the approve link from the response data.

private get_approve_link(int $http_code, array<string|int, mixed> $response_data) : string|null
Parameters
$http_code : int

The HTTP code of the response.

$response_data : array<string|int, mixed>

The response data.

Return values
string|null

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
throws
Exception

If the order items cannot be built.

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
throws
Exception

If the custom ID is too long.

Return values
string

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>|nullReturns null if the shipping is not required, or the address is not set, or is incomplete.

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

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>|null
Parameters
$method : string

The HTTP method to use.

$endpoint : string

The endpoint to request.

$request_body : array<string|int, mixed>

The request body.

Tags
throws
Exception

If the site ID is not found.

Return values
array<string|int, mixed>|nullThe API response body, or null if the request fails.