Merge branch 'refs/heads/trunk' into new-package-workflow

# Conflicts:
#	composer.lock
#	modules/ppcp-api-client/src/Endpoint/WebhookEndpoint.php
#	modules/ppcp-paypal-subscriptions/assets-compiler.json
#	modules/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php
#	package.json
This commit is contained in:
Moritz Meißelbach 2024-08-22 09:36:44 +02:00
commit c9b2fce769
No known key found for this signature in database
GPG key ID: 9FDCE7BEB31FA3E5
576 changed files with 75586 additions and 10423 deletions

9
.eslintrc Normal file
View file

@ -0,0 +1,9 @@
{
"extends": [ "plugin:@wordpress/eslint-plugin/recommended" ],
"globals": {
"wc": true
},
"rules": {
"no-console": ["error", { "allow": ["warn", "error"] }]
}
}

View file

@ -1,13 +1,13 @@
name: e2e tests
on: [push]
on: workflow_dispatch
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.3', '7.4', '8.1']
php-versions: ['7.3', '7.4', '8.2']
wc-versions: ['5.9.5', '7.7.2']
name: PHP ${{ matrix.php-versions }} WC ${{ matrix.wc-versions }}

View file

@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1']
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
name: PHP ${{ matrix.php-versions }}
steps:

6
.gitignore vendored
View file

@ -4,9 +4,9 @@
node_modules
.phpunit.result.cache
yarn-error.log
modules/ppcp-button/assets/*
modules/ppcp-wc-gateway/assets/js
modules/ppcp-wc-gateway/assets/css
modules/*/vendor/*
modules/*/assets/*
!modules/ppcp-wc-gateway/assets/images
*.zip
.env
.env.e2e

1
.megaignore Normal file
View file

@ -0,0 +1 @@
-s:*

View file

@ -50,6 +50,27 @@ namespace Vendidero\Germanized\Shipments {
public function add_note( $note, $added_by_user = false ) {
}
/**
* Return an array of items within this shipment.
*
* @return ShipmentItem[]
*/
public function get_items() {
}
}
class ShipmentItem extends WC_Data {
/**
* Get order ID this meta belongs to.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return int
*/
public function get_order_item_id( $context = 'view' ) {
}
}
}

View file

@ -2,6 +2,12 @@
if (!defined('PAYPAL_INTEGRATION_DATE')) {
define('PAYPAL_INTEGRATION_DATE', '2023-06-02');
}
if (!defined('PAYPAL_URL')) {
define( 'PAYPAL_URL', 'https://www.paypal.com' );
}
if (!defined('PAYPAL_SANDBOX_URL')) {
define( 'PAYPAL_SANDBOX_URL', 'https://www.sandbox.paypal.com' );
}
if (!defined('EP_PAGES')) {
define('EP_PAGES', 4096);
}
@ -11,11 +17,18 @@ if (!defined('MONTH_IN_SECONDS')) {
if (!defined('HOUR_IN_SECONDS')) {
define('HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS);
}
if (!defined('MINUTE_IN_SECONDS')) {
define( 'MINUTE_IN_SECONDS', 60 );
}
if (!defined('ABSPATH')) {
define('ABSPATH', '');
}
if (!defined('PPCP_PAYPAL_BN_CODE')) {
define('PPCP_PAYPAL_BN_CODE', 'Woo_PPCP');
}
/**
* Cancel the next occurrence of a scheduled action.
*
@ -32,6 +45,27 @@ if (!defined('ABSPATH')) {
*
* @return string|null The scheduled action ID if a scheduled action was found, or null if no matching action found.
*/
function as_unschedule_action($hook, $args = array(), $group = '')
{
function as_unschedule_action($hook, $args = array(), $group = '') {}
/**
* Schedule an action to run one time
*
* @param int $timestamp When the job will run.
* @param string $hook The hook to trigger.
* @param array $args Arguments to pass when the hook triggers.
* @param string $group The group to assign this job to.
* @param bool $unique Whether the action should be unique.
*
* @return int The action ID.
*/
function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false ) {}
/**
* HTML API: WP_HTML_Tag_Processor class
*/
class WP_HTML_Tag_Processor {
public function __construct( $html ) {}
public function next_tag( $query = null ) {}
public function set_attribute( $name, $value ) {}
public function get_updated_html() {}
}

View file

@ -2107,3 +2107,16 @@ function wc_get_page_screen_id( $for ) {}
*
*/
class WC_Product_Subscription_Variation extends WC_Product_Variation {}
/**
* Variable Subscription Product Class
*
* This class extends the WC Variable product class to create variable products with recurring payments.
*
* @class WC_Product_Variable_Subscription
* @package WooCommerce Subscriptions
* @category Class
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v1.3
*
*/
class WC_Product_Variable_Subscription extends WC_Product_Variable {}

View file

@ -23,6 +23,8 @@ Optionally, change the `PAYPAL_INTEGRATION_DATE` constant to `gmdate( 'Y-m-d' )`
2. `$ ./vendor/bin/phpunit`
3. `$ ./vendor/bin/phpcs`
4. `$ ./vendor/bin/psalm`
5. `$ wp-scripts lint-js`
6. `$ yarn run test:unit-js` - Ensure node version is `18` or above
### Building a release package
@ -65,7 +67,8 @@ You may also need `$ ddev restart` to apply the config changes.
Tests and code style:
- `$ yarn ddev:test`
- `$ yarn ddev:lint`
- `$ yarn ddev:fix-lint` - PHPCBF to fix basic code style issued
- `$ yarn ddev:fix-lint`
- `$ yarn ddev:lint-js`
See [package.json](/package.json) for other useful commands.

View file

@ -19,7 +19,9 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\PPCP;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\RefundFeesUpdater;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
/**
@ -46,6 +48,19 @@ function ppcp_get_paypal_order( $paypal_id_or_wc_order ): Order {
return $order_endpoint->order( $paypal_id_or_wc_order );
}
/**
* Creates a PayPal order for the given WC order.
*
* @param WC_Order $wc_order The WC order.
* @throws Exception When the operation fails.
*/
function ppcp_create_paypal_order_for_wc_order( WC_Order $wc_order ): Order {
$order_processor = PPCP::container()->get( 'wcgateway.order-processor' );
assert( $order_processor instanceof OrderProcessor );
return $order_processor->create_order( $wc_order );
}
/**
* Captures the PayPal order.
*
@ -72,6 +87,32 @@ function ppcp_capture_order( WC_Order $wc_order ): void {
}
}
/**
* Reauthorizes the PayPal order.
*
* @param WC_Order $wc_order The WC order.
* @throws InvalidArgumentException When the order cannot be captured.
* @throws Exception When the operation fails.
*/
function ppcp_reauthorize_order( WC_Order $wc_order ): void {
$intent = strtoupper( (string) $wc_order->get_meta( PayPalGateway::INTENT_META_KEY ) );
if ( $intent !== 'AUTHORIZE' ) {
throw new InvalidArgumentException( 'Only orders with "authorize" intent can be reauthorized.' );
}
$captured = wc_string_to_bool( $wc_order->get_meta( AuthorizedPaymentsProcessor::CAPTURED_META_KEY ) );
if ( $captured ) {
throw new InvalidArgumentException( 'The order is already captured.' );
}
$authorized_payment_processor = PPCP::container()->get( 'wcgateway.processor.authorized-payments' );
assert( $authorized_payment_processor instanceof AuthorizedPaymentsProcessor );
if ( $authorized_payment_processor->reauthorize_payment( $wc_order ) !== AuthorizedPaymentsProcessor::SUCCESSFUL ) {
throw new RuntimeException( $authorized_payment_processor->reauthorization_failure_reason() ?: 'Reauthorization failed.' );
}
}
/**
* Refunds the PayPal order.
* Note that you can use wc_refund_payment() to trigger the refund in WC and PayPal.
@ -107,3 +148,14 @@ function ppcp_void_order( WC_Order $wc_order ): void {
$refund_processor->void( $order );
}
/**
* Updates the PayPal refund fees totals on an order.
*
* @param WC_Order $wc_order The WC order.
*/
function ppcp_update_order_refund_fees( WC_Order $wc_order ): void {
$updater = PPCP::container()->get( 'wcgateway.helper.refund-fees-updater' );
assert( $updater instanceof RefundFeesUpdater );
$updater->update( $wc_order );
}

3
babel.config.json Normal file
View file

@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}

View file

@ -1,5 +1,312 @@
*** Changelog ***
= 2.8.3 - 2024-08-12 =
* Fix - Google Pay: Prevent field validation from being triggered on checkout page load #2474
* Fix - Do not add tax info into order meta during order creation #2471
* Fix - PayPal declares subscription support when for Subscription mode is set Disable PayPal for subscription #2425
* Fix - PayPal js files loaded on non PayPal pages #2411
* Fix - Google Pay: Fix the incorrect popup triggering #2414
* Fix - Add tax configurator when programmatically creating WC orders #2431
* Fix - Shipping callback compatibility with WC Name Your Price plugin #2402
* Fix - Uncaught Error: Cannot use object of type ...\Settings as array in .../AbstractPaymentMethodType.php (3253) #2334
* Fix - Prevent displaying smart button multiple times on variable product page #2420
* Fix - Prevent enabling Standard Card Button when ACDC is enabled #2404
* Fix - Use client credentials for user tokens #2491
* Fix - Apple Pay: Fix the shipping callback #2492
* Enhancement - Separate Google Pay button for Classic Checkout #2430
* Enhancement - Add Apple Pay and Google Pay support for China, simplify country-currency matrix #2468
* Enhancement - Add AMEX support for Advanced Card Processing in China #2469
= 2.8.2 - 2024-07-22 =
* Fix - Sold individually checkbox automatically disabled after adding product to the cart more than once #2415
* Fix - All products "Sold individually" when PayPal Subscriptions selected as Subscriptions Mode #2400
* Fix - W3 Total Cache: Remove type from file parameter as sometimes null gets passed causing errors #2403
* Fix - Shipping methods during callback not updated correctly #2421
* Fix - Preserve subscription renewal processing when switching Subscriptions Mode or disabling gateway #2394
* Fix - Remove shipping callback for Venmo express button #2374
* Fix - Google Pay: Fix issuse with data.paymentSource being undefined #2390
* Fix - Loading of non-Order as a WC_Order causes warnings and potential data corruption #2343
* Fix - Apple Pay and Google Pay buttons don't appear in PayPal Button stack on multi-step Checkout #2372
* Fix - Apple Pay: Fix when shipping is disabled #2391
* Fix - Wrong string in smart button preview on Standard Payments tab #2409
* Fix - Don't break orders screen when there is an exception for package tracking #2369
* Fix - Pay Later button preview is missing #2371
* Fix - Apple Pay button layout #2367
* Enhancement - Remove BCDC button from block Express Checkout area #2381
* Enhancement - Extend Advanced Card Processing country eligibility for China #2397
= 2.8.1 - 2024-07-01 =
* Fix - Don't render tracking metabox if PayPal order does not belong to connected merchant #2360
* Fix - Fatal error when the ppcp-paylater-configurator module is disabled via code snippet #2327
* Fix - Apple Pay & Google Pay buttons no longer visible in Standard Payments button previews after moving the configuration to Advanced Card Processing tab #2325
* Fix - Fix Smart Buttons on Elementor checkout widget #2284
* Fix - Pay by link - Capturing order from guest user causing fatal error when Vaulting is enabled #2382
* Fix - Enable the gateway settings JS file on connection tab #2377
* Enhancement - Add filter for certain settings to allow gateway translation e.g. via WPML #2308
* Enhancement - Filter for adding more contexts in can_render_dcc checker #2346
* Enhancement - Do not request id_token for guest users #2283
* Enhancement - Prevent multiple PayPal Subscription products in the cart if PayPal Subscription API is active #2320
* Enhancement - Prevent script caching & minification from Litespeed Cache and W3 Total Cache plugins #2316
* Enhancement - Remove Giropay references due to deprecation #2379
= 2.8.0 - 2024-06-11 =
* Fix - Calculate totals after adding shipping to include taxes #2296
* Fix - Package tracking integration throws error in 2.7.1 #2289
* Fix - Make PayPal Subscription products unique in cart #2265
* Fix - PayPal declares subscription support when merchant not enabled for Reference Transactions #2282
* Fix - Google Pay and Apple Pay Settings button from Connection tab have wrong links #2273
* Fix - Smart Buttons in Block Checkout not respecting the location setting (2830) #2278
* Fix - Disable Pay Upon Invoice if billing/shipping country not set #2281
* Fix - Critical error on pay for order page when we try to pay with ACDC gateway #2321
* Enhancement - Enable shipping callback for WC subscriptions #2259
* Enhancement - Disable the shipping callback for "venmo" when vaulting is active #2269
* Enhancement - Improve "Could not retrieve order" error message #2271
* Enhancement - Add block Checkout compatibility to Advanced Card Processing #2246
= 2.7.1 - 2024-05-28 =
* Fix - Ensure package tracking data is sent to original PayPal transaction #2180
* Fix - Set the 'Woo_PPCP' as a default value for data-partner-attribution-id #2188
* Fix - Allow PUI Gateway for refund processor #2192
* Fix - Notice on newly created block cart checkout #2211
* Fix - Apple Pay button in the editor #2177
* Fix - Allow shipping callback and skipping confirmation page from any express button #2236
* Fix - Pay Later messaging configurator sometimes displays old settings after saving #2249
* Fix - Update the apple-developer-merchantid-domain-association validation strings for Apple Pay #2251
* Fix - Enable the Shipping Callback handlers #2266
* Enhancement - Use admin theme color #1602
= 2.7.0 - 2024-04-30 =
* Fix - Zero sum subscriptions cause CANNOT_BE_ZERO_OR_NEGATIVE when using Vault v3 #2152
* Fix - Incorrect Pricing Issue with Variable Subscriptions in PayPal Subscriptions Mode #2156
* Fix - Wrong return_url in multisite setup when using subdomains #2157
* Fix - Fix the fundingSource is not defined error on Block Checkout #2185
* Enhancement - Add the data-page-type attribute for JS SDK #2161
* Enhancement - Save Card Last Digits in order meta for Advanced Card Payments #2149
* Enhancement - Refactor the Pay Later Messaging block and add dedicated Cart/Checkout blocks #2153
* Enhancement - "Next Payment" status not updated when using PayPal Subscriptions #2091
* Enhancement - Optimize default settings for new store configurations #2158
* Enhancement - Improve tooltip information for tagline #2154
* Enhancement - Improve error message on certain exceptions #1354
* Enhancement - Cart Pay Later block: Change the default insert position #2179
* Enhancement - Messages Bootstrap: Add a render retry functionality #2181
= 2.6.1 - 2024-04-09 =
* Fix - Payment tokens fixes and adjustments #2106
* Fix - Pay upon Invoice: Add input validation to Experience Context fields #2092
* Fix - Disable markup in get_plugin_data() returns to fix an issue with wptexturize() #2094
* Fix - Problem changing the shipping option in block pages #2142
* Fix - Saved payment token deleted after payment with another saved payment token #2146
* Enhancement - Pay later messaging configurator improvements #2107
* Enhancement - Replace the middleware URL from connect.woocommerce.com to api.woocommerce.com/integrations #2130
* Enhancement - Remove all Sofort references as it has been deprecated #2124
* Enhancement - Improve funding source names #2118
* Enhancement - More fraud prevention capabilities by storing additional data in the order #2125
* Enhancement - Update ACDC currency eligibility for AMEX #2129
* Enhancement - Sync shipping options with Venmo when skipping final confirmation on Checkout #2108
* Enhancement - Card Fields: Add a filter for the CVC field and update the placeholder to match the label #2089
* Enhancement - Product Title: Sanitize before sending to PayPal #2090
* Enhancement - Add filter for disabling permit_multiple_payment_tokens vault attribute #2136
* Enhancement - Filter to hide PayPal email address not working on order detail #2137
= 2.6.0 - 2024-03-20 =
* Fix - invoice_id not included in API call when creating payment with saved card #2086
* Fix - Typo in SCA indicators for ACDC Vault transactions #2083
* Fix - Payments with saved card tokens use Capture intent when Authorize is configured #2069
* Fix - WooPayments multi-currency causing currency mismatch error on Block Cart & Checkout pages #2054
* Fix - "Must pass createSubscription with intent=subscription" error with PayPal Subscriptions mode #2058
* Fix - "Proceed to PayPal" button displayed for Free trial PayPal Subscription products when payment token is saved #2041
* Fix - ACDC payments with new credit card may fail when debugging is enabled (JSON malformed by warning) #2051
* Enhancement - Add Pay Later Messaging block #1897
* Enhancement - Submit the form instead of refreshing the page to show the save notice #2081
* Enhancement - Integrate pay later messaging block with the messaging configurator #2080
* Enhancement - Reauthorize authorized payments #2062
* Enhancement - Do not handle VAULT.PAYMENT-TOKEN.CREATED webhook for Vault v3 #2079
* Enhancement - Improve the messaging configurator styles #2053
* Enhancement - Ensure PayPal Vaulting is not selected as Subscriptions Mode when Reference Transactions are disabled #2057
* Enhancement - Pay later messaging configurator & messaging block adjustments #2096
= 2.5.4 - 2024-02-27 =
* Fix - Cannot enable Apple Pay when API credentials were manually created #2015
* Fix - Cart simulation type error #1943
* Enhancement - Apple Pay recurring payments #1986
* Enhancement - Real Time Account Updater (RTAU) integration #2027
* Enhancement - Prepare the SKU for sending to PayPal #2033
* Enhancement - Store the Card Brand in Address Verification Result instead of 3DS authentication result #2026
* Enhancement - Update country eligibility for AdvancedCard Processing, Apple Pay, Google Pay #2019
* Enhancement - Disable PayPal Vaulting setting instead of hiding it when Reference Transactions not available #2029
* Enhancement - Store three d secure enrollment status and authentication status responses in wc order #1980
* Enhancement - Add more checks to prevent "PayPal order ID not found" errors #2038
* Enhancement - Disable messaging configurator when vault is enabled #2042
* Feature preview - Pay Later Messaging configurator #1924
= 2.5.3 - 2024-02-06 =
* Fix - Free trial subscription products using PayPal Vaulting when PayPal Subscriptions configured as Subscriptions Mode #1979
* Fix - Pay by link - Germany - PayPal buttons are not visible on Pay for order page #2014
* Enhancement - Extend Apple Pay, Google Pay, Vault v3 (& RTAU) country availability #1992
* Enhancement - Enable card fields for ACDC and Vault v3 supported countries/currencies #2007
* Enhancement - Update ACDC supported currencies list #1991
* Enhancement - Check if the $wpdb->wc_orders exists before query #1996
* Enhancement - Remove MercadoPago from disable funding sources #2003
* Enhancement - Improve onboarding notice text #2002
= 2.5.2 - 2024-02-01 =
* Fix - NOT_ENABLED_TO_VAULT_PAYMENT_SOURCE error for merchants without reference transactions #1984
* Fix - Fatal error in WooCommerce PayPal Payments plugin after 2.5.0 update #1985
* Fix - Can not refund order purchased with Vault v3 Card payment #1997
* Fix - PayPal Vaulting Subscriptions mode setting visible when merchant not enabled for Reference Transactions #1999
* Fix - card-fields parameter included in button script despite Advanced Card Processing disabled #2005
* Enhancement - Add setup URL for reference transactions #1964
* Enhancement - Improve PUI performance for variable products #1950
= 2.5.1 - 2024-01-24 =
* Temporary revert Vaulting integration changes introduced in 2.5.0
= 2.5.0 - 2024-01-22 =
* Fix - WC Subscriptions change subscription payment #1953
* Fix - GooglePay and ApplePay buttons disappear from the minicart when adding a product to the cart on the shop page #1915
* Enhancement - Enable Vault v3 and Card Fields by default for US merchants #1967
* Enhancement - Vault v3 WC Subscriptions integration #1920
* Enhancement - Implement early WC validation for Hosted Card Fields #1925
* Enhancement - Rename button locations #1946
* Enhancement - Improve Apple Pay validation notice text #1938
* Enhancement - Improve feature availability check UX #1941
* Enhancement - Make all hosted card fields strings translatable #1926
* Enhancement - PHP 8.2 deprecations #1939
* Enhancement - Subscription support on Block Cart & Block Express Checkout #1956
* Enhancement - Venmo Vaulting integration #1958
* Enhancement - Add package tracking support for UK #1970
= 2.4.3 - 2024-01-04 =
* Fix - PayPal Subscription initiated without a WooCommerce order #1907
* Fix - Block Checkout reloads when submitting order with empty fields #1904
* Fix - "Send checkout billing and shipping data to Apple Pay" displayed when Apple Pay is disabled #1883
* Fix - "Order does not contain intent" error for ACDC renewals when triggering 3D Secure #1888
* Fix - PayPal Subscriptions button greyed out (inactive) on Checkout page for variable subscription products #1914
* Enhancement - Add button to reload feature eligibility status from Connection tab #1902
* Enhancement - Apple Pay validation message improvements #1901
* Enhancement - Improve support for Classic Cart & Classic Checkout blocks #1894
* Enhancement - Ensure uniform button appearance for PayPal, Google Pay, and Apple Pay buttons #1900
* Enhancement - remove string translations for package tracking carriers from repository #1885
* Enhancement - Incorrect margins when PayPal buttons are rendered as separate gateways. #1908
* Enhancement - Improved button spacing when Apple Pay is enabled but current buyer is not eligible #1922
* Feature preview - Save payment methods (Vault v3) integration #1779
= 2.4.2 - 2023-12-04 =
* Fix - Action callback arguments count in ShipStation tracking integration #1841
* Fix - Google Pay scripts loading on unrelated admin pages #1834
* Fix - Do not ignore disabled APMs list in blocks #1865
* Fix - Display Package Tracking metabox below Order actions when HPOS is active #1850
* Fix - ApplePay use checkout form data to update shipping and billing #1832
* Fix - Fix Apple Pay CSS #1872
* Enhancement - Allow redirect to PayPal with "Place order" button if smart buttons failed to load #1840 #1870
* Enhancement - Extend list of supported countries for Package Tracking v2 integration #1848
* Enhancement - Improve Block Theme support for Pay Later messaging #1855
* Enhancement - Render block buttons separately and add block style settings #1858
* Enhancement - Enable Block Cart and Block Express Checkout button locations by default #1852
* Enhancement - Improve single product page button placement with Block themes #1847
* Enhancement - Remove the Home location from default enabled Pay Later messaging locations #1856
* Enhancement - Chrome browser detected as eligible for Apple Pay on settings page #1828
* Enhancement - Hide Apple Pay & Google Pay for subscription type products #1835
* Enhancement - Add Standard Card Button gateway styling settings & preview #1827
* Feature preview - Upgrade to new Hosted Card Fields for Advanced Card Processing #1843
= 2.4.1 - 2023-11-14 =
* Fix - Error "PayPal order ID not found in meta" prevents automations from triggering when buying subscription via third-party payment gateway #1822
* Fix - Card button subscription support declaration #1796
* Fix - Pay Later messaging disappears when updating shipping option on cart page #1807
* Fix - Apple Pay payment from single product may fail after changing shipping options in Apple Pay payment sheet #1810
* Enhancement - Extend list of supported countries for Advanced Card Processing #1808
* Enhancement - Extend Apple Pay/Google Pay country eligibility to Italy #1811
* Enhancement - Override language used to display PayPal buttons #600
* Enhancement - Apple Pay button preview #1824
* Enhancement - Add Apple Pay & Google Pay logos on the onboarding page #1823
* Enhancement - Improve Apple Pay compatibility with variable products on single product page #1803
* Enhancement - Apple Pay domain registration & browser eligibility check #1821
* Enhancement - Package Tracking compatibility with WooCommerce Shipping & ShipStation for WooCommerce #1813
* Enhancement - Fill form when continuation in block #1794
* Enhancement - Display Shop location Pay Later messaging on product category pages #1809
* Enhancement - Present apple-developer-merchantid-domain-association file only when Apple Pay is enabled #1818
* Enhancement - Improve Apple Pay compatibility on Pay for Order page #1815
* Enhancement - Display Pay Later messages before the payment methods on the Pay for Order page #1814
* Enhancement - Handle undefined array key warnings on PHP 8.1 #1804
= 2.4.0 - 2023-10-31 =
* Fix - Mini-Cart Bug cause of wrong DOM-Structure in v2.3.1 #1735
* Fix - ACDC disappearing after plugin updates #1751
* Fix - Subscription module hooks #1748
* Fix - Ensure PayPal Subscriptions API products description is 1-127 characters #1738
* Fix - Add validation on the Plan Name field to not accept a blank value #1754
* Enhancement - Improve Pay Later messages and add Shop, Home locations #1770
* Enhancement - Use api-m PayPal API URLs #1740
* Enhancement - Google Pay Settings improvements #1719
* Enhancement - Apple Pay transaction improvements #1767
* Enhancement - Change default ACDC title #1750
* Enhancement - Cart simulation improvements #1753
* Enhancement - Billing schedule fields not greyed out when PayPal Subscriptions product is connected #1755
* Enhancement - Check validation errors when submitting in block #1528
* Enhancement - Improve handling of server error when submitting block #1785
* Enhancement - Extend Apple Pay country eligibility #1781
* Enhancement - Apple Pay validation notice improvements #1783
* Enhancement - Apple Pay payment process issues #1789
* Enhancement - Disable the tracking if payment is not captured #1780
* Enhancement - Place order button remains - Could not retrieve order #1786
* Enhancement - Google Pay for variable product greyed out but clickable #1788
* Enhancement - Merchant credential validation & remove PAYEE object #1795
= 2.3.1 - 2023-09-26 =
* Fix - Fatal error when saving product while WooCommerce Subscriptions plugin is not active #1731
* Fix - Validate tracking data only for add/update Package Tracking #1729
* Fix - Disable Package Tracking for order if transaction ID doesn't exist #1727
= 2.3.0 - 2023-09-26 =
* Fix - Plus sign in PayPal account email address gets converted to space #771
* Fix - Payment method dropdown option label on edit order screen for ppcp-gateway option displaying wrong name #1639
* Fix - WooCommerce Bookings products don't remain in Cart as a guest when PayPal button active on single product #1645
* Fix - Since version > 2.2.0 the PayPal Checkout button on single product pages does not redirect anymore #1664
* Fix - PayPal fee and PayPal Payout do not change on order if we do partial refund #1578
* Fix - Order does not contain intent error when using ACDC payment token while buyer is not present #1506
* Fix - Error when product description linked with a PayPal subscription exceeds 127 characters #1700
* Fix - $_POST uses the wrong key to hold the shipping method #1652
* Fix - WC Payment Token created multiple times when webhook is received #1663
* Fix - Subtotal mismatch line name shows on Account settings page when merchant is disconnected #1702
* Fix - Warning prevents payments on Pay for Order page when debugging is enabled #1703
* Fix - paypal-overlay-uid_ blocks page after closing PayPal popup on Pay for Order page | Terms checkbox validation fails on Pay for Order page #1704
* Enhancement - Add support for HPOS for tracking module #1676
* Enhancement - Billing agreements endpoint called too frequently for Reference Transactions check #1646
* Enhancement - Do not declare subscription support for PayPal when only ACDC vaulting #1669
* Enhancement - Apply Capture On Status Change only when order contains PayPal payment method #1595
* Enhancement - Do not use transient expiration longer than one month to support memcached #1448
* Enhancement - By disconnecting or disabling the plugin the connection should clear the Onboarding links from cache #1668
* Enhancement - Upgrade tracking integration #1562
* Enhancement - Include url & image_url in create order call #1649
* Enhancement - Add compat layer for Yith tracking #1656
* Enhancement - Improve invalid currency backend notice (1926) #1588
* Enhancement - Hide ACDC footer frame via CSS to avoid empty space #1613
* Enhancement - Compatibility with WooCommerce Product Add-Ons plugin #1586
* Enhancement - Remove "no shipment" message after adding tracking #1674
* Enhancement - Improve error & success validation messages #1675
* Enhancement - Compatibility with third-party "Product Add-Ons" plugins #1601
* Enhancement - PayPal logo flashes when switching between tabs #1345
* Enhancement - Include url & image_url in create order call #1649
* Enhancement - Include item_url & image_url to tracking call #1712
* Enhancement - Update strings for tracking metabox #1714
* Enhancement - Validate email address API credentials field #1691
* Enhancement - Set payment method title for order edit page only if our gateway #1661
* Enhancement - Fix missing Pay Later messages in cart + refactoring #1683
* Enhancement - Product page PP button keep loading popup - "wc_add_to_cart_params is not defined" error in WooCommerce #1655
* Enhancement - Remove PayPal Subscriptions API feature flag #1690
* Enhancement - Don't send image_url when it is empty #1678
* Enhancement - Subscription support depending on Vaulting setting instead of subscription mode setting #1697
* Enhancement - Wrong PayPal subscription id on vaulted subscriptions #1699
* Enhancement - Remove payment vaulted checker functionality (2030) #1711
* Feature preview - Apple Pay integration #1514
* Feature preview - Google Pay integration #1654
= 2.2.2 - 2023-08-29 =
* Fix - High rate of auth voids on vaulted subscriptions for guest users #1529
* Enhancement - HPOS compatibility issues #1594
* Feature preview - PayPal Subscriptions API fixes and improvements #1600 #1607
= 2.2.1 - 2023-08-24 =
* Fix - One-page checkout causes mini cart not showing the PP button on certain pages #1536
* Fix - When onboarding loading the return_url too fast may cause the onboarding to fail #1565

View file

@ -23,13 +23,14 @@
"phpunit/phpunit": "^7.0 | ^8.0 | ^9.0",
"brain/monkey": "^2.4",
"php-stubs/wordpress-stubs": "^5.0@stable",
"php-stubs/woocommerce-stubs": "^5.0@stable",
"php-stubs/woocommerce-stubs": "^8.0@stable",
"vimeo/psalm": "^4.0",
"vlucas/phpdotenv": "^5"
},
"autoload": {
"psr-4": {
"WooCommerce\\PayPalCommerce\\": "src",
"WooCommerce\\PayPalCommerce\\Common\\": "lib/common/",
"WooCommerce\\PayPalCommerce\\Vendor\\": "lib/packages/"
},
"files": [

761
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,10 @@
## packages
The packages that are likely to cause conflicts with other plugins (by loading multiple incompatible versions).
Their namespaces are isolated by [Mozart](https://github.com/coenjacobs/mozart).
Currently, the packages are simply added in the repo to avoid making the build process more complex (Mozart has different PHP requirements).
We need to isolate only PSR-11 containers and Dhii modularity packages, which are not supposed to change often.
## common
This folder contains reusable classes or components that do not fit into any specific module.
They are designed to be versatile and can be used by any module within the plugin.

View file

@ -0,0 +1,68 @@
<?php
/**
* The Singleton Trait can be used to wrap an execution block, so it behaves like a Singleton.
* It executes the callable once, on subsequent calls returns the same result.
*/
namespace WooCommerce\PayPalCommerce\Common\Pattern;
/**
* Class SingletonDecorator.
*/
class SingletonDecorator {
/**
* The callable with the executing code
*
* @var callable
*/
private $callable;
/**
* The execution result
*
* @var mixed
*/
private $result;
/**
* Indicates if the callable is resolved
*
* @var bool
*/
private $executed = false;
/**
* SingletonDecorator constructor.
*
* @param callable $callable
*/
public function __construct( callable $callable ) {
$this->callable = $callable;
}
/**
* The make constructor.
*
* @param callable $callable
* @return self
*/
public static function make( callable $callable ): self {
return new static( $callable );
}
/**
* Invokes a callable once and returns the same result on subsequent invokes.
*
* @param mixed ...$args Arguments to be passed to the callable.
* @return mixed
*/
public function __invoke( ...$args ) {
if ( ! $this->executed ) {
$this->result = call_user_func_array( $this->callable, $args );
$this->executed = true;
}
return $this->result;
}
}

View file

@ -0,0 +1,41 @@
<?php
/**
* The Singleton Trait can be used to add singleton behaviour to a class.
*
* @package WooCommerce\PayPalCommerce\Common\Pattern
*/
namespace WooCommerce\PayPalCommerce\Common\Pattern;
/**
* Class SingletonTrait.
*/
trait SingletonTrait {
/**
* The single instance of the class.
*
* @var self
*/
protected static $instance = null;
/**
* Static method to get the instance of the Singleton class
*
* @return self|null
*/
public static function get_instance(): ?self {
return self::$instance;
}
/**
* Static method to get the instance of the Singleton class
*
* @param self $instance
* @return self
*/
protected static function set_instance( self $instance ): self {
self::$instance = $instance;
return self::$instance;
}
}

View file

@ -5,6 +5,9 @@
* @package WooCommerce\PayPalCommerce
*/
use WooCommerce\PayPalCommerce\PayLaterBlock\PayLaterBlockModule;
use WooCommerce\PayPalCommerce\PayLaterWCBlocks\PayLaterWCBlocksModule;
use WooCommerce\PayPalCommerce\PayLaterConfigurator\PayLaterConfiguratorModule;
use WooCommerce\PayPalCommerce\PluginModule;
return function ( string $root_dir ): iterable {
@ -15,19 +18,75 @@ return function ( string $root_dir ): iterable {
( require "$modules_dir/woocommerce-logging/module.php" )(),
( require "$modules_dir/ppcp-admin-notices/module.php" )(),
( require "$modules_dir/ppcp-api-client/module.php" )(),
( require "$modules_dir/ppcp-button/module.php" )(),
( require "$modules_dir/ppcp-compat/module.php" )(),
( require "$modules_dir/ppcp-button/module.php" )(),
( require "$modules_dir/ppcp-onboarding/module.php" )(),
( require "$modules_dir/ppcp-session/module.php" )(),
( require "$modules_dir/ppcp-status-report/module.php" )(),
( require "$modules_dir/ppcp-subscription/module.php" )(),
( require "$modules_dir/ppcp-wc-subscriptions/module.php" )(),
( require "$modules_dir/ppcp-wc-gateway/module.php" )(),
( require "$modules_dir/ppcp-webhooks/module.php" )(),
( require "$modules_dir/ppcp-vaulting/module.php" )(),
( require "$modules_dir/ppcp-order-tracking/module.php" )(),
( require "$modules_dir/ppcp-uninstall/module.php" )(),
( require "$modules_dir/ppcp-blocks/module.php" )(),
( require "$modules_dir/ppcp-paypal-subscriptions/module.php" )(),
);
// phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
if ( apply_filters(
'woocommerce.feature-flags.woocommerce_paypal_payments.applepay_enabled',
getenv( 'PCP_APPLEPAY_ENABLED' ) !== '0'
) ) {
$modules[] = ( require "$modules_dir/ppcp-applepay/module.php" )();
}
if ( apply_filters(
'woocommerce.feature-flags.woocommerce_paypal_payments.googlepay_enabled',
getenv( 'PCP_GOOGLEPAY_ENABLED' ) !== '0'
) ) {
$modules[] = ( require "$modules_dir/ppcp-googlepay/module.php" )();
}
if ( apply_filters(
'woocommerce.deprecated_flags.woocommerce_paypal_payments.saved_payment_checker_enabled',
getenv( 'PCP_SAVED_PAYMENT_CHECKER_ENABLED' ) === '1'
) ) {
$modules[] = ( require "$modules_dir/ppcp-saved-payment-checker/module.php" )();
}
if ( apply_filters(
'woocommerce.feature-flags.woocommerce_paypal_payments.card_fields_enabled',
getenv( 'PCP_CARD_FIELDS_ENABLED' ) !== '0'
) ) {
$modules[] = ( require "$modules_dir/ppcp-card-fields/module.php" )();
}
if ( apply_filters(
'woocommerce.feature-flags.woocommerce_paypal_payments.save_payment_methods_enabled',
getenv( 'PCP_SAVE_PAYMENT_METHODS' ) !== '0'
) ) {
$modules[] = ( require "$modules_dir/ppcp-save-payment-methods/module.php" )();
}
if ( PayLaterBlockModule::is_module_loading_required() ) {
$modules[] = ( require "$modules_dir/ppcp-paylater-block/module.php" )();
}
if ( PayLaterConfiguratorModule::is_enabled() ) {
$modules[] = ( require "$modules_dir/ppcp-paylater-configurator/module.php" )();
if ( PayLaterWCBlocksModule::is_module_loading_required() ) {
$modules[] = ( require "$modules_dir/ppcp-paylater-wc-blocks/module.php" )();
}
}
if ( apply_filters(
'woocommerce.feature-flags.woocommerce_paypal_payments.axo_enabled',
getenv( 'PCP_AXO_ENABLED' ) === '1'
) ) {
$modules[] = ( require "$modules_dir/ppcp-axo/module.php" )();
}
return $modules;
};

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\AdminNotices;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
@ -40,6 +42,34 @@ class AdminNotices implements ModuleInterface {
$renderer->render();
}
);
add_action(
Repository::NOTICES_FILTER,
/**
* Adds persisted notices to the notices array.
*
* @param array $notices The notices.
* @return array
*
* @psalm-suppress MissingClosureParamType
*/
function ( $notices ) use ( $c ) {
if ( ! is_array( $notices ) ) {
return $notices;
}
$admin_notices = $c->get( 'admin-notices.repository' );
assert( $admin_notices instanceof Repository );
$persisted_notices = $admin_notices->get_persisted_and_clear();
if ( $persisted_notices ) {
$notices = array_merge( $notices, $persisted_notices );
}
return $notices;
}
);
}
/**

View file

@ -35,17 +35,26 @@ class Message {
*/
private $dismissable;
/**
* The wrapper selector that will contain the notice.
*
* @var string
*/
private $wrapper;
/**
* Message constructor.
*
* @param string $message The message text.
* @param string $type The message type.
* @param bool $dismissable Whether the message is dismissable.
* @param string $wrapper The wrapper selector that will contain the notice.
*/
public function __construct( string $message, string $type, bool $dismissable = true ) {
public function __construct( string $message, string $type, bool $dismissable = true, string $wrapper = '' ) {
$this->type = $type;
$this->message = $message;
$this->dismissable = $dismissable;
$this->wrapper = $wrapper;
}
/**
@ -74,4 +83,27 @@ class Message {
public function is_dismissable(): bool {
return $this->dismissable;
}
/**
* Returns the wrapper selector that will contain the notice.
*
* @return string
*/
public function wrapper(): string {
return $this->wrapper;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array(): array {
return array(
'type' => $this->type,
'message' => $this->message,
'dismissable' => $this->dismissable,
'wrapper' => $this->wrapper,
);
}
}

View file

@ -41,9 +41,10 @@ class Renderer implements RendererInterface {
$messages = $this->repository->current_message();
foreach ( $messages as $message ) {
printf(
'<div class="notice notice-%s %s"><p>%s</p></div>',
'<div class="notice notice-%s %s" %s><p>%s</p></div>',
$message->type(),
( $message->is_dismissable() ) ? 'is-dismissible' : '',
( $message->wrapper() ? sprintf( 'data-ppcp-wrapper="%s"', esc_attr( $message->wrapper() ) ) : '' ),
wp_kses_post( $message->message() )
);
}

View file

@ -16,7 +16,8 @@ use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
*/
class Repository implements RepositoryInterface {
const NOTICES_FILTER = 'ppcp.admin-notices.current-notices';
const NOTICES_FILTER = 'ppcp.admin-notices.current-notices';
const PERSISTED_NOTICES_OPTION = 'woocommerce_ppcp-admin-notices';
/**
* Returns the current messages.
@ -37,4 +38,40 @@ class Repository implements RepositoryInterface {
}
);
}
/**
* Adds a message to persist between page reloads.
*
* @param Message $message The message.
* @return void
*/
public function persist( Message $message ): void {
$persisted_notices = get_option( self::PERSISTED_NOTICES_OPTION ) ?: array();
$persisted_notices[] = $message->to_array();
update_option( self::PERSISTED_NOTICES_OPTION, $persisted_notices );
}
/**
* Adds a message to persist between page reloads.
*
* @return array|Message[]
*/
public function get_persisted_and_clear(): array {
$notices = array();
$persisted_data = get_option( self::PERSISTED_NOTICES_OPTION ) ?: array();
foreach ( $persisted_data as $notice_data ) {
$notices[] = new Message(
(string) ( $notice_data['message'] ?? '' ),
(string) ( $notice_data['type'] ?? '' ),
(bool) ( $notice_data['dismissable'] ?? true ),
(string) ( $notice_data['wrapper'] ?? '' )
);
}
update_option( self::PERSISTED_NOTICES_OPTION, array(), true );
return $notices;
}
}

File diff suppressed because it is too large Load diff

View file

@ -9,10 +9,16 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderTransient;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
/**
* Class ApiModule
@ -40,6 +46,56 @@ class ApiModule implements ModuleInterface {
WC()->session->set( 'ppcp_fees', $fees );
}
);
add_filter(
'ppcp_create_order_request_body_data',
function( array $data ) use ( $c ) {
foreach ( ( $data['purchase_units'] ?? array() ) as $purchase_unit_index => $purchase_unit ) {
foreach ( ( $purchase_unit['items'] ?? array() ) as $item_index => $item ) {
$data['purchase_units'][ $purchase_unit_index ]['items'][ $item_index ]['name'] =
apply_filters( 'woocommerce_paypal_payments_cart_line_item_name', $item['name'], $item['cart_item_key'] ?? null );
}
}
return $data;
}
);
add_action(
'woocommerce_paypal_payments_paypal_order_created',
function ( Order $order ) use ( $c ) {
$transient = $c->has( 'api.helper.order-transient' ) ? $c->get( 'api.helper.order-transient' ) : null;
if ( $transient instanceof OrderTransient ) {
$transient->on_order_created( $order );
}
},
10,
1
);
add_action(
'woocommerce_paypal_payments_woocommerce_order_created',
function ( WC_Order $wc_order, Order $order ) use ( $c ) {
$transient = $c->has( 'api.helper.order-transient' ) ? $c->get( 'api.helper.order-transient' ) : null;
if ( $transient instanceof OrderTransient ) {
$transient->on_woocommerce_order_created( $wc_order, $order );
}
},
10,
2
);
add_action(
'woocommerce_paypal_payments_clear_apm_product_status',
function () use ( $c ) {
$failure_registry = $c->has( 'api.helper.failure-registry' ) ? $c->get( 'api.helper.failure-registry' ) : null;
if ( $failure_registry instanceof FailureRegistry ) {
$failure_registry->clear_failures( FailureRegistry::SELLER_STATUS_KEY );
}
},
10,
2
);
}
/**

View file

@ -0,0 +1,49 @@
<?php
/**
* The client credentials.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Authentication
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
/**
* Class ClientCredentials
*/
class ClientCredentials {
/**
* The settings.
*
* @var Settings
*/
protected $settings;
/**
* ClientCredentials constructor.
*
* @param Settings $settings The settings.
*/
public function __construct( Settings $settings ) {
$this->settings = $settings;
}
/**
* Returns encoded client credentials.
*
* @return string
* @throws NotFoundException If setting does not found.
*/
public function credentials(): string {
$client_id = $this->settings->has( 'client_id' ) ? $this->settings->get( 'client_id' ) : '';
$client_secret = $this->settings->has( 'client_secret' ) ? $this->settings->get( 'client_secret' ) : '';
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
return 'Basic ' . base64_encode( $client_id . ':' . $client_secret );
}
}

View file

@ -0,0 +1,119 @@
<?php
/**
* Generates user ID token for payer.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Authentication
*/
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WP_Error;
/**
* Class SdkClientToken
*/
class SdkClientToken {
use RequestTrait;
const CACHE_KEY = 'sdk-client-token-key';
/**
* The host.
*
* @var string
*/
private $host;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* The client credentials.
*
* @var ClientCredentials
*/
private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/**
* SdkClientToken constructor.
*
* @param string $host The host.
* @param LoggerInterface $logger The logger.
* @param ClientCredentials $client_credentials The client credentials.
* @param Cache $cache The cache.
*/
public function __construct(
string $host,
LoggerInterface $logger,
ClientCredentials $client_credentials,
Cache $cache
) {
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
$this->cache = $cache;
}
/**
* Returns the client token for SDK `data-sdk-client-token`.
*
* @return string
*
* @throws PayPalApiException If the request fails.
* @throws RuntimeException If something unexpected happens.
*/
public function sdk_client_token(): string {
if ( $this->cache->has( self::CACHE_KEY ) ) {
return $this->cache->get( self::CACHE_KEY );
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$domain = wp_unslash( $_SERVER['HTTP_HOST'] ?? '' );
$domain = preg_replace( '/^www\./', '', $domain );
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=client_token&intent=sdk_init&domains[]=' . $domain;
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => $this->client_credentials->credentials(),
'Content-Type' => 'application/x-www-form-urlencoded',
),
);
$response = $this->request( $url, $args );
if ( $response instanceof WP_Error ) {
throw new RuntimeException( $response->get_error_message() );
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code ) {
throw new PayPalApiException( $json, $status_code );
}
$access_token = $json->access_token;
$expires_in = (int) $json->expires_in;
$this->cache->set( self::CACHE_KEY, $access_token, $expires_in );
return $access_token;
}
}

View file

@ -0,0 +1,103 @@
<?php
/**
* Generates user ID token for payer.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Authentication
*/
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WP_Error;
/**
* Class UserIdToken
*/
class UserIdToken {
use RequestTrait;
/**
* The host.
*
* @var string
*/
private $host;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* The client credentials.
*
* @var ClientCredentials
*/
private $client_credentials;
/**
* UserIdToken constructor.
*
* @param string $host The host.
* @param LoggerInterface $logger The logger.
* @param ClientCredentials $client_credentials The client credentials.
*/
public function __construct(
string $host,
LoggerInterface $logger,
ClientCredentials $client_credentials
) {
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
}
/**
* Returns `id_token` which uniquely identifies the payer.
*
* @param string $target_customer_id Vaulted customer id.
*
* @return string
*
* @throws PayPalApiException If the request fails.
* @throws RuntimeException If something unexpected happens.
*/
public function id_token( string $target_customer_id = '' ): string {
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
if ( $target_customer_id ) {
$url = add_query_arg(
array(
'target_customer_id' => $target_customer_id,
),
$url
);
}
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => $this->client_credentials->credentials(),
'Content-Type' => 'application/x-www-form-urlencoded',
),
);
$response = $this->request( $url, $args );
if ( $response instanceof WP_Error ) {
throw new RuntimeException( $response->get_error_message() );
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code ) {
throw new PayPalApiException( $json, $status_code );
}
return $json->id_token;
}
}

View file

@ -121,7 +121,7 @@ class BillingAgreementsEndpoint {
*/
public function reference_transaction_enabled(): bool {
try {
if ( get_transient( 'ppcp_reference_transaction_enabled' ) === true ) {
if ( wc_string_to_bool( get_transient( 'ppcp_reference_transaction_enabled' ) ) === true ) {
return true;
}
@ -135,7 +135,7 @@ class BillingAgreementsEndpoint {
);
} finally {
$this->is_request_logging_enabled = true;
set_transient( 'ppcp_reference_transaction_enabled', true, 3 * MONTH_IN_SECONDS );
set_transient( 'ppcp_reference_transaction_enabled', true, MONTH_IN_SECONDS );
}
return true;

View file

@ -54,8 +54,6 @@ class BillingPlans {
private $plan_factory;
/**
* The logger.
*
* The logger.
*
* @var LoggerInterface

View file

@ -83,13 +83,10 @@ class CatalogProducts {
*/
public function create( string $name, string $description ): Product {
$data = array(
'name' => $name,
'name' => $name,
'description' => $description ?: $name,
);
if ( $description ) {
$data['description'] = $description;
}
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/catalogs/products';
$args = array(

View file

@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
@ -27,7 +28,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\ErrorResponse;
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet;
use WP_Error;
@ -174,12 +175,15 @@ class OrderEndpoint {
/**
* Creates an order.
*
* @param PurchaseUnit[] $items The purchase unit items for the order.
* @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values.
* @param Payer|null $payer The payer off the order.
* @param PaymentToken|null $payment_token The payment token.
* @param string $paypal_request_id The paypal request id.
* @param string $user_action The user action.
* @param PurchaseUnit[] $items The purchase unit items for the order.
* @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values.
* @param Payer|null $payer The payer off the order.
* @param PaymentToken|null $payment_token The payment token.
* @param string $paypal_request_id The PayPal request id.
* @param string $user_action The user action.
* @param string $payment_method WC payment method.
* @param array $request_data Request data.
* @param PaymentSource|null $payment_source The payment source.
*
* @return Order
* @throws RuntimeException If the request fails.
@ -190,11 +194,14 @@ class OrderEndpoint {
Payer $payer = null,
PaymentToken $payment_token = null,
string $paypal_request_id = '',
string $user_action = ApplicationContext::USER_ACTION_CONTINUE
string $user_action = ApplicationContext::USER_ACTION_CONTINUE,
string $payment_method = '',
array $request_data = array(),
PaymentSource $payment_source = null
): Order {
$bearer = $this->bearer->bearer();
$data = array(
'intent' => ( $this->subscription_helper->cart_contains_subscription() || $this->subscription_helper->current_product_is_subscription() ) ? 'AUTHORIZE' : $this->intent,
'intent' => apply_filters( 'woocommerce_paypal_payments_order_intent', $this->intent ),
'purchase_units' => array_map(
static function ( PurchaseUnit $item ) use ( $shipping_preference ): array {
$data = $item->to_array();
@ -217,11 +224,16 @@ class OrderEndpoint {
if ( $payment_token ) {
$data['payment_source']['token'] = $payment_token->to_array();
}
if ( $payment_source ) {
$data['payment_source'] = array(
$payment_source->name() => $payment_source->properties(),
);
}
/**
* The filter can be used to modify the order creation request body data.
*/
$data = apply_filters( 'ppcp_create_order_request_body_data', $data );
$data = apply_filters( 'ppcp_create_order_request_body_data', $data, $payment_method, $request_data );
$url = trailingslashit( $this->host ) . 'v2/checkout/orders';
$args = array(
'method' => 'POST',
@ -260,27 +272,29 @@ class OrderEndpoint {
);
throw $error;
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 201 !== $status_code ) {
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
$error = new PayPalApiException(
$json,
$status_code
);
$this->logger->log(
'warning',
$this->logger->warning(
sprintf(
'Failed to create order. PayPal API response: %1$s',
$error->getMessage()
),
array(
'args' => $args,
'response' => $response,
)
);
throw $error;
}
$order = $this->order_factory->from_paypal_response( $json );
do_action( 'woocommerce_paypal_payments_paypal_order_created', $order );
return $order;
}

View file

@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\SellerStatusFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
/**
* Class PartnersEndpoint
@ -65,6 +66,13 @@ class PartnersEndpoint {
*/
private $merchant_id;
/**
* The failure registry.
*
* @var FailureRegistry
*/
private $failure_registry;
/**
* PartnersEndpoint constructor.
*
@ -74,6 +82,7 @@ class PartnersEndpoint {
* @param SellerStatusFactory $seller_status_factory The seller status factory.
* @param string $partner_id The partner ID.
* @param string $merchant_id The merchant ID.
* @param FailureRegistry $failure_registry The API failure registry.
*/
public function __construct(
string $host,
@ -81,7 +90,8 @@ class PartnersEndpoint {
LoggerInterface $logger,
SellerStatusFactory $seller_status_factory,
string $partner_id,
string $merchant_id
string $merchant_id,
FailureRegistry $failure_registry
) {
$this->host = $host;
$this->bearer = $bearer;
@ -89,6 +99,7 @@ class PartnersEndpoint {
$this->seller_status_factory = $seller_status_factory;
$this->partner_id = $partner_id;
$this->merchant_id = $merchant_id;
$this->failure_registry = $failure_registry;
}
/**
@ -140,9 +151,15 @@ class PartnersEndpoint {
'response' => $response,
)
);
// Register the failure on api failure registry.
$this->failure_registry->add_failure( FailureRegistry::SELLER_STATUS_KEY );
throw $error;
}
$this->failure_registry->clear_failures( FailureRegistry::SELLER_STATUS_KEY );
$status = $this->seller_status_factory->from_paypal_reponse( $json );
return $status;
}

View file

@ -112,7 +112,7 @@ class PayUponInvoiceOrderEndpoint {
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
'purchase_units' => array_map(
static function ( PurchaseUnit $item ): array {
return $item->to_array( false );
return $item->to_array( true, false );
},
$items
),
@ -166,8 +166,11 @@ class PayUponInvoiceOrderEndpoint {
throw new PayPalApiException( $json, $status_code );
}
$order = $this->order_factory->from_paypal_response( $json );
return $this->order_factory->from_paypal_response( $json );
do_action( 'woocommerce_paypal_payments_paypal_order_created', $order );
return $order;
}
/**

View file

@ -0,0 +1,155 @@
<?php
/**
* The Payment Method Tokens endpoint.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use stdClass;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
/**
* Class PaymentMethodTokensEndpoint
*/
class PaymentMethodTokensEndpoint {
use RequestTrait;
/**
* The host.
*
* @var string
*/
private $host;
/**
* The bearer.
*
* @var Bearer
*/
private $bearer;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* PaymentMethodTokensEndpoint constructor.
*
* @param string $host The host.
* @param Bearer $bearer The bearer.
* @param LoggerInterface $logger The logger.
*/
public function __construct( string $host, Bearer $bearer, LoggerInterface $logger ) {
$this->host = $host;
$this->bearer = $bearer;
$this->logger = $logger;
}
/**
* Creates a setup token.
*
* @param PaymentSource $payment_source The payment source.
*
* @return stdClass
*
* @throws RuntimeException When something when wrong with the request.
* @throws PayPalApiException When something when wrong setting up the token.
*/
public function setup_tokens( PaymentSource $payment_source ): stdClass {
$data = array(
'payment_source' => array(
$payment_source->name() => $payment_source->properties(),
),
);
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v3/vault/setup-tokens';
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
),
'body' => wp_json_encode( $data ),
);
$response = $this->request( $url, $args );
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
throw new RuntimeException( 'Not able to create setup token.' );
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
throw new PayPalApiException(
$json,
$status_code
);
}
return $json;
}
/**
* Creates a payment token for the given payment source.
*
* @param PaymentSource $payment_source The payment source.
*
* @return stdClass
*
* @throws RuntimeException When something when wrong with the request.
* @throws PayPalApiException When something when wrong setting up the token.
*/
public function create_payment_token( PaymentSource $payment_source ): stdClass {
$data = array(
'payment_source' => array(
$payment_source->name() => $payment_source->properties(),
),
);
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens';
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
),
'body' => wp_json_encode( $data ),
);
$response = $this->request( $url, $args );
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
throw new RuntimeException( 'Not able to create setup token.' );
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
throw new PayPalApiException(
$json,
$status_code
);
}
return $json;
}
}

View file

@ -0,0 +1,145 @@
<?php
/**
* Payment tokens version 3 endpoint.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WP_Error;
/**
* Class PaymentTokensEndpoint
*/
class PaymentTokensEndpoint {
use RequestTrait;
/**
* The bearer.
*
* @var Bearer
*/
private $bearer;
/**
* The host.
*
* @var string
*/
private $host;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* PaymentTokensEndpoint constructor.
*
* @param string $host The bearer.
* @param Bearer $bearer The bearer.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
string $host,
Bearer $bearer,
LoggerInterface $logger
) {
$this->host = $host;
$this->bearer = $bearer;
$this->logger = $logger;
}
/**
* Deletes a payment token with the given id.
*
* @param string $id Payment token id.
*
* @return void
*
* @throws RuntimeException When something went wrong with the request.
* @throws PayPalApiException When something went wrong deleting the payment token.
*/
public function delete( string $id ): void {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens/' . $id;
$args = array(
'method' => 'DELETE',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
),
);
$response = $this->request( $url, $args );
if ( $response instanceof WP_Error ) {
throw new RuntimeException( $response->get_error_message() );
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 204 !== $status_code ) {
throw new PayPalApiException( $json, $status_code );
}
}
/**
* Returns all payment tokens for the given customer.
*
* @param string $customer_id PayPal customer id.
* @return array
*
* @throws RuntimeException When something went wrong with the request.
* @throws PayPalApiException When something went wrong getting the payment tokens.
*/
public function payment_tokens_for_customer( string $customer_id ): array {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens?customer_id=' . $customer_id;
$args = array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
),
);
$response = $this->request( $url, $args );
if ( $response instanceof WP_Error ) {
throw new RuntimeException( $response->get_error_message() );
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code ) {
throw new PayPalApiException( $json, $status_code );
}
$tokens = array();
$payment_tokens = $json->payment_tokens ?? array();
foreach ( $payment_tokens as $payment_token ) {
$name = array_key_first( (array) $payment_token->payment_source ) ?? '';
if ( $name ) {
$tokens[] = array(
'id' => $payment_token->id,
'payment_source' => new PaymentSource(
$name,
$payment_token->payment_source->$name
),
);
}
}
return $tokens;
}
}

View file

@ -13,7 +13,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
use WooCommerce\PayPalCommerce\ApiClient\Entity\RefundCapture;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\AuthorizationFactory;
@ -193,16 +193,63 @@ class PaymentsEndpoint {
return $this->capture_factory->from_paypal_response( $json );
}
/**
* Reauthorizes an order.
*
* @param string $authorization_id The id.
* @param Money|null $amount The amount to capture. If not specified, the whole authorized amount is captured.
*
* @return string
* @throws RuntimeException If the request fails.
* @throws PayPalApiException If the request fails.
*/
public function reauthorize( string $authorization_id, ?Money $amount = null ) : string {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/payments/authorizations/' . $authorization_id . '/reauthorize';
$data = array();
if ( $amount ) {
$data['amount'] = $amount->to_array();
}
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation',
),
'body' => wp_json_encode( $data, JSON_FORCE_OBJECT ),
);
$response = $this->request( $url, $args );
$json = json_decode( $response['body'] );
if ( is_wp_error( $response ) ) {
throw new RuntimeException( 'Could not reauthorize authorized payment.' );
}
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 201 !== $status_code || ! is_object( $json ) ) {
throw new PayPalApiException(
$json,
$status_code
);
}
return $json->id;
}
/**
* Refunds a payment.
*
* @param Refund $refund The refund to be processed.
* @param RefundCapture $refund The refund to be processed.
*
* @return string Refund ID.
* @throws RuntimeException If the request fails.
* @throws PayPalApiException If the request fails.
*/
public function refund( Refund $refund ) : string {
public function refund( RefundCapture $refund ) : string {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/payments/captures/' . $refund->for_capture()->id() . '/refund';
$args = array(

View file

@ -42,7 +42,7 @@ trait RequestTrait {
*/
$args = apply_filters( 'ppcp_request_args', $args, $url );
if ( ! isset( $args['headers']['PayPal-Partner-Attribution-Id'] ) ) {
$args['headers']['PayPal-Partner-Attribution-Id'] = 'Woo_PPCP';
$args['headers']['PayPal-Partner-Attribution-Id'] = PPCP_PAYPAL_BN_CODE;
}
$response = wp_remote_get( $url, $args );

View file

@ -17,6 +17,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookEventFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookFactory;
use Psr\Log\LoggerInterface;
use WP_Error;
/**
* Class WebhookEndpoint
@ -193,7 +194,7 @@ class WebhookEndpoint {
);
$response = $this->request( $url, $args );
if ( is_wp_error( $response ) ) {
if ( $response instanceof WP_Error ) {
throw new RuntimeException(
__( 'Not able to delete the webhook.', 'woocommerce-paypal-payments' )
);
@ -201,7 +202,15 @@ class WebhookEndpoint {
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 204 !== $status_code ) {
$json = json_decode( $response['body'] );
$json = null;
/**
* Use in array as consistency check.
*
* @psalm-suppress RedundantConditionGivenDocblockType
*/
if ( is_array( $response ) ) {
$json = json_decode( $response['body'] );
}
throw new PayPalApiException(
$json,
$status_code

View file

@ -28,19 +28,29 @@ class Authorization {
*/
private $authorization_status;
/**
* The fraud processor response (AVS, CVV ...).
*
* @var FraudProcessorResponse|null
*/
protected $fraud_processor_response;
/**
* Authorization constructor.
*
* @param string $id The id.
* @param AuthorizationStatus $authorization_status The status.
* @param string $id The id.
* @param AuthorizationStatus $authorization_status The status.
* @param FraudProcessorResponse|null $fraud_processor_response The fraud processor response (AVS, CVV ...).
*/
public function __construct(
string $id,
AuthorizationStatus $authorization_status
AuthorizationStatus $authorization_status,
?FraudProcessorResponse $fraud_processor_response
) {
$this->id = $id;
$this->authorization_status = $authorization_status;
$this->id = $id;
$this->authorization_status = $authorization_status;
$this->fraud_processor_response = $fraud_processor_response;
}
/**
@ -71,15 +81,30 @@ class Authorization {
$this->authorization_status->is( AuthorizationStatus::PENDING );
}
/**
* Returns the fraud processor response (AVS, CVV ...).
*
* @return FraudProcessorResponse|null
*/
public function fraud_processor_response() : ?FraudProcessorResponse {
return $this->fraud_processor_response;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array(): array {
return array(
$data = array(
'id' => $this->id,
'status' => $this->authorization_status->name(),
);
if ( $this->fraud_processor_response ) {
$data['fraud_processor_response'] = $this->fraud_processor_response->to_array();
}
return $data;
}
}

View file

@ -14,7 +14,6 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
*/
class CardAuthenticationResult {
const LIABILITY_SHIFT_POSSIBLE = 'POSSIBLE';
const LIABILITY_SHIFT_NO = 'NO';
const LIABILITY_SHIFT_UNKNOWN = 'UNKNOWN';

View file

@ -66,6 +66,20 @@ class Item {
*/
private $category;
/**
* The product url.
*
* @var string
*/
protected $url;
/**
* The product image url.
*
* @var string
*/
protected $image_url;
/**
* The tax rate.
*
@ -90,6 +104,8 @@ class Item {
* @param Money|null $tax The tax.
* @param string $sku The SKU.
* @param string $category The category.
* @param string $url The product url.
* @param string $image_url The product image url.
* @param float $tax_rate The tax rate.
* @param ?string $cart_item_key The cart key for this item.
*/
@ -101,6 +117,8 @@ class Item {
Money $tax = null,
string $sku = '',
string $category = 'PHYSICAL_GOODS',
string $url = '',
string $image_url = '',
float $tax_rate = 0,
string $cart_item_key = null
) {
@ -111,8 +129,9 @@ class Item {
$this->description = $description;
$this->tax = $tax;
$this->sku = $sku;
$this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = $category;
$this->url = $url;
$this->image_url = $image_url;
$this->tax_rate = $tax_rate;
$this->cart_item_key = $cart_item_key;
}
@ -180,6 +199,24 @@ class Item {
return $this->category;
}
/**
* Returns the url.
*
* @return string
*/
public function url():string {
return $this->url;
}
/**
* Returns the image url.
*
* @return string
*/
public function image_url():string {
return $this->validate_image_url() ? $this->image_url : '';
}
/**
* Returns the tax rate.
*
@ -203,7 +240,7 @@ class Item {
*
* @return array
*/
public function to_array() {
public function to_array(): array {
$item = array(
'name' => $this->name(),
'unit_amount' => $this->unit_amount()->to_array(),
@ -211,8 +248,13 @@ class Item {
'description' => $this->description(),
'sku' => $this->sku(),
'category' => $this->category(),
'url' => $this->url(),
);
if ( $this->image_url() ) {
$item['image_url'] = $this->image_url();
}
if ( $this->tax() ) {
$item['tax'] = $this->tax()->to_array();
}
@ -227,4 +269,14 @@ class Item {
return $item;
}
/**
* Validates the image url for PayPal request.
*
* @return bool true if valid, otherwise false.
*/
protected function validate_image_url(): bool {
$pattern = '/^(https:)([\/|\.|\w|\s|-])*\.(?:jpg|gif|png|jpeg|JPG|GIF|PNG|JPEG)$/';
return (bool) preg_match( $pattern, $this->image_url );
}
}

View file

@ -107,14 +107,6 @@ class Order {
$this->id = $id;
$this->application_context = $application_context;
$this->purchase_units = array_values(
array_filter(
$purchase_units,
static function ( $unit ): bool {
return is_a( $unit, PurchaseUnit::class );
}
)
);
$this->payer = $payer;
$this->order_status = $order_status;
$this->intent = ( 'CAPTURE' === $intent ) ? 'CAPTURE' : 'AUTHORIZE';
@ -236,9 +228,6 @@ class Order {
if ( $this->application_context() ) {
$order['application_context'] = $this->application_context()->to_array();
}
if ( $this->payment_source() ) {
$order['payment_source'] = $this->payment_source()->to_array();
}
return $order;
}

View file

@ -15,14 +15,15 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
* Class OrderStatus
*/
class OrderStatus {
const INTERNAL = 'INTERNAL';
const CREATED = 'CREATED';
const SAVED = 'SAVED';
const APPROVED = 'APPROVED';
const VOIDED = 'VOIDED';
const COMPLETED = 'COMPLETED';
const PENDING_APPROVAL = 'PENDING_APPROVAL';
const VALID_STATUS = array(
const INTERNAL = 'INTERNAL';
const CREATED = 'CREATED';
const SAVED = 'SAVED';
const APPROVED = 'APPROVED';
const VOIDED = 'VOIDED';
const COMPLETED = 'COMPLETED';
const PENDING_APPROVAL = 'PENDING_APPROVAL';
const PAYER_ACTION_REQUIRED = 'PAYER_ACTION_REQUIRED';
const VALID_STATUS = array(
self::INTERNAL,
self::CREATED,
self::SAVED,
@ -30,6 +31,7 @@ class OrderStatus {
self::VOIDED,
self::COMPLETED,
self::PENDING_APPROVAL,
self::PAYER_ACTION_REQUIRED,
);
/**

View file

@ -9,74 +9,53 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
use stdClass;
/**
* Class PaymentSource
*/
class PaymentSource {
/**
* The card.
* Payment source name.
*
* @var PaymentSourceCard|null
* @var string
*/
private $card;
private $name;
/**
* The wallet.
* Payment source properties.
*
* @var PaymentSourceWallet|null
* @var object
*/
private $wallet;
private $properties;
/**
* PaymentSource constructor.
*
* @param PaymentSourceCard|null $card The card.
* @param PaymentSourceWallet|null $wallet The wallet.
* @param string $name Payment source name.
* @param object $properties Payment source properties.
*/
public function __construct(
PaymentSourceCard $card = null,
PaymentSourceWallet $wallet = null
) {
$this->card = $card;
$this->wallet = $wallet;
public function __construct( string $name, object $properties ) {
$this->name = $name;
$this->properties = $properties;
}
/**
* Returns the card.
* Payment source name.
*
* @return PaymentSourceCard|null
* @return string
*/
public function card() {
return $this->card;
public function name(): string {
return $this->name;
}
/**
* Returns the wallet.
* Payment source properties.
*
* @return PaymentSourceWallet|null
* @return object
*/
public function wallet() {
return $this->wallet;
}
/**
* Returns the array of the object.
*
* @return array
*/
public function to_array(): array {
$data = array();
if ( $this->card() ) {
$data['card'] = $this->card()->to_array();
}
if ( $this->wallet() ) {
$data['wallet'] = $this->wallet()->to_array();
}
return $data;
public function properties(): object {
return $this->properties;
}
}

View file

@ -1,123 +0,0 @@
<?php
/**
* The PaymentSourceCard object.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class PaymentSourceCard
*/
class PaymentSourceCard {
/**
* The last digits of the card.
*
* @var string
*/
private $last_digits;
/**
* The brand.
*
* @var string
*/
private $brand;
/**
* The type.
*
* @var string
*/
private $type;
/**
* The authentication result.
*
* @var CardAuthenticationResult|null
*/
private $authentication_result;
/**
* PaymentSourceCard constructor.
*
* @param string $last_digits The last digits of the card.
* @param string $brand The brand of the card.
* @param string $type The type of the card.
* @param CardAuthenticationResult|null $authentication_result The authentication result.
*/
public function __construct(
string $last_digits,
string $brand,
string $type,
CardAuthenticationResult $authentication_result = null
) {
$this->last_digits = $last_digits;
$this->brand = $brand;
$this->type = $type;
$this->authentication_result = $authentication_result;
}
/**
* Returns the last digits.
*
* @return string
*/
public function last_digits(): string {
return $this->last_digits;
}
/**
* Returns the brand.
*
* @return string
*/
public function brand(): string {
return $this->brand;
}
/**
* Returns the type.
*
* @return string
*/
public function type(): string {
return $this->type;
}
/**
* Returns the authentication result.
*
* @return CardAuthenticationResult|null
*/
public function authentication_result() {
return $this->authentication_result;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array(): array {
$data = array(
'last_digits' => $this->last_digits(),
'brand' => $this->brand(),
'type' => $this->type(),
);
if ( $this->authentication_result() ) {
$data['authentication_result'] = $this->authentication_result()->to_array();
}
return $data;
}
}

View file

@ -1,25 +0,0 @@
<?php
/**
* The PaymentSourcewallet.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class PaymentSourceWallet
*/
class PaymentSourceWallet {
/**
* Returns the object as array.
*
* @return array
*/
public function to_array(): array {
return array();
}
}

View file

@ -28,13 +28,21 @@ class Payments {
*/
private $captures;
/**
* The Refunds.
*
* @var Refund[]
*/
private $refunds;
/**
* Payments constructor.
*
* @param array $authorizations The Authorizations.
* @param array $captures The Captures.
* @param array $refunds The Refunds.
*/
public function __construct( array $authorizations, array $captures ) {
public function __construct( array $authorizations, array $captures, array $refunds = array() ) {
foreach ( $authorizations as $key => $authorization ) {
if ( is_a( $authorization, Authorization::class ) ) {
continue;
@ -47,8 +55,15 @@ class Payments {
}
unset( $captures[ $key ] );
}
foreach ( $refunds as $key => $refund ) {
if ( is_a( $refund, Refund::class ) ) {
continue;
}
unset( $refunds[ $key ] );
}
$this->authorizations = $authorizations;
$this->captures = $captures;
$this->refunds = $refunds;
}
/**
@ -70,6 +85,12 @@ class Payments {
},
$this->captures()
),
'refunds' => array_map(
static function ( Refund $refund ): array {
return $refund->to_array();
},
$this->refunds()
),
);
}
@ -90,4 +111,13 @@ class Payments {
public function captures(): array {
return $this->captures;
}
/**
* Returns the Refunds.
*
* @return Refund[]
**/
public function refunds(): array {
return $this->refunds;
}
}

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer;
/**
* Class PurchaseUnit
*/
@ -49,13 +51,6 @@ class PurchaseUnit {
*/
private $description;
/**
* The Payee.
*
* @var Payee|null
*/
private $payee;
/**
* The custom id.
*
@ -91,6 +86,13 @@ class PurchaseUnit {
*/
private $contains_physical_goods = false;
/**
* The sanitizer for this purchase unit output.
*
* @var PurchaseUnitSanitizer|null
*/
private $sanitizer;
/**
* PurchaseUnit constructor.
*
@ -99,7 +101,6 @@ class PurchaseUnit {
* @param Shipping|null $shipping The Shipping.
* @param string $reference_id The reference ID.
* @param string $description The description.
* @param Payee|null $payee The Payee.
* @param string $custom_id The custom ID.
* @param string $invoice_id The invoice ID.
* @param string $soft_descriptor The soft descriptor.
@ -111,7 +112,6 @@ class PurchaseUnit {
Shipping $shipping = null,
string $reference_id = 'default',
string $description = '',
Payee $payee = null,
string $custom_id = '',
string $invoice_id = '',
string $soft_descriptor = '',
@ -141,7 +141,6 @@ class PurchaseUnit {
}
)
);
$this->payee = $payee;
$this->custom_id = $custom_id;
$this->invoice_id = $invoice_id;
$this->soft_descriptor = $soft_descriptor;
@ -220,6 +219,16 @@ class PurchaseUnit {
$this->custom_id = $custom_id;
}
/**
* Sets the sanitizer for this purchase unit output.
*
* @param PurchaseUnitSanitizer|null $sanitizer The sanitizer.
* @return void
*/
public function set_sanitizer( ?PurchaseUnitSanitizer $sanitizer ) {
$this->sanitizer = $sanitizer;
}
/**
* Returns the invoice id.
*
@ -238,15 +247,6 @@ class PurchaseUnit {
return $this->soft_descriptor;
}
/**
* Returns the Payee.
*
* @return Payee|null
*/
public function payee() {
return $this->payee;
}
/**
* Returns the Payments.
*
@ -277,11 +277,12 @@ class PurchaseUnit {
/**
* Returns the object as array.
*
* @param bool $ditch_items_when_mismatch Whether ditch items when mismatch or not.
* @param bool $sanitize_output Whether output should be sanitized for PayPal consumption.
* @param bool $allow_ditch_items Whether to allow items to be ditched.
*
* @return array
*/
public function to_array( bool $ditch_items_when_mismatch = true ): array {
public function to_array( bool $sanitize_output = true, bool $allow_ditch_items = true ): array {
$purchase_unit = array(
'reference_id' => $this->reference_id(),
'amount' => $this->amount()->to_array(),
@ -294,21 +295,6 @@ class PurchaseUnit {
),
);
$ditch = $ditch_items_when_mismatch && $this->ditch_items_when_mismatch( $this->amount(), ...$this->items() );
/**
* The filter can be used to control when the items and totals breakdown are removed from PayPal order info.
*/
$ditch = apply_filters( 'ppcp_ditch_items_breakdown', $ditch, $this );
if ( $ditch ) {
unset( $purchase_unit['items'] );
unset( $purchase_unit['amount']['breakdown'] );
}
if ( $this->payee() ) {
$purchase_unit['payee'] = $this->payee()->to_array();
}
if ( $this->payments() ) {
$purchase_unit['payments'] = $this->payments()->to_array();
}
@ -325,101 +311,45 @@ class PurchaseUnit {
if ( $this->soft_descriptor() ) {
$purchase_unit['soft_descriptor'] = $this->soft_descriptor();
}
return $purchase_unit;
$has_ditched_items_breakdown = false;
if ( $sanitize_output && isset( $this->sanitizer ) ) {
$purchase_unit = ( $this->sanitizer->sanitize( $purchase_unit, $allow_ditch_items ) );
$has_ditched_items_breakdown = $this->sanitizer->has_ditched_items_breakdown();
}
return $this->apply_ditch_items_mismatch_filter(
$has_ditched_items_breakdown,
$purchase_unit
);
}
/**
* All money values send to PayPal can only have 2 decimal points. WooCommerce internally does
* not have this restriction. Therefore the totals of the cart in WooCommerce and the totals
* of the rounded money values of the items, we send to PayPal, can differ. In those cases,
* we can not send the line items.
* Applies the ppcp_ditch_items_breakdown filter.
* If true purchase_unit items and breakdown are ditched from PayPal.
*
* @param Amount $amount The amount.
* @param Item ...$items The items.
* @return bool
* @param bool $ditched_items_breakdown If the breakdown and items were already ditched.
* @param array $purchase_unit The purchase_unit array.
* @return array
*/
private function ditch_items_when_mismatch( Amount $amount, Item ...$items ): bool {
$breakdown = $amount->breakdown();
if ( ! $breakdown ) {
return false;
}
public function apply_ditch_items_mismatch_filter( bool $ditched_items_breakdown, array $purchase_unit ): array {
/**
* The filter can be used to control when the items and totals breakdown are removed from PayPal order info.
*/
$ditch = apply_filters( 'ppcp_ditch_items_breakdown', $ditched_items_breakdown, $this );
$item_total = $breakdown->item_total();
if ( $item_total ) {
$remaining_item_total = array_reduce(
$items,
function ( float $total, Item $item ): float {
return $total - (float) $item->unit_amount()->value_str() * (float) $item->quantity();
},
(float) $item_total->value_str()
);
if ( $ditch ) {
unset( $purchase_unit['items'] );
unset( $purchase_unit['amount']['breakdown'] );
$remaining_item_total = round( $remaining_item_total, 2 );
if ( 0.0 !== $remaining_item_total ) {
return true;
if ( isset( $this->sanitizer ) && ( $ditch !== $ditched_items_breakdown ) ) {
$this->sanitizer->set_last_message(
__( 'Ditch items breakdown filter. Items and breakdown ditched.', 'woocommerce-paypal-payments' )
);
}
}
$tax_total = $breakdown->tax_total();
$items_with_tax = array_filter(
$this->items,
function ( Item $item ): bool {
return null !== $item->tax();
}
);
if ( $tax_total && ! empty( $items_with_tax ) ) {
$remaining_tax_total = array_reduce(
$items,
function ( float $total, Item $item ): float {
$tax = $item->tax();
if ( $tax ) {
$total -= (float) $tax->value_str() * (float) $item->quantity();
}
return $total;
},
(float) $tax_total->value_str()
);
$remaining_tax_total = round( $remaining_tax_total, 2 );
if ( 0.0 !== $remaining_tax_total ) {
return true;
}
}
$shipping = $breakdown->shipping();
$discount = $breakdown->discount();
$shipping_discount = $breakdown->shipping_discount();
$handling = $breakdown->handling();
$insurance = $breakdown->insurance();
$amount_total = 0.0;
if ( $shipping ) {
$amount_total += (float) $shipping->value_str();
}
if ( $item_total ) {
$amount_total += (float) $item_total->value_str();
}
if ( $discount ) {
$amount_total -= (float) $discount->value_str();
}
if ( $tax_total ) {
$amount_total += (float) $tax_total->value_str();
}
if ( $shipping_discount ) {
$amount_total -= (float) $shipping_discount->value_str();
}
if ( $handling ) {
$amount_total += (float) $handling->value_str();
}
if ( $insurance ) {
$amount_total += (float) $insurance->value_str();
}
$amount_str = $amount->value_str();
$amount_total_str = ( new Money( $amount_total, $amount->currency_code() ) )->value_str();
$needs_to_ditch = $amount_str !== $amount_total_str;
return $needs_to_ditch;
return $purchase_unit;
}
}

View file

@ -1,6 +1,8 @@
<?php
/**
* The refund object.
* The refund entity.
*
* @link https://developer.paypal.com/docs/api/orders/v2/#definition-refund
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
@ -15,11 +17,32 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
class Refund {
/**
* The Capture.
* The ID.
*
* @var Capture
* @var string
*/
private $capture;
private $id;
/**
* The status.
*
* @var RefundStatus
*/
private $status;
/**
* The amount.
*
* @var Amount
*/
private $amount;
/**
* The detailed breakdown of the refund activity (fees, ...).
*
* @var SellerPayableBreakdown|null
*/
private $seller_payable_breakdown;
/**
* The invoice id.
@ -29,50 +52,97 @@ class Refund {
private $invoice_id;
/**
* The note to the payer.
* The custom id.
*
* @var string
*/
private $custom_id;
/**
* The acquirer reference number.
*
* @var string
*/
private $acquirer_reference_number;
/**
* The acquirer reference number.
*
* @var string
*/
private $note_to_payer;
/**
* The Amount.
* The payer of the refund.
*
* @var Amount|null
* @var ?RefundPayer
*/
private $amount;
private $payer;
/**
* Refund constructor.
*
* @param Capture $capture The capture where the refund is supposed to be applied at.
* @param string $invoice_id The invoice id.
* @param string $note_to_payer The note to the payer.
* @param Amount|null $amount The Amount.
* @param string $id The ID.
* @param RefundStatus $status The status.
* @param Amount $amount The amount.
* @param string $invoice_id The invoice id.
* @param string $custom_id The custom id.
* @param SellerPayableBreakdown|null $seller_payable_breakdown The detailed breakdown of the refund activity (fees, ...).
* @param string $acquirer_reference_number The acquirer reference number.
* @param string $note_to_payer The note to payer.
* @param RefundPayer|null $payer The payer.
*/
public function __construct(
Capture $capture,
string $id,
RefundStatus $status,
Amount $amount,
string $invoice_id,
string $note_to_payer = '',
Amount $amount = null
string $custom_id,
?SellerPayableBreakdown $seller_payable_breakdown,
string $acquirer_reference_number,
string $note_to_payer,
?RefundPayer $payer
) {
$this->capture = $capture;
$this->invoice_id = $invoice_id;
$this->note_to_payer = $note_to_payer;
$this->amount = $amount;
$this->id = $id;
$this->status = $status;
$this->amount = $amount;
$this->invoice_id = $invoice_id;
$this->custom_id = $custom_id;
$this->seller_payable_breakdown = $seller_payable_breakdown;
$this->acquirer_reference_number = $acquirer_reference_number;
$this->note_to_payer = $note_to_payer;
$this->payer = $payer;
}
/**
* Returns the capture for the refund.
* Returns the ID.
*
* @return Capture
* @return string
*/
public function for_capture() : Capture {
return $this->capture;
public function id() : string {
return $this->id;
}
/**
* Return the invoice id.
* Returns the status.
*
* @return RefundStatus
*/
public function status() : RefundStatus {
return $this->status;
}
/**
* Returns the amount.
*
* @return Amount
*/
public function amount() : Amount {
return $this->amount;
}
/**
* Returns the invoice id.
*
* @return string
*/
@ -81,7 +151,34 @@ class Refund {
}
/**
* Returns the note to the payer.
* Returns the custom id.
*
* @return string
*/
public function custom_id() : string {
return $this->custom_id;
}
/**
* Returns the detailed breakdown of the refund activity (fees, ...).
*
* @return SellerPayableBreakdown|null
*/
public function seller_payable_breakdown() : ?SellerPayableBreakdown {
return $this->seller_payable_breakdown;
}
/**
* The acquirer reference number.
*
* @return string
*/
public function acquirer_reference_number() : string {
return $this->acquirer_reference_number;
}
/**
* The note to payer.
*
* @return string
*/
@ -90,28 +187,38 @@ class Refund {
}
/**
* Returns the Amount.
* Returns the refund payer.
*
* @return Amount|null
* @return RefundPayer|null
*/
public function amount() {
return $this->amount;
public function payer() : ?RefundPayer {
return $this->payer;
}
/**
* Returns the object as array.
* Returns the entity as array.
*
* @return array
*/
public function to_array() : array {
$data = array(
'invoice_id' => $this->invoice_id(),
$data = array(
'id' => $this->id(),
'status' => $this->status()->name(),
'amount' => $this->amount()->to_array(),
'invoice_id' => $this->invoice_id(),
'custom_id' => $this->custom_id(),
'acquirer_reference_number' => $this->acquirer_reference_number(),
'note_to_payer' => (array) $this->note_to_payer(),
);
if ( $this->note_to_payer() ) {
$data['note_to_payer'] = $this->note_to_payer();
$details = $this->status()->details();
if ( $details ) {
$data['status_details'] = array( 'reason' => $details->reason() );
}
if ( $this->amount() ) {
$data['amount'] = $this->amount()->to_array();
if ( $this->seller_payable_breakdown ) {
$data['seller_payable_breakdown'] = $this->seller_payable_breakdown->to_array();
}
if ( $this->payer ) {
$data['payer'] = $this->payer->to_array();
}
return $data;
}

View file

@ -0,0 +1,118 @@
<?php
/**
* The refund capture object.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class RefundCapture
*/
class RefundCapture {
/**
* The Capture.
*
* @var Capture
*/
private $capture;
/**
* The invoice id.
*
* @var string
*/
private $invoice_id;
/**
* The note to the payer.
*
* @var string
*/
private $note_to_payer;
/**
* The Amount.
*
* @var Amount|null
*/
private $amount;
/**
* Refund constructor.
*
* @param Capture $capture The capture where the refund is supposed to be applied at.
* @param string $invoice_id The invoice id.
* @param string $note_to_payer The note to the payer.
* @param Amount|null $amount The Amount.
*/
public function __construct(
Capture $capture,
string $invoice_id,
string $note_to_payer = '',
Amount $amount = null
) {
$this->capture = $capture;
$this->invoice_id = $invoice_id;
$this->note_to_payer = $note_to_payer;
$this->amount = $amount;
}
/**
* Returns the capture for the refund.
*
* @return Capture
*/
public function for_capture() : Capture {
return $this->capture;
}
/**
* Return the invoice id.
*
* @return string
*/
public function invoice_id() : string {
return $this->invoice_id;
}
/**
* Returns the note to the payer.
*
* @return string
*/
public function note_to_payer() : string {
return $this->note_to_payer;
}
/**
* Returns the Amount.
*
* @return Amount|null
*/
public function amount() {
return $this->amount;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array() : array {
$data = array(
'invoice_id' => $this->invoice_id(),
);
if ( $this->note_to_payer() ) {
$data['note_to_payer'] = $this->note_to_payer();
}
if ( $this->amount ) {
$data['amount'] = $this->amount->to_array();
}
return $data;
}
}

View file

@ -0,0 +1,79 @@
<?php
/**
* The refund payer object.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class RefundPayer
* The customer who sends the money.
*/
class RefundPayer {
/**
* The email address.
*
* @var string
*/
private $email_address;
/**
* The merchant id.
*
* @var string
*/
private $merchant_id;
/**
* RefundPayer constructor.
*
* @param string $email_address The email.
* @param string $merchant_id The merchant id.
*/
public function __construct(
string $email_address,
string $merchant_id
) {
$this->email_address = $email_address;
$this->merchant_id = $merchant_id;
}
/**
* Returns the email address.
*
* @return string
*/
public function email_address(): string {
return $this->email_address;
}
/**
* Returns the merchant id.
*
* @return string
*/
public function merchant_id(): string {
return $this->merchant_id;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array() {
$payer = array(
'email_address' => $this->email_address(),
);
if ( $this->merchant_id ) {
$payer['merchant_id'] = $this->merchant_id();
}
return $payer;
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* The RefundStatus object.
*
* @see https://developer.paypal.com/docs/api/orders/v2/#definition-refund_status
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class RefundStatus
*/
class RefundStatus {
const COMPLETED = 'COMPLETED';
const CANCELLED = 'CANCELLED';
const FAILED = 'FAILED';
const PENDING = 'PENDING';
/**
* The status.
*
* @var string
*/
private $status;
/**
* The details.
*
* @var RefundStatusDetails|null
*/
private $details;
/**
* RefundStatus constructor.
*
* @param string $status The status.
* @param RefundStatusDetails|null $details The details.
*/
public function __construct( string $status, ?RefundStatusDetails $details = null ) {
$this->status = $status;
$this->details = $details;
}
/**
* Compares the current status with a given one.
*
* @param string $status The status to compare with.
*
* @return bool
*/
public function is( string $status ): bool {
return $this->status === $status;
}
/**
* Returns the status.
*
* @return string
*/
public function name(): string {
return $this->status;
}
/**
* Returns the details.
*
* @return RefundStatusDetails|null
*/
public function details(): ?RefundStatusDetails {
return $this->details;
}
}

View file

@ -0,0 +1,71 @@
<?php
/**
* The RefundStatusDetails object.
*
* @see https://developer.paypal.com/docs/api/payments/v2/#definition-refund_status_details
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class RefundStatusDetails
*/
class RefundStatusDetails {
const ECHECK = 'ECHECK';
/**
* The reason.
*
* @var string
*/
private $reason;
/**
* RefundStatusDetails constructor.
*
* @param string $reason The reason explaining refund status.
*/
public function __construct( string $reason ) {
$this->reason = $reason;
}
/**
* Compares the current reason with a given one.
*
* @param string $reason The reason to compare with.
*
* @return bool
*/
public function is( string $reason ): bool {
return $this->reason === $reason;
}
/**
* Returns the reason explaining refund status.
* One of RefundStatusDetails constants.
*
* @return string
*/
public function reason(): string {
return $this->reason;
}
/**
* Returns the human-readable reason text explaining refund status.
*
* @return string
*/
public function text(): string {
switch ( $this->reason ) {
case self::ECHECK:
return __( 'The payer paid by an eCheck that has not yet cleared.', 'woocommerce-paypal-payments' );
default:
return $this->reason;
}
}
}

View file

@ -0,0 +1,202 @@
<?php
/**
* The info about fees and amount that will be paid by the seller.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class SellerPayableBreakdown
*/
class SellerPayableBreakdown {
/**
* The amount for this refunded payment in the currency of the transaction.
*
* @var Money|null
*/
private $gross_amount;
/**
* The applicable fee for this refunded payment in the currency of the transaction.
*
* @var Money|null
*/
private $paypal_fee;
/**
* The applicable fee for this captured payment in the receivable currency.
*
* Present only in cases the fee is charged in the receivable currency.
*
* @var Money|null
*/
private $paypal_fee_in_receivable_currency;
/**
* The net amount that the payee receives for this refunded payment in their PayPal account.
*
* Computed as gross_amount minus the paypal_fee minus the platform_fees.
*
* @var Money|null
*/
private $net_amount;
/**
* The net amount for this refunded payment in the receivable currency.
*
* @var Money|null
*/
private $net_amount_in_receivable_currency;
/**
* The total amount for this refund.
*
* @var Money|null
*/
private $total_refunded_amount;
/**
* An array of platform or partner fees, commissions, or brokerage fees that associated with the captured payment.
*
* @var PlatformFee[]
*/
private $platform_fees;
/**
* SellerPayableBreakdown constructor.
*
* @param Money|null $gross_amount The amount for this refunded payment in the currency of the transaction.
* @param Money|null $paypal_fee The applicable fee for this refunded payment in the currency of the transaction.
* @param Money|null $paypal_fee_in_receivable_currency The applicable fee for this refunded payment in the receivable currency.
* @param Money|null $net_amount The net amount that the payee receives for this refunded payment in their PayPal account.
* @param Money|null $net_amount_in_receivable_currency The net amount for this refunded payment in the receivable currency.
* @param Money|null $total_refunded_amount The total amount for this refund.
* @param PlatformFee[] $platform_fees An array of platform or partner fees, commissions, or brokerage fees that associated with the captured payment.
*/
public function __construct(
?Money $gross_amount,
?Money $paypal_fee,
?Money $paypal_fee_in_receivable_currency,
?Money $net_amount,
?Money $net_amount_in_receivable_currency,
?Money $total_refunded_amount,
array $platform_fees
) {
$this->gross_amount = $gross_amount;
$this->paypal_fee = $paypal_fee;
$this->paypal_fee_in_receivable_currency = $paypal_fee_in_receivable_currency;
$this->net_amount = $net_amount;
$this->net_amount_in_receivable_currency = $net_amount_in_receivable_currency;
$this->total_refunded_amount = $total_refunded_amount;
$this->platform_fees = $platform_fees;
}
/**
* The amount for this refunded payment in the currency of the transaction.
*
* @return Money|null
*/
public function gross_amount(): ?Money {
return $this->gross_amount;
}
/**
* The applicable fee for this refunded payment in the currency of the transaction.
*
* @return Money|null
*/
public function paypal_fee(): ?Money {
return $this->paypal_fee;
}
/**
* The applicable fee for this refunded payment in the receivable currency.
*
* Present only in cases the fee is charged in the receivable currency.
*
* @return Money|null
*/
public function paypal_fee_in_receivable_currency(): ?Money {
return $this->paypal_fee_in_receivable_currency;
}
/**
* The net amount that the payee receives for this refunded payment in their PayPal account.
*
* Computed as gross_amount minus the paypal_fee minus the platform_fees.
*
* @return Money|null
*/
public function net_amount(): ?Money {
return $this->net_amount;
}
/**
* The net amount for this refunded payment in the receivable currency.
*
* @return Money|null
*/
public function net_amount_in_receivable_currency(): ?Money {
return $this->net_amount_in_receivable_currency;
}
/**
* The total amount for this refund.
*
* @return Money|null
*/
public function total_refunded_amount(): ?Money {
return $this->total_refunded_amount;
}
/**
* An array of platform or partner fees, commissions, or brokerage fees that associated with the refunded payment.
*
* @return PlatformFee[]
*/
public function platform_fees(): array {
return $this->platform_fees;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array(): array {
$data = array();
if ( $this->gross_amount ) {
$data['gross_amount'] = $this->gross_amount->to_array();
}
if ( $this->paypal_fee ) {
$data['paypal_fee'] = $this->paypal_fee->to_array();
}
if ( $this->paypal_fee_in_receivable_currency ) {
$data['paypal_fee_in_receivable_currency'] = $this->paypal_fee_in_receivable_currency->to_array();
}
if ( $this->net_amount ) {
$data['net_amount'] = $this->net_amount->to_array();
}
if ( $this->net_amount_in_receivable_currency ) {
$data['net_amount_in_receivable_currency'] = $this->net_amount_in_receivable_currency->to_array();
}
if ( $this->total_refunded_amount ) {
$data['total_refunded_amount'] = $this->total_refunded_amount->to_array();
}
if ( $this->platform_fees ) {
$data['platform_fees'] = array_map(
function ( PlatformFee $fee ) {
return $fee->to_array();
},
$this->platform_fees
);
}
return $data;
}
}

View file

@ -21,19 +21,37 @@ class SellerStatus {
*/
private $products;
/**
* The capabilities.
*
* @var SellerStatusCapability[]
*/
private $capabilities;
/**
* SellerStatus constructor.
*
* @param SellerStatusProduct[] $products The products.
* @param SellerStatusProduct[] $products The products.
* @param SellerStatusCapability[] $capabilities The capabilities.
*
* @psalm-suppress RedundantConditionGivenDocblockType
*/
public function __construct( array $products ) {
public function __construct( array $products, array $capabilities ) {
foreach ( $products as $key => $product ) {
if ( is_a( $product, SellerStatusProduct::class ) ) {
continue;
}
unset( $products[ $key ] );
}
$this->products = $products;
foreach ( $capabilities as $key => $capability ) {
if ( is_a( $capability, SellerStatusCapability::class ) ) {
continue;
}
unset( $capabilities[ $key ] );
}
$this->products = $products;
$this->capabilities = $capabilities;
}
/**
@ -45,6 +63,15 @@ class SellerStatus {
return $this->products;
}
/**
* Returns the capabilities.
*
* @return SellerStatusCapability[]
*/
public function capabilities() : array {
return $this->capabilities;
}
/**
* Returns the enitity as array.
*
@ -58,8 +85,16 @@ class SellerStatus {
$this->products()
);
$capabilities = array_map(
function( SellerStatusCapability $capability ) : array {
return $capability->to_array();
},
$this->capabilities()
);
return array(
'products' => $products,
'products' => $products,
'capabilities' => $capabilities,
);
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* The capabilities of a seller status.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class SellerStatusCapability
*/
class SellerStatusCapability {
const STATUS_ACTIVE = 'ACTIVE';
/**
* The name of the product.
*
* @var string
*/
private $name;
/**
* The status of the capability.
*
* @var string
*/
private $status;
/**
* SellerStatusCapability constructor.
*
* @param string $name The name of the product.
* @param string $status The status of the capability.
*/
public function __construct(
string $name,
string $status
) {
$this->name = $name;
$this->status = $status;
}
/**
* Returns the name of the product.
*
* @return string
*/
public function name() : string {
return $this->name;
}
/**
* Returns the status for this capability.
*
* @return string
*/
public function status() : string {
return $this->status;
}
/**
* Returns the entity as array.
*
* @return array
*/
public function to_array() : array {
return array(
'name' => $this->name(),
'status' => $this->status(),
);
}
}

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Exception;
use stdClass;
/**
* Class PayPalApiException
*/
@ -17,7 +19,7 @@ class PayPalApiException extends RuntimeException {
/**
* The JSON response object of PayPal.
*
* @var \stdClass
* @var stdClass
*/
private $response;
@ -31,10 +33,10 @@ class PayPalApiException extends RuntimeException {
/**
* PayPalApiException constructor.
*
* @param \stdClass|null $response The JSON object.
* @param int $status_code The HTTP status code.
* @param stdClass|null $response The JSON object.
* @param int $status_code The HTTP status code.
*/
public function __construct( \stdClass $response = null, int $status_code = 0 ) {
public function __construct( stdClass $response = null, int $status_code = 0 ) {
if ( is_null( $response ) ) {
$response = new \stdClass();
}
@ -65,7 +67,7 @@ class PayPalApiException extends RuntimeException {
*/
$this->response = $response;
$this->status_code = $status_code;
$message = $response->message;
$message = $this->get_customer_friendly_message( $response );
if ( $response->name ) {
$message = '[' . $response->name . '] ' . $message;
}
@ -141,4 +143,40 @@ class PayPalApiException extends RuntimeException {
return $details;
}
/**
* Returns a friendly message if the error detail is known.
*
* @param stdClass $json The response.
* @return string
*/
public function get_customer_friendly_message( stdClass $json ): string {
if ( empty( $json->details ) ) {
return $json->message;
}
$improved_keys_messages = array(
'PAYMENT_DENIED' => __( 'PayPal rejected the payment. Please reach out to the PayPal support for more information.', 'woocommerce-paypal-payments' ),
'TRANSACTION_REFUSED' => __( 'The transaction has been refused by the payment processor. Please reach out to the PayPal support for more information.', 'woocommerce-paypal-payments' ),
'DUPLICATE_INVOICE_ID' => __( 'The transaction has been refused because the Invoice ID already exists. Please create a new order or reach out to the store owner.', 'woocommerce-paypal-payments' ),
'PAYER_CANNOT_PAY' => __( 'There was a problem processing this transaction. Please reach out to the store owner.', 'woocommerce-paypal-payments' ),
'PAYEE_ACCOUNT_RESTRICTED' => __( 'There was a problem processing this transaction. Please reach out to the store owner.', 'woocommerce-paypal-payments' ),
'AGREEMENT_ALREADY_CANCELLED' => __( 'The requested agreement is already canceled. Please reach out to the PayPal support for more information.', 'woocommerce-paypal-payments' ),
);
$improved_errors = array_filter(
array_keys( $improved_keys_messages ),
function ( $key ) use ( $json ): bool {
foreach ( $json->details as $detail ) {
if ( isset( $detail->issue ) && $detail->issue === $key ) {
return true;
}
}
return false;
}
);
if ( $improved_errors ) {
$improved_errors = array_values( $improved_errors );
return $improved_keys_messages[ $improved_errors[0] ];
}
return $json->message;
}
}

View file

@ -14,7 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\AmountBreakdown;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcSubscriptions\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;

View file

@ -19,6 +19,22 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
*/
class AuthorizationFactory {
/**
* The FraudProcessorResponseFactory factory.
*
* @var FraudProcessorResponseFactory
*/
protected $fraud_processor_response_factory;
/**
* AuthorizationFactory constructor.
*
* @param FraudProcessorResponseFactory $fraud_processor_response_factory The FraudProcessorResponseFactory factory.
*/
public function __construct( FraudProcessorResponseFactory $fraud_processor_response_factory ) {
$this->fraud_processor_response_factory = $fraud_processor_response_factory;
}
/**
* Returns an Authorization based off a PayPal response.
*
@ -42,12 +58,17 @@ class AuthorizationFactory {
$reason = $data->status_details->reason ?? null;
$fraud_processor_response = isset( $data->processor_response ) ?
$this->fraud_processor_response_factory->from_paypal_response( $data->processor_response )
: null;
return new Authorization(
$data->id,
new AuthorizationStatus(
$data->status,
$reason ? new AuthorizationStatusDetails( $reason ) : null
)
),
$fraud_processor_response
);
}
}

View file

@ -0,0 +1,33 @@
<?php
/**
* The card authentication result factory.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use stdClass;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult;
/**
* Class CardAuthenticationResultFactory
*/
class CardAuthenticationResultFactory {
/**
* Returns a card authentication result from the given response object.
*
* @param stdClass $authentication_result The authentication result object.
* @return CardAuthenticationResult
*/
public function from_paypal_response( stdClass $authentication_result ): CardAuthenticationResult {
return new CardAuthenticationResult(
$authentication_result->liability_shift ?? '',
$authentication_result->three_d_secure->enrollment_status ?? '',
$authentication_result->three_d_secure->authentication_status ?? ''
);
}
}

View file

@ -25,8 +25,8 @@ class FraudProcessorResponseFactory {
* @return FraudProcessorResponse
*/
public function from_paypal_response( stdClass $data ): FraudProcessorResponse {
$avs_code = $data->avs_code ?: null;
$cvv_code = $data->cvv_code ?: null;
$avs_code = ( $data->avs_code ?? null ) ?: null;
$cvv_code = ( $data->cvv_code ?? null ) ?: null;
return new FraudProcessorResponse( $avs_code, $cvv_code );
}

View file

@ -13,11 +13,15 @@ use WC_Product;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\ItemTrait;
/**
* Class ItemFactory
*/
class ItemFactory {
use ItemTrait;
/**
* 3-letter currency code of the shop.
*
@ -53,16 +57,19 @@ class ItemFactory {
* @var \WC_Product $product
*/
$quantity = (int) $item['quantity'];
$image = wp_get_attachment_image_src( (int) $product->get_image_id(), 'full' );
$price = (float) $item['line_subtotal'] / (float) $item['quantity'];
return new Item(
mb_substr( $product->get_name(), 0, 127 ),
$this->prepare_item_string( $product->get_name() ),
new Money( $price, $this->currency ),
$quantity,
$this->prepare_description( $product->get_description() ),
$this->prepare_item_string( $product->get_description() ),
null,
$product->get_sku(),
$this->prepare_sku( $product->get_sku() ),
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
$product->get_permalink(),
$image[0] ?? '',
0,
$cart_item_key
);
@ -128,15 +135,18 @@ class ItemFactory {
$quantity = (int) $item->get_quantity();
$price_without_tax = (float) $order->get_item_subtotal( $item, false );
$price_without_tax_rounded = round( $price_without_tax, 2 );
$image = $product instanceof WC_Product ? wp_get_attachment_image_src( (int) $product->get_image_id(), 'full' ) : '';
return new Item(
mb_substr( $item->get_name(), 0, 127 ),
$this->prepare_item_string( $item->get_name() ),
new Money( $price_without_tax_rounded, $currency ),
$quantity,
$product instanceof WC_Product ? $this->prepare_description( $product->get_description() ) : '',
$product instanceof WC_Product ? $this->prepare_item_string( $product->get_description() ) : '',
null,
$product instanceof WC_Product ? $product->get_sku() : '',
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
$product instanceof WC_Product ? $this->prepare_sku( $product->get_sku() ) : '',
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
$product instanceof WC_Product ? $product->get_permalink() : '',
$image[0] ?? ''
);
}
@ -150,7 +160,7 @@ class ItemFactory {
*/
private function from_wc_order_fee( \WC_Order_Item_Fee $item, \WC_Order $order ): Item {
return new Item(
$item->get_name(),
$this->prepare_item_string( $item->get_name() ),
new Money( (float) $item->get_amount(), $order->get_currency() ),
$item->get_quantity(),
'',
@ -190,6 +200,8 @@ class ItemFactory {
: null;
$sku = ( isset( $data->sku ) ) ? $data->sku : '';
$category = ( isset( $data->category ) ) ? $data->category : 'PHYSICAL_GOODS';
$url = ( isset( $data->url ) ) ? $data->url : '';
$image_url = ( isset( $data->image_url ) ) ? $data->image_url : '';
return new Item(
$data->name,
@ -198,18 +210,9 @@ class ItemFactory {
$description,
$tax,
$sku,
$category
$category,
$url,
$image_url
);
}
/**
* Cleanups the description and prepares it for sending to PayPal.
*
* @param string $description Item description.
* @return string
*/
protected function prepare_description( string $description ): string {
$description = strip_shortcodes( wp_strip_all_tags( $description ) );
return substr( $description, 0, 127 ) ?: '';
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
@ -48,13 +49,6 @@ class OrderFactory {
*/
private $application_context_factory;
/**
* The PaymentSource factory.
*
* @var PaymentSourceFactory
*/
private $payment_source_factory;
/**
* OrderFactory constructor.
*
@ -62,21 +56,18 @@ class OrderFactory {
* @param PayerFactory $payer_factory The Payer factory.
* @param ApplicationContextRepository $application_context_repository The Application Context repository.
* @param ApplicationContextFactory $application_context_factory The Application Context factory.
* @param PaymentSourceFactory $payment_source_factory The Payment Source factory.
*/
public function __construct(
PurchaseUnitFactory $purchase_unit_factory,
PayerFactory $payer_factory,
ApplicationContextRepository $application_context_repository,
ApplicationContextFactory $application_context_factory,
PaymentSourceFactory $payment_source_factory
ApplicationContextFactory $application_context_factory
) {
$this->purchase_unit_factory = $purchase_unit_factory;
$this->payer_factory = $payer_factory;
$this->application_context_repository = $application_context_repository;
$this->application_context_factory = $application_context_factory;
$this->payment_source_factory = $payment_source_factory;
}
/**
@ -152,9 +143,23 @@ class OrderFactory {
$application_context = ( isset( $order_data->application_context ) ) ?
$this->application_context_factory->from_paypal_response( $order_data->application_context )
: null;
$payment_source = ( isset( $order_data->payment_source ) ) ?
$this->payment_source_factory->from_paypal_response( $order_data->payment_source ) :
null;
$payment_source = null;
if ( isset( $order_data->payment_source ) ) {
$json_encoded_payment_source = wp_json_encode( $order_data->payment_source );
if ( $json_encoded_payment_source ) {
$payment_source_as_array = json_decode( $json_encoded_payment_source, true );
if ( $payment_source_as_array ) {
$name = array_key_first( $payment_source_as_array );
if ( $name ) {
$payment_source = new PaymentSource(
$name,
$order_data->payment_source->$name
);
}
}
}
}
return new Order(
$order_data->id,

View file

@ -1,53 +0,0 @@
<?php
/**
* The PaymentSource factory.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSourceCard;
/**
* Class PaymentSourceFactory
*/
class PaymentSourceFactory {
/**
* Returns a PaymentSource for a PayPal Response.
*
* @param \stdClass $data The JSON object.
*
* @return PaymentSource
*/
public function from_paypal_response( \stdClass $data ): PaymentSource {
$card = null;
$wallet = null;
if ( isset( $data->card ) ) {
$authentication_result = null;
if ( isset( $data->card->authentication_result ) ) {
$authentication_result = new CardAuthenticationResult(
isset( $data->card->authentication_result->liability_shift ) ?
(string) $data->card->authentication_result->liability_shift : '',
isset( $data->card->authentication_result->three_d_secure->enrollment_status ) ?
(string) $data->card->authentication_result->three_d_secure->enrollment_status : '',
isset( $data->card->authentication_result->three_d_secure->authentication_status ) ?
(string) $data->card->authentication_result->three_d_secure->authentication_status : ''
);
}
$card = new PaymentSourceCard(
isset( $data->card->last_digits ) ? (string) $data->card->last_digits : '',
isset( $data->card->brand ) ? (string) $data->card->brand : '',
isset( $data->card->type ) ? (string) $data->card->type : '',
$authentication_result
);
}
return new PaymentSource( $card, $wallet );
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payments;
/**
@ -32,19 +33,29 @@ class PaymentsFactory {
*/
private $capture_factory;
/**
* The Refund factory.
*
* @var RefundFactory
*/
private $refund_factory;
/**
* PaymentsFactory constructor.
*
* @param AuthorizationFactory $authorization_factory The Authorization factory.
* @param CaptureFactory $capture_factory The Capture factory.
* @param RefundFactory $refund_factory The Refund factory.
*/
public function __construct(
AuthorizationFactory $authorization_factory,
CaptureFactory $capture_factory
CaptureFactory $capture_factory,
RefundFactory $refund_factory
) {
$this->authorization_factory = $authorization_factory;
$this->capture_factory = $capture_factory;
$this->refund_factory = $refund_factory;
}
/**
@ -62,12 +73,18 @@ class PaymentsFactory {
isset( $data->authorizations ) ? $data->authorizations : array()
);
$captures = array_map(
function ( \stdClass $authorization ): Capture {
return $this->capture_factory->from_paypal_response( $authorization );
function ( \stdClass $capture ): Capture {
return $this->capture_factory->from_paypal_response( $capture );
},
isset( $data->captures ) ? $data->captures : array()
);
$payments = new Payments( $authorizations, $captures );
$refunds = array_map(
function ( \stdClass $refund ): Refund {
return $this->refund_factory->from_paypal_response( $refund );
},
isset( $data->refunds ) ? $data->refunds : array()
);
$payments = new Payments( $authorizations, $captures, $refunds );
return $payments;
}
}

View file

@ -13,7 +13,7 @@ use WC_Session_Handler;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer;
use WooCommerce\PayPalCommerce\Webhooks\CustomIds;
/**
@ -28,20 +28,6 @@ class PurchaseUnitFactory {
*/
private $amount_factory;
/**
* The payee repository.
*
* @var PayeeRepository
*/
private $payee_repository;
/**
* The payee factory.
*
* @var PayeeFactory
*/
private $payee_factory;
/**
* The item factory.
*
@ -77,37 +63,41 @@ class PurchaseUnitFactory {
*/
private $soft_descriptor;
/**
* The sanitizer for purchase unit output data.
*
* @var PurchaseUnitSanitizer|null
*/
private $sanitizer;
/**
* PurchaseUnitFactory constructor.
*
* @param AmountFactory $amount_factory The amount factory.
* @param PayeeRepository $payee_repository The Payee repository.
* @param PayeeFactory $payee_factory The Payee factory.
* @param ItemFactory $item_factory The item factory.
* @param ShippingFactory $shipping_factory The shipping factory.
* @param PaymentsFactory $payments_factory The payments factory.
* @param string $prefix The prefix.
* @param string $soft_descriptor The soft descriptor.
* @param AmountFactory $amount_factory The amount factory.
* @param ItemFactory $item_factory The item factory.
* @param ShippingFactory $shipping_factory The shipping factory.
* @param PaymentsFactory $payments_factory The payments factory.
* @param string $prefix The prefix.
* @param string $soft_descriptor The soft descriptor.
* @param ?PurchaseUnitSanitizer $sanitizer The purchase unit to_array sanitizer.
*/
public function __construct(
AmountFactory $amount_factory,
PayeeRepository $payee_repository,
PayeeFactory $payee_factory,
ItemFactory $item_factory,
ShippingFactory $shipping_factory,
PaymentsFactory $payments_factory,
string $prefix = 'WC-',
string $soft_descriptor = ''
string $soft_descriptor = '',
PurchaseUnitSanitizer $sanitizer = null
) {
$this->amount_factory = $amount_factory;
$this->payee_repository = $payee_repository;
$this->payee_factory = $payee_factory;
$this->item_factory = $item_factory;
$this->shipping_factory = $shipping_factory;
$this->payments_factory = $payments_factory;
$this->prefix = $prefix;
$this->soft_descriptor = $soft_descriptor;
$this->sanitizer = $sanitizer;
}
/**
@ -135,7 +125,6 @@ class PurchaseUnitFactory {
}
$reference_id = 'default';
$description = '';
$payee = $this->payee_repository->payee();
$custom_id = (string) $order->get_id();
$invoice_id = $this->prefix . $order->get_order_number();
$soft_descriptor = $this->soft_descriptor;
@ -146,11 +135,13 @@ class PurchaseUnitFactory {
$shipping,
$reference_id,
$description,
$payee,
$custom_id,
$invoice_id,
$soft_descriptor
);
$this->init_purchase_unit( $purchase_unit );
/**
* Returns PurchaseUnit for the WC order.
*/
@ -197,8 +188,6 @@ class PurchaseUnitFactory {
$reference_id = 'default';
$description = '';
$payee = $this->payee_repository->payee();
$custom_id = '';
$session = WC()->session;
if ( $session instanceof WC_Session_Handler ) {
@ -215,12 +204,13 @@ class PurchaseUnitFactory {
$shipping,
$reference_id,
$description,
$payee,
$custom_id,
$invoice_id,
$soft_descriptor
);
$this->init_purchase_unit( $purchase_unit );
return $purchase_unit;
}
@ -253,7 +243,6 @@ class PurchaseUnitFactory {
$data->items
);
}
$payee = isset( $data->payee ) ? $this->payee_factory->from_paypal_response( $data->payee ) : null;
$shipping = null;
try {
if ( isset( $data->shipping ) ) {
@ -277,12 +266,14 @@ class PurchaseUnitFactory {
$shipping,
$data->reference_id,
$description,
$payee,
$custom_id,
$invoice_id,
$soft_descriptor,
$payments
);
$this->init_purchase_unit( $purchase_unit );
return $purchase_unit;
}
@ -313,4 +304,16 @@ class PurchaseUnitFactory {
$countries = array( 'AE', 'AF', 'AG', 'AI', 'AL', 'AN', 'AO', 'AW', 'BB', 'BF', 'BH', 'BI', 'BJ', 'BM', 'BO', 'BS', 'BT', 'BW', 'BZ', 'CD', 'CF', 'CG', 'CI', 'CK', 'CL', 'CM', 'CO', 'CR', 'CV', 'DJ', 'DM', 'DO', 'EC', 'EG', 'ER', 'ET', 'FJ', 'FK', 'GA', 'GD', 'GH', 'GI', 'GM', 'GN', 'GQ', 'GT', 'GW', 'GY', 'HK', 'HN', 'HT', 'IE', 'IQ', 'IR', 'JM', 'JO', 'KE', 'KH', 'KI', 'KM', 'KN', 'KP', 'KW', 'KY', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LY', 'ML', 'MM', 'MO', 'MR', 'MS', 'MT', 'MU', 'MW', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NP', 'NR', 'NU', 'OM', 'PA', 'PE', 'PF', 'PY', 'QA', 'RW', 'SA', 'SB', 'SC', 'SD', 'SL', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SY', 'TC', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'UY', 'VC', 'VE', 'VG', 'VN', 'VU', 'WS', 'XA', 'XB', 'XC', 'XE', 'XL', 'XM', 'XN', 'XS', 'YE', 'ZM', 'ZW' );
return in_array( $country_code, $countries, true );
}
/**
* Initializes a purchase unit object.
*
* @param PurchaseUnit $purchase_unit The purchase unit.
* @return void
*/
private function init_purchase_unit( PurchaseUnit $purchase_unit ): void {
if ( $this->sanitizer instanceof PurchaseUnitSanitizer ) {
$purchase_unit->set_sanitizer( $this->sanitizer );
}
}
}

View file

@ -0,0 +1,91 @@
<?php
/**
* The refund factory.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
use WooCommerce\PayPalCommerce\ApiClient\Entity\RefundStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\RefundStatusDetails;
/**
* Class RefundFactory
*/
class RefundFactory {
/**
* The Amount factory.
*
* @var AmountFactory
*/
private $amount_factory;
/**
* The SellerPayableBreakdownFactory factory.
*
* @var SellerPayableBreakdownFactory
*/
private $seller_payable_breakdown_factory;
/**
* The RefundPayerFactory factory.
*
* @var RefundPayerFactory
*/
private $refund_payer_factory;
/**
* RefundFactory constructor.
*
* @param AmountFactory $amount_factory The amount factory.
* @param SellerPayableBreakdownFactory $seller_payable_breakdown_factory The payable breakdown factory.
* @param RefundPayerFactory $refund_payer_factory The payer breakdown factory.
*/
public function __construct(
AmountFactory $amount_factory,
SellerPayableBreakdownFactory $seller_payable_breakdown_factory,
RefundPayerFactory $refund_payer_factory
) {
$this->amount_factory = $amount_factory;
$this->seller_payable_breakdown_factory = $seller_payable_breakdown_factory;
$this->refund_payer_factory = $refund_payer_factory;
}
/**
* Returns the refund object based off the PayPal response.
*
* @param \stdClass $data The PayPal response.
*
* @return Refund
*/
public function from_paypal_response( \stdClass $data ) : Refund {
$reason = $data->status_details->reason ?? null;
$seller_payable_breakdown = isset( $data->seller_payable_breakdown ) ?
$this->seller_payable_breakdown_factory->from_paypal_response( $data->seller_payable_breakdown )
: null;
$payer = isset( $data->payer ) ?
$this->refund_payer_factory->from_paypal_response( $data->payer )
: null;
return new Refund(
(string) $data->id,
new RefundStatus(
(string) $data->status,
$reason ? new RefundStatusDetails( $reason ) : null
),
$this->amount_factory->from_paypal_response( $data->amount ),
(string) ( $data->invoice_id ?? '' ),
(string) ( $data->custom_id ?? '' ),
$seller_payable_breakdown,
(string) ( $data->acquirer_reference_number ?? '' ),
(string) ( $data->note_to_payer ?? '' ),
$payer
);
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* The RefundPayerFactory factory.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Address;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerName;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerTaxInfo;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Phone;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PhoneWithType;
use WooCommerce\PayPalCommerce\ApiClient\Entity\RefundPayer;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
/**
* Class RefundPayerFactory
*/
class RefundPayerFactory {
/**
* Returns a Refund Payer object based off a PayPal Response.
*
* @param \stdClass $data The JSON object.
*
* @return RefundPayer
*/
public function from_paypal_response( \stdClass $data ): RefundPayer {
return new RefundPayer(
isset( $data->email_address ) ? $data->email_address : '',
isset( $data->merchant_id ) ? $data->merchant_id : ''
);
}
}

View file

@ -0,0 +1,81 @@
<?php
/**
* The SellerPayableBreakdownFactory Factory.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use stdClass;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PlatformFee;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerPayableBreakdown;
/**
* Class SellerPayableBreakdownFactory
*/
class SellerPayableBreakdownFactory {
/**
* The Money factory.
*
* @var MoneyFactory
*/
private $money_factory;
/**
* The PlatformFee factory.
*
* @var PlatformFeeFactory
*/
private $platform_fee_factory;
/**
* SellerPayableBreakdownFactory constructor.
*
* @param MoneyFactory $money_factory The Money factory.
* @param PlatformFeeFactory $platform_fee_factory The PlatformFee factory.
*/
public function __construct(
MoneyFactory $money_factory,
PlatformFeeFactory $platform_fee_factory
) {
$this->money_factory = $money_factory;
$this->platform_fee_factory = $platform_fee_factory;
}
/**
* Returns a SellerPayableBreakdownFactory object based off a PayPal Response.
*
* @param stdClass $data The JSON object.
*
* @return SellerPayableBreakdown
*/
public function from_paypal_response( stdClass $data ): SellerPayableBreakdown {
$gross_amount = ( isset( $data->gross_amount ) ) ? $this->money_factory->from_paypal_response( $data->gross_amount ) : null;
$paypal_fee = ( isset( $data->paypal_fee ) ) ? $this->money_factory->from_paypal_response( $data->paypal_fee ) : null;
$paypal_fee_in_receivable_currency = ( isset( $data->paypal_fee_in_receivable_currency ) ) ? $this->money_factory->from_paypal_response( $data->paypal_fee_in_receivable_currency ) : null;
$net_amount = ( isset( $data->net_amount ) ) ? $this->money_factory->from_paypal_response( $data->net_amount ) : null;
$net_amount_in_receivable_currency = ( isset( $data->net_amount_in_receivable_currency ) ) ? $this->money_factory->from_paypal_response( $data->net_amount_in_receivable_currency ) : null;
$total_refunded_amount = ( isset( $data->total_refunded_amount ) ) ? $this->money_factory->from_paypal_response( $data->total_refunded_amount ) : null;
$platform_fees = ( isset( $data->platform_fees ) ) ? array_map(
function ( stdClass $fee_data ): PlatformFee {
return $this->platform_fee_factory->from_paypal_response( $fee_data );
},
$data->platform_fees
) : array();
return new SellerPayableBreakdown(
$gross_amount,
$paypal_fee,
$paypal_fee_in_receivable_currency,
$net_amount,
$net_amount_in_receivable_currency,
$total_refunded_amount,
$platform_fees
);
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusCapability;
/**
* Class SellerStatusFactory
@ -37,6 +38,17 @@ class SellerStatusFactory {
isset( $json->products ) ? (array) $json->products : array()
);
return new SellerStatus( $products );
$capabilities = array_map(
function( $json ) : SellerStatusCapability {
$capability = new SellerStatusCapability(
isset( $json->name ) ? (string) $json->name : '',
isset( $json->status ) ? (string) $json->status : ''
);
return $capability;
},
isset( $json->capabilities ) ? (array) $json->capabilities : array()
);
return new SellerStatus( $products, $capabilities );
}
}

View file

@ -50,9 +50,7 @@ class ShippingOptionFactory {
$cart->calculate_shipping();
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods', array() );
if ( ! is_array( $chosen_shipping_methods ) ) {
$chosen_shipping_methods = array();
}
$chosen_shipping_method = $chosen_shipping_methods[0] ?? false;
$packages = WC()->shipping()->get_packages();
$options = array();
@ -62,11 +60,10 @@ class ShippingOptionFactory {
if ( ! $rate instanceof \WC_Shipping_Rate ) {
continue;
}
$options[] = new ShippingOption(
$rate->get_id(),
$rate->get_label(),
in_array( $rate->get_id(), $chosen_shipping_methods, true ),
$rate->get_id() === $chosen_shipping_method,
new Money(
(float) $rate->get_cost(),
get_woocommerce_currency()

View file

@ -0,0 +1,94 @@
<?php
/**
* Failure registry.
*
* This class is used to remember API failures.
* Mostly to prevent multiple failed API requests.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
/**
* Class FailureRegistry
*/
class FailureRegistry {
const CACHE_KEY = 'failure_registry';
const CACHE_TIMEOUT = 60 * 60 * 24; // DAY_IN_SECONDS, if necessary we can increase this.
const SELLER_STATUS_KEY = 'seller_status';
/**
* The Cache.
*
* @var Cache
*/
private $cache;
/**
* FailureRegistry constructor.
*
* @param Cache $cache The Cache.
*/
public function __construct( Cache $cache ) {
$this->cache = $cache;
}
/**
* Returns if there was a failure within a given timeframe.
*
* @param string $key The cache key.
* @param int $seconds The timeframe in seconds.
* @return bool
*/
public function has_failure_in_timeframe( string $key, int $seconds ): bool {
$cache_key = $this->cache_key( $key );
$failure_time = $this->cache->get( $cache_key );
if ( ! $failure_time ) {
return false;
}
$expiration = $failure_time + $seconds;
return $expiration > time();
}
/**
* Registers a failure.
*
* @param string $key The cache key.
* @return void
*/
public function add_failure( string $key ) {
$cache_key = $this->cache_key( $key );
$this->cache->set( $cache_key, time(), self::CACHE_TIMEOUT );
}
/**
* Clear a given failure.
*
* @param string $key The cache key.
* @return void
*/
public function clear_failures( string $key ) {
$cache_key = $this->cache_key( $key );
if ( $this->cache->has( $cache_key ) ) {
$this->cache->delete( $cache_key );
}
}
/**
* Build cache key.
*
* @param string $key The cache key.
* @return string
*/
private function cache_key( string $key ): string {
return implode( '_', array( self::CACHE_KEY, $key ) );
}
}

View file

@ -0,0 +1,34 @@
<?php
/**
* PayPal item helper.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
trait ItemTrait {
/**
* Cleans up item strings (title and description for example) and prepares them for sending to PayPal.
*
* @param string $string Item string.
* @return string
*/
protected function prepare_item_string( string $string ): string {
$string = strip_shortcodes( wp_strip_all_tags( $string ) );
return substr( $string, 0, 127 ) ?: '';
}
/**
* Prepares the sku for sending to PayPal.
*
* @param string $sku Item sku.
* @return string
*/
protected function prepare_sku( string $sku ): string {
return substr( wp_strip_all_tags( $sku ), 0, 127 ) ?: '';
}
}

View file

@ -33,4 +33,16 @@ class MoneyFormatter {
? (string) round( $value, 0 )
: number_format( $value, 2, '.', '' );
}
/**
* Returns the minimum amount a currency can be incremented or decremented.
*
* @param string $currency The 3-letter currency code.
* @return float
*/
public function minimum_increment( string $currency ): float {
return (float) in_array( $currency, $this->currencies_without_decimals, true )
? 1.00
: 0.01;
}
}

View file

@ -0,0 +1,160 @@
<?php
/**
* PayPal order transient helper.
*
* This class is used to pass transient data between the PayPal order and the WooCommerce order.
* These two orders can be created on different requests and at different times so this transient
* data must be persisted between requests.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
/**
* Class OrderTransient
*/
class OrderTransient {
const CACHE_KEY = 'order_transient';
const CACHE_TIMEOUT = 60 * 60 * 24; // DAY_IN_SECONDS, if necessary we can increase this.
/**
* The Cache.
*
* @var Cache
*/
private $cache;
/**
* The purchase unit sanitizer.
*
* @var PurchaseUnitSanitizer
*/
private $purchase_unit_sanitizer;
/**
* OrderTransient constructor.
*
* @param Cache $cache The Cache.
* @param PurchaseUnitSanitizer $purchase_unit_sanitizer The purchase unit sanitizer.
*/
public function __construct( Cache $cache, PurchaseUnitSanitizer $purchase_unit_sanitizer ) {
$this->cache = $cache;
$this->purchase_unit_sanitizer = $purchase_unit_sanitizer;
}
/**
* Processes the created PayPal order.
*
* @param Order $order The PayPal order.
* @return void
*/
public function on_order_created( Order $order ): void {
$message = $this->purchase_unit_sanitizer->get_last_message();
$this->add_order_note( $order, $message );
}
/**
* Processes the created WooCommerce order.
*
* @param WC_Order $wc_order The WooCommerce order.
* @param Order $order The PayPal order.
* @return void
*/
public function on_woocommerce_order_created( WC_Order $wc_order, Order $order ): void {
$cache_key = $this->cache_key( $order );
if ( ! $cache_key ) {
return;
}
$this->apply_order_notes( $order, $wc_order );
$this->cache->delete( $cache_key );
}
/**
* Adds an order note associated with a PayPal order.
* It can be added to a WooCommerce order associated with this PayPal order in the future.
*
* @param Order $order The PayPal order.
* @param string $message The message to be added to order notes.
* @return void
*/
private function add_order_note( Order $order, string $message ): void {
if ( ! $message ) {
return;
}
$cache_key = $this->cache_key( $order );
if ( ! $cache_key ) {
return;
}
$transient = $this->cache->get( $cache_key );
if ( ! is_array( $transient ) ) {
$transient = array();
}
if ( ! is_array( $transient['notes'] ?? null ) ) {
$transient['notes'] = array();
}
$transient['notes'][] = $message;
$this->cache->set( $cache_key, $transient, self::CACHE_TIMEOUT );
}
/**
* Adds an order note associated with a PayPal order.
* It can be added to a WooCommerce order associated with this PayPal order in the future.
*
* @param Order $order The PayPal order.
* @param WC_Order $wc_order The WooCommerce order.
* @return void
*/
private function apply_order_notes( Order $order, WC_Order $wc_order ): void {
$cache_key = $this->cache_key( $order );
if ( ! $cache_key ) {
return;
}
$transient = $this->cache->get( $cache_key );
if ( ! is_array( $transient ) ) {
return;
}
if ( ! is_array( $transient['notes'] ) ) {
return;
}
foreach ( $transient['notes'] as $note ) {
if ( ! is_string( $note ) ) {
continue;
}
$wc_order->add_order_note( $note );
}
}
/**
* Build cache key.
*
* @param Order $order The PayPal order.
* @return string|null
*/
private function cache_key( Order $order ): ?string {
if ( ! $order->id() ) {
return null;
}
return implode( '_', array( self::CACHE_KEY . $order->id() ) );
}
}

View file

@ -0,0 +1,368 @@
<?php
/**
* Class PurchaseUnitSanitizer.
*
* Sanitizes a purchase_unit array to be consumed by PayPal.
*
* All money values send to PayPal can only have 2 decimal points. WooCommerce internally does
* not have this restriction. Therefore, the totals of the cart in WooCommerce and the totals
* of the rounded money values of the items, we send to PayPal, can differ. In those case we either:
* - Add an extra line with roundings.
* - Don't send the line items.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
/**
* Class PurchaseUnitSanitizer
*/
class PurchaseUnitSanitizer {
const MODE_DITCH = 'ditch';
const MODE_EXTRA_LINE = 'extra_line';
const VALID_MODES = array(
self::MODE_DITCH,
self::MODE_EXTRA_LINE,
);
const EXTRA_LINE_NAME = 'Subtotal mismatch';
/**
* The purchase unit data
*
* @var array
*/
private $purchase_unit = array();
/**
* Whether to allow items to be ditched.
*
* @var bool
*/
private $allow_ditch_items = true;
/**
* The working mode
*
* @var string
*/
private $mode;
/**
* The name for the extra line
*
* @var string
*/
private $extra_line_name;
/**
* The last message. To be added to order notes.
*
* @var string
*/
private $last_message = '';
/**
* If the items and breakdown has been ditched.
*
* @var bool
*/
private $has_ditched_items_breakdown = false;
/**
* PurchaseUnitSanitizer constructor.
*
* @param string|null $mode The mismatch handling mode, ditch or extra_line.
* @param string|null $extra_line_name The name of the extra line.
*/
public function __construct( string $mode = null, string $extra_line_name = null ) {
if ( ! in_array( $mode, self::VALID_MODES, true ) ) {
$mode = self::MODE_DITCH;
}
if ( ! $extra_line_name ) {
$extra_line_name = self::EXTRA_LINE_NAME;
}
$this->mode = $mode;
$this->extra_line_name = $extra_line_name;
}
/**
* The purchase_unit amount.
*
* @return array
*/
private function amount(): array {
return $this->purchase_unit['amount'] ?? array();
}
/**
* The purchase_unit currency code.
*
* @return string
*/
private function currency_code(): string {
return (string) ( $this->amount()['currency_code'] ?? '' );
}
/**
* The purchase_unit breakdown.
*
* @return array
*/
private function breakdown(): array {
return $this->amount()['breakdown'] ?? array();
}
/**
* The purchase_unit breakdown.
*
* @param string $key The breakdown element to get the value from.
* @return float
*/
private function breakdown_value( string $key ): float {
if ( ! isset( $this->breakdown()[ $key ] ) ) {
return 0.0;
}
return (float) ( $this->breakdown()[ $key ]['value'] ?? 0.0 );
}
/**
* The purchase_unit items array.
*
* @return array
*/
private function items(): array {
return $this->purchase_unit['items'] ?? array();
}
/**
* The sanitizes the purchase_unit array.
*
* @param array $purchase_unit The purchase_unit array that should be sanitized.
* @param bool $allow_ditch_items Whether to allow items to be ditched.
* @return array
*/
public function sanitize( array $purchase_unit, bool $allow_ditch_items = true ): array {
$this->purchase_unit = $purchase_unit;
$this->allow_ditch_items = $allow_ditch_items;
$this->has_ditched_items_breakdown = false;
$this->sanitize_item_amount_mismatch();
$this->sanitize_item_tax_mismatch();
$this->sanitize_breakdown_mismatch();
return $this->purchase_unit;
}
/**
* The sanitizes the purchase_unit items amount.
*
* @return void
*/
private function sanitize_item_amount_mismatch(): void {
$item_mismatch = $this->calculate_item_mismatch();
if ( $this->mode === self::MODE_EXTRA_LINE ) {
if ( $item_mismatch < 0 ) {
// Do floors on item amounts so item_mismatch is a positive value.
foreach ( $this->purchase_unit['items'] as $index => $item ) {
// Get a more intelligent adjustment mechanism.
$increment = ( new MoneyFormatter() )->minimum_increment( $item['unit_amount']['currency_code'] );
$this->purchase_unit['items'][ $index ]['unit_amount'] = ( new Money(
( (float) $item['unit_amount']['value'] ) - $increment,
$item['unit_amount']['currency_code']
) )->to_array();
}
}
$item_mismatch = $this->calculate_item_mismatch();
if ( $item_mismatch > 0 ) {
// Add extra line item with roundings.
$line_name = $this->extra_line_name;
$roundings_money = new Money( $item_mismatch, $this->currency_code() );
$this->purchase_unit['items'][] = ( new Item( $line_name, $roundings_money, 1 ) )->to_array();
$this->set_last_message(
__( 'Item amount mismatch. Extra line added.', 'woocommerce-paypal-payments' )
);
}
$item_mismatch = $this->calculate_item_mismatch();
}
if ( $item_mismatch !== 0.0 ) {
// Ditch items.
if ( $this->allow_ditch_items && isset( $this->purchase_unit['items'] ) ) {
unset( $this->purchase_unit['items'] );
$this->set_last_message(
__( 'Item amount mismatch. Items ditched.', 'woocommerce-paypal-payments' )
);
}
}
}
/**
* The sanitizes the purchase_unit items tax.
*
* @return void
*/
private function sanitize_item_tax_mismatch(): void {
$tax_mismatch = $this->calculate_tax_mismatch();
if ( $this->allow_ditch_items && $tax_mismatch !== 0.0 ) {
// Unset tax in items.
foreach ( $this->purchase_unit['items'] as $index => $item ) {
if ( isset( $this->purchase_unit['items'][ $index ]['tax'] ) ) {
unset( $this->purchase_unit['items'][ $index ]['tax'] );
}
if ( isset( $this->purchase_unit['items'][ $index ]['tax_rate'] ) ) {
unset( $this->purchase_unit['items'][ $index ]['tax_rate'] );
}
}
}
}
/**
* The sanitizes the purchase_unit breakdown.
*
* @return void
*/
private function sanitize_breakdown_mismatch(): void {
$breakdown_mismatch = $this->calculate_breakdown_mismatch();
if ( $this->allow_ditch_items && $breakdown_mismatch !== 0.0 ) {
// Ditch breakdowns and items.
if ( isset( $this->purchase_unit['items'] ) ) {
unset( $this->purchase_unit['items'] );
}
if ( isset( $this->purchase_unit['amount']['breakdown'] ) ) {
unset( $this->purchase_unit['amount']['breakdown'] );
}
$this->has_ditched_items_breakdown = true;
$this->set_last_message(
__( 'Breakdown mismatch. Items and breakdown ditched.', 'woocommerce-paypal-payments' )
);
}
}
/**
* The calculates amount mismatch of items sums with breakdown.
*
* @return float
*/
private function calculate_item_mismatch(): float {
$item_total = $this->breakdown_value( 'item_total' );
if ( ! $item_total ) {
return 0;
}
$remaining_item_total = array_reduce(
$this->items(),
function ( float $total, array $item ): float {
return $total - (float) $item['unit_amount']['value'] * (float) $item['quantity'];
},
$item_total
);
return round( $remaining_item_total, 2 );
}
/**
* The calculates tax mismatch of items sums with breakdown.
*
* @return float
*/
private function calculate_tax_mismatch(): float {
$tax_total = $this->breakdown_value( 'tax_total' );
$items_with_tax = array_filter(
$this->items(),
function ( array $item ): bool {
return isset( $item['tax'] );
}
);
if ( ! $tax_total || empty( $items_with_tax ) ) {
return 0;
}
$remaining_tax_total = array_reduce(
$this->items(),
function ( float $total, array $item ): float {
$tax = $item['tax'] ?? false;
if ( $tax ) {
$total -= (float) $tax['value'] * (float) $item['quantity'];
}
return $total;
},
$tax_total
);
return round( $remaining_tax_total, 2 );
}
/**
* The calculates mismatch of breakdown sums with total amount.
*
* @return float
*/
private function calculate_breakdown_mismatch(): float {
$breakdown = $this->breakdown();
if ( ! $breakdown ) {
return 0;
}
$amount_total = 0.0;
$amount_total += $this->breakdown_value( 'item_total' );
$amount_total += $this->breakdown_value( 'tax_total' );
$amount_total += $this->breakdown_value( 'shipping' );
$amount_total -= $this->breakdown_value( 'discount' );
$amount_total -= $this->breakdown_value( 'shipping_discount' );
$amount_total += $this->breakdown_value( 'handling' );
$amount_total += $this->breakdown_value( 'insurance' );
$amount_str = $this->amount()['value'] ?? 0;
$amount_total_str = ( new Money( $amount_total, $this->currency_code() ) )->value_str();
return $amount_str - $amount_total_str;
}
/**
* Indicates if the items and breakdown were ditched.
*
* @return bool
*/
public function has_ditched_items_breakdown(): bool {
return $this->has_ditched_items_breakdown;
}
/**
* Returns the last sanitization message.
*
* @return string
*/
public function get_last_message(): string {
return $this->last_message;
}
/**
* Set the last sanitization message.
*
* @param string $message The message.
*/
public function set_last_message( string $message ): void {
$this->last_message = $message;
}
}

View file

@ -54,7 +54,7 @@ class ApplicationContextRepository {
$payment_preference = $this->settings->has( 'payee_preferred' ) && $this->settings->get( 'payee_preferred' ) ?
ApplicationContext::PAYMENT_METHOD_IMMEDIATE_PAYMENT_REQUIRED : ApplicationContext::PAYMENT_METHOD_UNRESTRICTED;
$context = new ApplicationContext(
network_home_url( \WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ),
home_url( \WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ),
(string) wc_get_checkout_url(),
(string) $brand_name,
$locale,

View file

@ -0,0 +1,14 @@
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": "3.25.0"
}
],
[
"@babel/preset-react"
]
]
}

3
modules/ppcp-applepay/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
assets/js
assets/css

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View file

@ -1,7 +1,7 @@
{
"name": "woocommerce/ppcp-subscription",
"name": "woocommerce/ppcp-applepay",
"type": "dhii-mod",
"description": "Subscription module for PPCP",
"description": "Applepay module for PPCP",
"license": "GPL-2.0",
"require": {
"php": "^7.2 | ^8.0",
@ -9,7 +9,7 @@
},
"autoload": {
"psr-4": {
"WooCommerce\\PayPalCommerce\\Subscription\\": "src"
"WooCommerce\\PayPalCommerce\\Applepay\\": "src"
}
},
"minimum-stability": "dev",

156
modules/ppcp-applepay/composer.lock generated Normal file
View file

@ -0,0 +1,156 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "30c5bd428bece98b555ddc0b2da044f3",
"packages": [
{
"name": "container-interop/service-provider",
"version": "v0.4.0",
"source": {
"type": "git",
"url": "https://github.com/container-interop/service-provider.git",
"reference": "4969b9e49460690b7430b3f1a87cab07be61418a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/container-interop/service-provider/zipball/4969b9e49460690b7430b3f1a87cab07be61418a",
"reference": "4969b9e49460690b7430b3f1a87cab07be61418a",
"shasum": ""
},
"require": {
"psr/container": "^1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Interop\\Container\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Promoting container interoperability through standard service providers",
"homepage": "https://github.com/container-interop/service-provider",
"support": {
"issues": "https://github.com/container-interop/service-provider/issues",
"source": "https://github.com/container-interop/service-provider/tree/master"
},
"time": "2017-09-20T14:13:36+00:00"
},
{
"name": "dhii/module-interface",
"version": "v0.3.0-alpha2",
"source": {
"type": "git",
"url": "https://github.com/Dhii/module-interface.git",
"reference": "0e39f167d7ed8990c82f5d2e6084159d1a502a5b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Dhii/module-interface/zipball/0e39f167d7ed8990c82f5d2e6084159d1a502a5b",
"reference": "0e39f167d7ed8990c82f5d2e6084159d1a502a5b",
"shasum": ""
},
"require": {
"container-interop/service-provider": "^0.4",
"php": "^7.1 | ^8.0",
"psr/container": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0 | ^8.0 | ^9.0",
"slevomat/coding-standard": "^6.0",
"vimeo/psalm": "^3.11.7 | ^4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-develop": "0.3.x-dev"
}
},
"autoload": {
"psr-4": {
"Dhii\\Modular\\Module\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Dhii Team",
"email": "development@dhii.co"
}
],
"description": "Interfaces for modules",
"support": {
"issues": "https://github.com/Dhii/module-interface/issues",
"source": "https://github.com/Dhii/module-interface/tree/v0.3.0-alpha2"
},
"time": "2021-08-23T08:23:01+00:00"
},
{
"name": "psr/container",
"version": "1.1.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/container.git",
"reference": "8622567409010282b7aeebe4bb841fe98b58dcaf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/8622567409010282b7aeebe4bb841fe98b58dcaf",
"reference": "8622567409010282b7aeebe4bb841fe98b58dcaf",
"shasum": ""
},
"require": {
"php": ">=7.2.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Psr\\Container\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common Container Interface (PHP FIG PSR-11)",
"homepage": "https://github.com/php-fig/container",
"keywords": [
"PSR-11",
"container",
"container-interface",
"container-interop",
"psr"
],
"support": {
"issues": "https://github.com/php-fig/container/issues",
"source": "https://github.com/php-fig/container/tree/1.1.1"
},
"time": "2021-03-05T17:36:06+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "dev",
"stability-flags": [],
"prefer-stable": true,
"prefer-lowest": false,
"platform": {
"php": "^7.2 | ^8.0"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

View file

@ -0,0 +1,357 @@
<?php
/**
* The Applepay module extensions.
*
* @package WooCommerce\PayPalCommerce\Applepay
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
use WooCommerce\PayPalCommerce\Applepay\Assets\PropertiesDictionary;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Helper\DisplayManager;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
return array(
'wcgateway.settings.fields' => function ( ContainerInterface $container, array $fields ): array {
// Used in various places to mark fields for the preview button.
$apm_name = 'ApplePay';
// Eligibility check.
if ( ! $container->has( 'applepay.eligible' ) || ! $container->get( 'applepay.eligible' ) ) {
return $fields;
}
$is_available = $container->get( 'applepay.available' );
$is_referral = $container->get( 'applepay.is_referral' );
$insert_after = function ( array $array, string $key, array $new ): array {
$keys = array_keys( $array );
$index = array_search( $key, $keys, true );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
};
$display_manager = $container->get( 'wcgateway.display-manager' );
assert( $display_manager instanceof DisplayManager );
// Domain registration.
$env = $container->get( 'onboarding.environment' );
assert( $env instanceof Environment );
$domain_registration_url = 'https://www.paypal.com/uccservicing/apm/applepay';
if ( $env->current_environment_is( Environment::SANDBOX ) ) {
$domain_registration_url = 'https://www.sandbox.paypal.com/uccservicing/apm/applepay';
}
// Domain validation.
$domain_validation_text = __( 'Status: Domain validation failed ❌', 'woocommerce-paypal-payments' );
if ( ! $container->get( 'applepay.has_validated' ) ) {
$domain_validation_text = __( 'The domain has not yet been validated. Use the Apple Pay button to validate the domain ❌', 'woocommerce-paypal-payments' );
} elseif ( $container->get( 'applepay.is_validated' ) ) {
$domain_validation_text = __( 'Status: Domain successfully validated ✔️', 'woocommerce-paypal-payments' );
}
// Device eligibility.
$device_eligibility_text = __( 'Status: Your current browser/device does not seem to support Apple Pay ❌', 'woocommerce-paypal-payments' );
$device_eligibility_notes = sprintf(
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
__( 'Though the button may display in previews, it won\'t appear in the shop. For details, refer to the %1$sApple Pay requirements%2$s.', 'woocommerce-paypal-payments' ),
'<a href="https://woo.com/document/woocommerce-paypal-payments/#apple-pay" target="_blank">',
'</a>'
);
if ( $container->get( 'applepay.is_browser_supported' ) ) {
$device_eligibility_text = __( 'Status: Your current browser/device seems to support Apple Pay ✔️', 'woocommerce-paypal-payments' );
$device_eligibility_notes = __( 'The Apple Pay button will be visible both in previews and below the PayPal buttons in the shop.', 'woocommerce-paypal-payments' );
}
$module_url = $container->get( 'applepay.url' );
// Connection tab fields.
$fields = $insert_after(
$fields,
'ppcp_reference_transactions_status',
array(
'applepay_status' => array(
'title' => __( 'Apple Pay Payments', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-text',
'text' => $container->get( 'applepay.settings.connection.status-text' ),
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID,
),
)
);
if ( ! $is_available && $is_referral ) {
$connection_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway&ppcp-tab=ppcp-connection#field-credentials_feature_onboarding_heading' );
$connection_link = '<a href="' . $connection_url . '" style="pointer-events: auto">';
return $insert_after(
$fields,
'digital_wallet_heading',
array(
'applepay_button_enabled' => array(
'title' => __( 'Apple Pay Button', 'woocommerce-paypal-payments' ),
'title_html' => sprintf(
'<img src="%sassets/images/applepay.png" alt="%s" style="max-width: 150px; max-height: 45px;" />',
$module_url,
__( 'Apple Pay', 'woocommerce-paypal-payments' )
),
'type' => 'checkbox',
'class' => array( 'ppcp-grayed-out-text' ),
'input_class' => array( 'ppcp-disabled-checkbox' ),
'label' => __( 'Enable Apple Pay button', 'woocommerce-paypal-payments' )
. '<p class="description">'
. sprintf(
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
__( 'Your PayPal account %1$srequires additional permissions%2$s to enable Apple Pay.', 'woocommerce-paypal-payments' ),
$connection_link,
'</a>'
)
. '</p>',
'default' => 'yes',
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
'custom_attributes' => array(
'data-ppcp-display' => wp_json_encode(
array(
$display_manager
->rule()
->condition_is_true( false )
->action_enable( 'applepay_button_enabled' )
->to_array(),
)
),
),
'classes' => array( 'ppcp-valign-label-middle', 'ppcp-align-label-center' ),
),
)
);
}
return $insert_after(
$fields,
'digital_wallet_heading',
array(
'spacer' => array(
'title' => '',
'type' => 'ppcp-text',
'text' => '',
'class' => array(),
'classes' => array( 'ppcp-active-spacer' ),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
),
'applepay_button_enabled' => array(
'title' => __( 'Apple Pay Button', 'woocommerce-paypal-payments' ),
'title_html' => sprintf(
'<img src="%sassets/images/applepay.png" alt="%s" style="max-width: 150px; max-height: 45px;" />',
$module_url,
__( 'Apple Pay', 'woocommerce-paypal-payments' )
),
'type' => 'checkbox',
'label' => __( 'Enable Apple Pay button', 'woocommerce-paypal-payments' )
. '<p class="description">'
. sprintf(
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
__( 'Buyers can use %1$sApple Pay%2$s to make payments on the web using the Safari web browser or an iOS device.', 'woocommerce-paypal-payments' ),
'<a href="https://woocommerce.com/document/woocommerce-paypal-payments/#apple-pay" target="_blank">',
'</a>'
)
. '</p>',
'default' => 'yes',
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
'custom_attributes' => array(
'data-ppcp-display' => wp_json_encode(
array(
$display_manager
->rule()
->condition_element( 'applepay_button_enabled', '1' )
->action_visible( 'applepay_button_domain_registration' )
->action_visible( 'applepay_button_domain_validation' )
->action_visible( 'applepay_button_device_eligibility' )
->action_visible( 'applepay_button_color' )
->action_visible( 'applepay_button_type' )
->action_visible( 'applepay_button_language' )
->action_visible( 'applepay_checkout_data_mode' )
->action_visible( 'applepay_button_preview' )
->action_class( 'applepay_button_enabled', 'active' )
->to_array(),
)
),
'data-ppcp-apm-name' => $apm_name,
'data-ppcp-field-name' => 'is_enabled',
),
'classes' => array( 'ppcp-valign-label-middle', 'ppcp-align-label-center' ),
),
'applepay_button_domain_registration' => array(
'title' => __( 'Domain Registration', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-text',
'text' =>
'<a href="' . $domain_registration_url . '" class="button" target="_blank">'
. __( 'Manage Domain Registration', 'woocommerce-paypal-payments' )
. '</a>'
. '<p class="description">'
. __( 'Any (sub)domain names showing an Apple Pay button must be registered on the PayPal website. If the domain displaying the Apple Pay button isn\'t registered, the payment method won\'t work.', 'woocommerce-paypal-payments' )
. '</p>',
'desc_tip' => true,
'description' => __(
'Registering the website domain on the PayPal site is mandated by Apple. Payments will fail if the Apple Pay button is used on an unregistered domain.',
'woocommerce-paypal-payments'
),
'classes' => array( 'ppcp-field-indent' ),
'class' => array(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
),
'applepay_button_domain_validation' => array(
'title' => __( 'Domain Validation', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-text',
'text' => $domain_validation_text
. '<p class="description">'
. sprintf(
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
__( '<strong>Note:</strong> PayPal Payments automatically presents the %1$sdomain association file%2$s for Apple to validate your registered domain.', 'woocommerce-paypal-payments' ),
'<a href="/.well-known/apple-developer-merchantid-domain-association" target="_blank">',
'</a>'
)
. '</p>',
'desc_tip' => true,
'description' => __(
'Apple requires the website domain to be registered and validated. PayPal Payments automatically presents your domain association file for Apple to validate the manually registered domain.',
'woocommerce-paypal-payments'
),
'classes' => array( 'ppcp-field-indent' ),
'class' => array(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
),
'applepay_button_device_eligibility' => array(
'title' => __( 'Device Eligibility', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-text',
'text' => $device_eligibility_text
. '<p class="description">'
. $device_eligibility_notes
. '</p>',
'desc_tip' => true,
'description' => __(
'Apple Pay demands certain Apple devices for secure payment execution. This helps determine if your current device is compliant.',
'woocommerce-paypal-payments'
),
'classes' => array( 'ppcp-field-indent' ),
'class' => array(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
),
'applepay_button_type' => array(
'title' => __( 'Button Label', 'woocommerce-paypal-payments' ),
'type' => 'select',
'desc_tip' => true,
'description' => __(
'This controls the label of the Apple Pay button.',
'woocommerce-paypal-payments'
),
'classes' => array( 'ppcp-field-indent' ),
'class' => array(),
'input_class' => array( 'wc-enhanced-select' ),
'default' => 'pay',
'options' => PropertiesDictionary::button_types(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
'custom_attributes' => array(
'data-ppcp-apm-name' => $apm_name,
'data-ppcp-field-name' => 'type',
),
),
'applepay_button_color' => array(
'title' => __( 'Button Color', 'woocommerce-paypal-payments' ),
'type' => 'select',
'desc_tip' => true,
'description' => __(
'The Apple Pay Button may appear as a black button with white lettering, white button with black lettering, or a white button with black lettering and a black outline.',
'woocommerce-paypal-payments'
),
'label' => '',
'input_class' => array( 'wc-enhanced-select' ),
'classes' => array( 'ppcp-field-indent' ),
'class' => array(),
'default' => 'black',
'options' => PropertiesDictionary::button_colors(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
'custom_attributes' => array(
'data-ppcp-apm-name' => $apm_name,
'data-ppcp-field-name' => 'color',
),
),
'applepay_button_language' => array(
'title' => __( 'Button Language', 'woocommerce-paypal-payments' ),
'type' => 'select',
'desc_tip' => true,
'description' => __(
'The language and region used for the displayed Apple Pay button. The default value is the current language and region setting in a browser.',
'woocommerce-paypal-payments'
),
'classes' => array( 'ppcp-field-indent' ),
'class' => array(),
'input_class' => array( 'wc-enhanced-select' ),
'default' => 'en',
'options' => PropertiesDictionary::button_languages(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
'custom_attributes' => array(
'data-ppcp-apm-name' => $apm_name,
'data-ppcp-field-name' => 'language',
),
),
'applepay_checkout_data_mode' => array(
'title' => __( 'Send checkout billing and shipping data to Apple Pay', 'woocommerce-paypal-payments' ),
'type' => 'select',
'classes' => array( 'ppcp-field-indent' ),
'class' => array(),
'input_class' => array( 'wc-enhanced-select' ),
'desc_tip' => true,
'description' => __( 'Using the WC form data increases convenience for the customers, but can cause issues if Apple Pay details do not match the billing and shipping data in the checkout form.', 'woocommerce-paypal-payments' ),
'default' => PropertiesDictionary::BILLING_DATA_MODE_DEFAULT,
'options' => PropertiesDictionary::billing_data_modes(),
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'dcc',
'requirements' => array(),
),
'applepay_button_preview' => array(
'type' => 'ppcp-preview',
'preview' => array(
'id' => 'ppcp' . $apm_name . 'ButtonPreview',
'type' => 'button',
'message' => __( 'Button Styling Preview', 'woocommerce-paypal-payments' ),
'apm' => $apm_name,
'single' => true,
),
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => 'dcc',
),
)
);
},
);

View file

@ -0,0 +1,16 @@
<?php
/**
* The Applepay module.
*
* @package WooCommerce\PayPalCommerce\Applepay
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
return static function (): ModuleInterface {
return new ApplepayModule();
};

View file

@ -0,0 +1,34 @@
{
"name": "ppcp-applepay",
"version": "1.0.0",
"license": "GPL-3.0-or-later",
"browserslist": [
"> 0.5%",
"Safari >= 8",
"Chrome >= 41",
"Firefox >= 43",
"Edge >= 14"
],
"dependencies": {
"@paypal/paypal-js": "^6.0.0",
"core-js": "^3.25.0"
},
"devDependencies": {
"@babel/core": "^7.19",
"@babel/preset-env": "^7.19",
"@babel/preset-react": "^7.18.6",
"@woocommerce/dependency-extraction-webpack-plugin": "^2.2.0",
"babel-loader": "^8.2",
"cross-env": "^7.0.3",
"file-loader": "^6.2.0",
"sass": "^1.42.1",
"sass-loader": "^12.1.0",
"webpack": "^5.76",
"webpack-cli": "^4.10"
},
"scripts": {
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
"watch": "cross-env BABEL_ENV=default NODE_ENV=production webpack --watch",
"dev": "cross-env BABEL_ENV=default webpack --watch"
}
}

View file

@ -0,0 +1,54 @@
.ppcp-button-applepay {
// Should replicate apm-button.scss sizes.
--apple-pay-button-height: 45px;
--apple-pay-button-min-height: 35px;
--apple-pay-button-width: 100%;
--apple-pay-button-max-width: 750px;
--apple-pay-button-border-radius: var(--apm-button-border-radius);
--apple-pay-button-overflow: hidden;
--apple-pay-button-box-sizing: border-box;
.ppcp-width-min & {
--apple-pay-button-height: 35px;
}
.ppcp-width-300 & {
--apple-pay-button-height: 45px;
}
.ppcp-width-500 & {
--apple-pay-button-height: 55px;
}
&.ppcp-button-minicart {
--apple-pay-button-display: block;
}
}
.wp-block-woocommerce-checkout, .wp-block-woocommerce-cart {
.ppcp-button-applepay {
--apple-pay-button-margin: 0;
apple-pay-button {
min-width: 0;
width: 100%;
--apple-pay-button-width-default: 100%;
}
}
}
.wp-admin {
&.ppcp-non-ios-device {
.ppcp-button-applepay {
apple-pay-button {
display: block;
}
}
}
.ppcp-button-applepay {
apple-pay-button {
display: block;
}
}
}

View file

@ -0,0 +1,963 @@
import ContextHandlerFactory from './Context/ContextHandlerFactory';
import { createAppleErrors } from './Helper/applePayError';
import { setVisible } from '../../../ppcp-button/resources/js/modules/Helper/Hiding';
import { setEnabled } from '../../../ppcp-button/resources/js/modules/Helper/ButtonDisabler';
import FormValidator from '../../../ppcp-button/resources/js/modules/Helper/FormValidator';
import ErrorHandler from '../../../ppcp-button/resources/js/modules/ErrorHandler';
import widgetBuilder from '../../../ppcp-button/resources/js/modules/Renderer/WidgetBuilder';
import { apmButtonsInit } from '../../../ppcp-button/resources/js/modules/Helper/ApmButtons';
class ApplepayButton {
constructor( context, externalHandler, buttonConfig, ppcpConfig ) {
apmButtonsInit( ppcpConfig );
this.isInitialized = false;
this.context = context;
this.externalHandler = externalHandler;
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.paymentsClient = null;
this.formData = null;
this.contextHandler = ContextHandlerFactory.create(
this.context,
this.buttonConfig,
this.ppcpConfig
);
this.updatedContactInfo = [];
this.selectedShippingMethod = [];
this.nonce =
document.getElementById( 'woocommerce-process-checkout-nonce' )
?.value || buttonConfig.nonce;
// Stores initialization data sent to the button.
this.initialPaymentRequest = null;
// Default eligibility status.
this.isEligible = true;
this.log = function () {
if ( this.buttonConfig.is_debug ) {
//console.log('[ApplePayButton]', ...arguments);
}
};
this.refreshContextData();
// Debug helpers
jQuery( document ).on( 'ppcp-applepay-debug', () => {
console.log( 'ApplePayButton', this.context, this );
} );
document.ppcpApplepayButtons = document.ppcpApplepayButtons || {};
document.ppcpApplepayButtons[ this.context ] = this;
}
init( config ) {
if ( this.isInitialized ) {
return;
}
if ( ! this.contextHandler.validateContext() ) {
return;
}
this.log( 'Init', this.context );
this.initEventHandlers();
this.isInitialized = true;
this.applePayConfig = config;
this.isEligible =
( this.applePayConfig.isEligible && window.ApplePaySession ) ||
this.buttonConfig.is_admin;
if ( this.isEligible ) {
this.fetchTransactionInfo().then( () => {
this.addButton();
const id_minicart =
'#apple-' + this.buttonConfig.button.mini_cart_wrapper;
const id = '#apple-' + this.buttonConfig.button.wrapper;
if ( this.context === 'mini-cart' ) {
document
.querySelector( id_minicart )
?.addEventListener( 'click', ( evt ) => {
evt.preventDefault();
this.onButtonClick();
} );
} else {
document
.querySelector( id )
?.addEventListener( 'click', ( evt ) => {
evt.preventDefault();
this.onButtonClick();
} );
}
} );
} else {
jQuery( '#' + this.buttonConfig.button.wrapper ).hide();
jQuery( '#' + this.buttonConfig.button.mini_cart_wrapper ).hide();
jQuery( '#express-payment-method-ppcp-applepay' ).hide();
}
}
reinit() {
if ( ! this.applePayConfig ) {
return;
}
this.isInitialized = false;
this.init( this.applePayConfig );
}
async fetchTransactionInfo() {
this.transactionInfo = await this.contextHandler.transactionInfo();
}
/**
* Returns configurations relative to this button context.
*/
contextConfig() {
const config = {
wrapper: this.buttonConfig.button.wrapper,
ppcpStyle: this.ppcpConfig.button.style,
buttonStyle: this.buttonConfig.button.style,
ppcpButtonWrapper: this.ppcpConfig.button.wrapper,
};
if ( this.context === 'mini-cart' ) {
config.wrapper = this.buttonConfig.button.mini_cart_wrapper;
config.ppcpStyle = this.ppcpConfig.button.mini_cart_style;
config.buttonStyle = this.buttonConfig.button.mini_cart_style;
config.ppcpButtonWrapper = this.ppcpConfig.button.mini_cart_wrapper;
}
if (
[ 'cart-block', 'checkout-block' ].indexOf( this.context ) !== -1
) {
config.ppcpButtonWrapper =
'#express-payment-method-ppcp-gateway-paypal';
}
return config;
}
initEventHandlers() {
const { wrapper, ppcpButtonWrapper } = this.contextConfig();
const wrapper_id = '#' + wrapper;
if ( wrapper_id === ppcpButtonWrapper ) {
throw new Error(
`[ApplePayButton] "wrapper" and "ppcpButtonWrapper" values must differ to avoid infinite loop. Current value: "${ wrapper_id }"`
);
}
const syncButtonVisibility = () => {
if ( ! this.isEligible ) {
return;
}
const $ppcpButtonWrapper = jQuery( ppcpButtonWrapper );
setVisible( wrapper_id, $ppcpButtonWrapper.is( ':visible' ) );
setEnabled(
wrapper_id,
! $ppcpButtonWrapper.hasClass( 'ppcp-disabled' )
);
};
jQuery( document ).on(
'ppcp-shown ppcp-hidden ppcp-enabled ppcp-disabled',
( ev, data ) => {
if ( jQuery( data.selector ).is( ppcpButtonWrapper ) ) {
syncButtonVisibility();
}
}
);
syncButtonVisibility();
}
/**
* Starts an ApplePay session.
* @param paymentRequest
*/
applePaySession( paymentRequest ) {
this.log( 'applePaySession', paymentRequest );
const session = new ApplePaySession( 4, paymentRequest );
session.begin();
if ( this.shouldRequireShippingInButton() ) {
session.onshippingmethodselected =
this.onShippingMethodSelected( session );
session.onshippingcontactselected =
this.onShippingContactSelected( session );
}
session.onvalidatemerchant = this.onValidateMerchant( session );
session.onpaymentauthorized = this.onPaymentAuthorized( session );
return session;
}
/**
* Adds an Apple Pay purchase button.
*/
addButton() {
this.log( 'addButton', this.context );
const { wrapper, ppcpStyle } = this.contextConfig();
const appleContainer = document.getElementById( wrapper );
const type = this.buttonConfig.button.type;
const language = this.buttonConfig.button.lang;
const color = this.buttonConfig.button.color;
const id = 'apple-' + wrapper;
if ( appleContainer ) {
appleContainer.innerHTML = `<apple-pay-button id="${ id }" buttonstyle="${ color }" type="${ type }" locale="${ language }">`;
}
const $wrapper = jQuery( '#' + wrapper );
$wrapper.addClass( 'ppcp-button-' + ppcpStyle.shape );
if ( ppcpStyle.height ) {
$wrapper.css(
'--apple-pay-button-height',
`${ ppcpStyle.height }px`
);
$wrapper.css( 'height', `${ ppcpStyle.height }px` );
}
}
//------------------------
// Button click
//------------------------
/**
* Show Apple Pay payment sheet when Apple Pay payment button is clicked
*/
async onButtonClick() {
this.log( 'onButtonClick', this.context );
const paymentRequest = this.paymentRequest();
window.ppcpFundingSource = 'apple_pay'; // Do this on another place like on create order endpoint handler.
// Trigger woocommerce validation if we are in the checkout page.
if ( this.context === 'checkout' ) {
const checkoutFormSelector = 'form.woocommerce-checkout';
const errorHandler = new ErrorHandler(
PayPalCommerceGateway.labels.error.generic,
document.querySelector( '.woocommerce-notices-wrapper' )
);
try {
const formData = new FormData(
document.querySelector( checkoutFormSelector )
);
this.formData = Object.fromEntries( formData.entries() );
this.updateRequestDataWithForm( paymentRequest );
} catch ( error ) {
console.error( error );
}
this.log( '=== paymentRequest', paymentRequest );
const session = this.applePaySession( paymentRequest );
const formValidator =
PayPalCommerceGateway.early_checkout_validation_enabled
? new FormValidator(
PayPalCommerceGateway.ajax.validate_checkout.endpoint,
PayPalCommerceGateway.ajax.validate_checkout.nonce
)
: null;
if ( formValidator ) {
try {
const errors = await formValidator.validate(
document.querySelector( checkoutFormSelector )
);
if ( errors.length > 0 ) {
errorHandler.messages( errors );
jQuery( document.body ).trigger( 'checkout_error', [
errorHandler.currentHtml(),
] );
session.abort();
return;
}
} catch ( error ) {
console.error( error );
}
}
return;
}
// Default session initialization.
this.applePaySession( paymentRequest );
}
/**
* If the button should show the shipping fields.
*
* @return {false|*}
*/
shouldRequireShippingInButton() {
return (
this.contextHandler.shippingAllowed() &&
this.buttonConfig.product.needShipping &&
( this.context !== 'checkout' ||
this.shouldUpdateButtonWithFormData() )
);
}
/**
* If the button should be updated with the form addresses.
*
* @return {boolean}
*/
shouldUpdateButtonWithFormData() {
if ( this.context !== 'checkout' ) {
return false;
}
return (
this.buttonConfig?.preferences?.checkout_data_mode ===
'use_applepay'
);
}
/**
* Indicates how payment completion should be handled if with the context handler default actions.
* Or with ApplePay module specific completion.
*
* @return {boolean}
*/
shouldCompletePaymentWithContextHandler() {
// Data already handled, ex: PayNow
if ( ! this.contextHandler.shippingAllowed() ) {
return true;
}
// Use WC form data mode in Checkout.
if (
this.context === 'checkout' &&
! this.shouldUpdateButtonWithFormData()
) {
return true;
}
return false;
}
/**
* Updates ApplePay paymentRequest with form data.
* @param paymentRequest
*/
updateRequestDataWithForm( paymentRequest ) {
if ( ! this.shouldUpdateButtonWithFormData() ) {
return;
}
// Add billing address.
paymentRequest.billingContact = this.fillBillingContact(
this.formData
);
// Add custom data.
// "applicationData" is originating a "PayPalApplePayError: An internal server error has occurred" on paypal.Applepay().confirmOrder().
// paymentRequest.applicationData = this.fillApplicationData(this.formData);
if ( ! this.shouldRequireShippingInButton() ) {
return;
}
// Add shipping address.
paymentRequest.shippingContact = this.fillShippingContact(
this.formData
);
// Get shipping methods.
const rate = this.transactionInfo.chosenShippingMethods[ 0 ];
paymentRequest.shippingMethods = [];
// Add selected shipping method.
for ( const shippingPackage of this.transactionInfo.shippingPackages ) {
if ( rate === shippingPackage.id ) {
const shippingMethod = {
label: shippingPackage.label,
detail: '',
amount: shippingPackage.cost_str,
identifier: shippingPackage.id,
};
// Remember this shipping method as the selected one.
this.selectedShippingMethod = shippingMethod;
paymentRequest.shippingMethods.push( shippingMethod );
break;
}
}
// Add other shipping methods.
for ( const shippingPackage of this.transactionInfo.shippingPackages ) {
if ( rate !== shippingPackage.id ) {
paymentRequest.shippingMethods.push( {
label: shippingPackage.label,
detail: '',
amount: shippingPackage.cost_str,
identifier: shippingPackage.id,
} );
}
}
// Store for reuse in case this data is not provided by ApplePay on authorization.
this.initialPaymentRequest = paymentRequest;
this.log(
'=== paymentRequest.shippingMethods',
paymentRequest.shippingMethods
);
}
paymentRequest() {
const applepayConfig = this.applePayConfig;
const buttonConfig = this.buttonConfig;
const baseRequest = {
countryCode: applepayConfig.countryCode,
merchantCapabilities: applepayConfig.merchantCapabilities,
supportedNetworks: applepayConfig.supportedNetworks,
requiredShippingContactFields: [
'postalAddress',
'email',
'phone',
],
requiredBillingContactFields: [ 'postalAddress' ], // ApplePay does not implement billing email and phone fields.
};
if ( ! this.shouldRequireShippingInButton() ) {
if ( this.shouldCompletePaymentWithContextHandler() ) {
// Data needs handled externally.
baseRequest.requiredShippingContactFields = [];
} else {
// Minimum data required for order creation.
baseRequest.requiredShippingContactFields = [
'email',
'phone',
];
}
}
const paymentRequest = Object.assign( {}, baseRequest );
paymentRequest.currencyCode = buttonConfig.shop.currencyCode;
paymentRequest.total = {
label: buttonConfig.shop.totalLabel,
type: 'final',
amount: this.transactionInfo.totalPrice,
};
return paymentRequest;
}
refreshContextData() {
switch ( this.context ) {
case 'product':
// Refresh product data that makes the price change.
this.productQuantity =
document.querySelector( 'input.qty' )?.value;
this.products = this.contextHandler.products();
this.log( 'Products updated', this.products );
break;
}
}
//------------------------
// Payment process
//------------------------
onValidateMerchant( session ) {
this.log( 'onvalidatemerchant', this.buttonConfig.ajax_url );
return ( applePayValidateMerchantEvent ) => {
this.log( 'onvalidatemerchant call' );
widgetBuilder.paypal
.Applepay()
.validateMerchant( {
validationUrl: applePayValidateMerchantEvent.validationURL,
} )
.then( ( validateResult ) => {
this.log( 'onvalidatemerchant ok' );
session.completeMerchantValidation(
validateResult.merchantSession
);
//call backend to update validation to true
jQuery.ajax( {
url: this.buttonConfig.ajax_url,
type: 'POST',
data: {
action: 'ppcp_validate',
validation: true,
'woocommerce-process-checkout-nonce': this.nonce,
},
} );
} )
.catch( ( validateError ) => {
this.log( 'onvalidatemerchant error', validateError );
console.error( validateError );
//call backend to update validation to false
jQuery.ajax( {
url: this.buttonConfig.ajax_url,
type: 'POST',
data: {
action: 'ppcp_validate',
validation: false,
'woocommerce-process-checkout-nonce': this.nonce,
},
} );
this.log( 'onvalidatemerchant session abort' );
session.abort();
} );
};
}
onShippingMethodSelected( session ) {
this.log( 'onshippingmethodselected', this.buttonConfig.ajax_url );
const ajax_url = this.buttonConfig.ajax_url;
return ( event ) => {
this.log( 'onshippingmethodselected call' );
const data = this.getShippingMethodData( event );
jQuery.ajax( {
url: ajax_url,
method: 'POST',
data,
success: (
applePayShippingMethodUpdate,
textStatus,
jqXHR
) => {
this.log( 'onshippingmethodselected ok' );
const response = applePayShippingMethodUpdate.data;
if ( applePayShippingMethodUpdate.success === false ) {
response.errors = createAppleErrors( response.errors );
}
this.selectedShippingMethod = event.shippingMethod;
// Sort the response shipping methods, so that the selected shipping method is the first one.
response.newShippingMethods =
response.newShippingMethods.sort( ( a, b ) => {
if (
a.label === this.selectedShippingMethod.label
) {
return -1;
}
return 1;
} );
if ( applePayShippingMethodUpdate.success === false ) {
response.errors = createAppleErrors( response.errors );
}
session.completeShippingMethodSelection( response );
},
error: ( jqXHR, textStatus, errorThrown ) => {
this.log( 'onshippingmethodselected error', textStatus );
console.warn( textStatus, errorThrown );
session.abort();
},
} );
};
}
onShippingContactSelected( session ) {
this.log( 'onshippingcontactselected', this.buttonConfig.ajax_url );
const ajax_url = this.buttonConfig.ajax_url;
return ( event ) => {
this.log( 'onshippingcontactselected call' );
const data = this.getShippingContactData( event );
jQuery.ajax( {
url: ajax_url,
method: 'POST',
data,
success: (
applePayShippingContactUpdate,
textStatus,
jqXHR
) => {
this.log( 'onshippingcontactselected ok' );
const response = applePayShippingContactUpdate.data;
this.updatedContactInfo = event.shippingContact;
if ( applePayShippingContactUpdate.success === false ) {
response.errors = createAppleErrors( response.errors );
}
if ( response.newShippingMethods ) {
this.selectedShippingMethod =
response.newShippingMethods[ 0 ];
}
session.completeShippingContactSelection( response );
},
error: ( jqXHR, textStatus, errorThrown ) => {
this.log( 'onshippingcontactselected error', textStatus );
console.warn( textStatus, errorThrown );
session.abort();
},
} );
};
}
getShippingContactData( event ) {
const product_id = this.buttonConfig.product.id;
this.refreshContextData();
switch ( this.context ) {
case 'product':
return {
action: 'ppcp_update_shipping_contact',
product_id,
products: JSON.stringify( this.products ),
caller_page: 'productDetail',
product_quantity: this.productQuantity,
simplified_contact: event.shippingContact,
need_shipping: this.shouldRequireShippingInButton(),
'woocommerce-process-checkout-nonce': this.nonce,
};
case 'cart':
case 'checkout':
case 'cart-block':
case 'checkout-block':
case 'mini-cart':
return {
action: 'ppcp_update_shipping_contact',
simplified_contact: event.shippingContact,
caller_page: 'cart',
need_shipping: this.shouldRequireShippingInButton(),
'woocommerce-process-checkout-nonce': this.nonce,
};
}
}
getShippingMethodData( event ) {
const product_id = this.buttonConfig.product.id;
this.refreshContextData();
switch ( this.context ) {
case 'product':
return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
simplified_contact: this.hasValidContactInfo(
this.updatedContactInfo
)
? this.updatedContactInfo
: this.initialPaymentRequest?.shippingContact ??
this.initialPaymentRequest?.billingContact,
product_id,
products: JSON.stringify( this.products ),
caller_page: 'productDetail',
product_quantity: this.productQuantity,
'woocommerce-process-checkout-nonce': this.nonce,
};
case 'cart':
case 'checkout':
case 'cart-block':
case 'checkout-block':
case 'mini-cart':
return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
simplified_contact: this.hasValidContactInfo(
this.updatedContactInfo
)
? this.updatedContactInfo
: this.initialPaymentRequest?.shippingContact ??
this.initialPaymentRequest?.billingContact,
caller_page: 'cart',
'woocommerce-process-checkout-nonce': this.nonce,
};
}
}
onPaymentAuthorized( session ) {
this.log( 'onpaymentauthorized' );
return async ( event ) => {
this.log( 'onpaymentauthorized call' );
function form() {
return document.querySelector( 'form.cart' );
}
const processInWooAndCapture = async ( data ) => {
return new Promise( ( resolve, reject ) => {
try {
const billingContact =
data.billing_contact ||
this.initialPaymentRequest.billingContact;
const shippingContact =
data.shipping_contact ||
this.initialPaymentRequest.shippingContact;
const shippingMethod =
this.selectedShippingMethod ||
( this.initialPaymentRequest.shippingMethods ||
[] )[ 0 ];
const request_data = {
action: 'ppcp_create_order',
caller_page: this.context,
product_id: this.buttonConfig.product.id ?? null,
products: JSON.stringify( this.products ),
product_quantity: this.productQuantity ?? null,
shipping_contact: shippingContact,
billing_contact: billingContact,
token: event.payment.token,
shipping_method: shippingMethod,
'woocommerce-process-checkout-nonce': this.nonce,
funding_source: 'applepay',
_wp_http_referer: '/?wc-ajax=update_order_review',
paypal_order_id: data.paypal_order_id,
};
this.log(
'onpaymentauthorized request',
this.buttonConfig.ajax_url,
data
);
jQuery.ajax( {
url: this.buttonConfig.ajax_url,
method: 'POST',
data: request_data,
complete: ( jqXHR, textStatus ) => {
this.log( 'onpaymentauthorized complete' );
},
success: (
authorizationResult,
textStatus,
jqXHR
) => {
this.log( 'onpaymentauthorized ok' );
resolve( authorizationResult );
},
error: ( jqXHR, textStatus, errorThrown ) => {
this.log(
'onpaymentauthorized error',
textStatus
);
reject( new Error( errorThrown ) );
},
} );
} catch ( error ) {
this.log( 'onpaymentauthorized catch', error );
console.log( error ); // handle error
}
} );
};
const id = await this.contextHandler.createOrder();
this.log(
'onpaymentauthorized paypal order ID',
id,
event.payment.token,
event.payment.billingContact
);
try {
const confirmOrderResponse = await widgetBuilder.paypal
.Applepay()
.confirmOrder( {
orderId: id,
token: event.payment.token,
billingContact: event.payment.billingContact,
} );
this.log(
'onpaymentauthorized confirmOrderResponse',
confirmOrderResponse
);
if (
confirmOrderResponse &&
confirmOrderResponse.approveApplePayPayment
) {
if (
confirmOrderResponse.approveApplePayPayment.status ===
'APPROVED'
) {
try {
if (
this.shouldCompletePaymentWithContextHandler()
) {
// No shipping, expect immediate capture, ex: PayNow, Checkout with form data.
let approveFailed = false;
await this.contextHandler.approveOrder(
{
orderID: id,
},
{
// actions mock object.
restart: () =>
new Promise(
( resolve, reject ) => {
approveFailed = true;
resolve();
}
),
order: {
get: () =>
new Promise(
( resolve, reject ) => {
resolve( null );
}
),
},
}
);
if ( ! approveFailed ) {
this.log(
'onpaymentauthorized approveOrder OK'
);
session.completePayment(
ApplePaySession.STATUS_SUCCESS
);
} else {
this.log(
'onpaymentauthorized approveOrder FAIL'
);
session.completePayment(
ApplePaySession.STATUS_FAILURE
);
session.abort();
console.error( error );
}
} else {
// Default payment.
const data = {
billing_contact:
event.payment.billingContact,
shipping_contact:
event.payment.shippingContact,
paypal_order_id: id,
};
const authorizationResult =
await processInWooAndCapture( data );
if (
authorizationResult.result === 'success'
) {
session.completePayment(
ApplePaySession.STATUS_SUCCESS
);
window.location.href =
authorizationResult.redirect;
} else {
session.completePayment(
ApplePaySession.STATUS_FAILURE
);
}
}
} catch ( error ) {
session.completePayment(
ApplePaySession.STATUS_FAILURE
);
session.abort();
console.error( error );
}
} else {
console.error( 'Error status is not APPROVED' );
session.completePayment(
ApplePaySession.STATUS_FAILURE
);
}
} else {
console.error( 'Invalid confirmOrderResponse' );
session.completePayment( ApplePaySession.STATUS_FAILURE );
}
} catch ( error ) {
console.error(
'Error confirming order with applepay token',
error
);
session.completePayment( ApplePaySession.STATUS_FAILURE );
session.abort();
}
};
}
fillBillingContact( data ) {
return {
givenName: data.billing_first_name ?? '',
familyName: data.billing_last_name ?? '',
emailAddress: data.billing_email ?? '',
phoneNumber: data.billing_phone ?? '',
addressLines: [ data.billing_address_1, data.billing_address_2 ],
locality: data.billing_city ?? '',
postalCode: data.billing_postcode ?? '',
countryCode: data.billing_country ?? '',
administrativeArea: data.billing_state ?? '',
};
}
fillShippingContact( data ) {
if ( data.shipping_first_name === '' ) {
return this.fillBillingContact( data );
}
return {
givenName:
data?.shipping_first_name && data.shipping_first_name !== ''
? data.shipping_first_name
: data?.billing_first_name,
familyName:
data?.shipping_last_name && data.shipping_last_name !== ''
? data.shipping_last_name
: data?.billing_last_name,
emailAddress:
data?.shipping_email && data.shipping_email !== ''
? data.shipping_email
: data?.billing_email,
phoneNumber:
data?.shipping_phone && data.shipping_phone !== ''
? data.shipping_phone
: data?.billing_phone,
addressLines: [
data.shipping_address_1 ?? '',
data.shipping_address_2 ?? '',
],
locality:
data?.shipping_city && data.shipping_city !== ''
? data.shipping_city
: data?.billing_city,
postalCode:
data?.shipping_postcode && data.shipping_postcode !== ''
? data.shipping_postcode
: data?.billing_postcode,
countryCode:
data?.shipping_country && data.shipping_country !== ''
? data.shipping_country
: data?.billing_country,
administrativeArea:
data?.shipping_state && data.shipping_state !== ''
? data.shipping_state
: data?.billing_state,
};
}
fillApplicationData( data ) {
const jsonString = JSON.stringify( data );
const utf8Str = encodeURIComponent( jsonString ).replace(
/%([0-9A-F]{2})/g,
( match, p1 ) => {
return String.fromCharCode( '0x' + p1 );
}
);
return btoa( utf8Str );
}
hasValidContactInfo( value ) {
return Array.isArray( value )
? value.length > 0
: Object.keys( value || {} ).length > 0;
}
}
export default ApplepayButton;

View file

@ -0,0 +1,52 @@
import buttonModuleWatcher from '../../../ppcp-button/resources/js/modules/ButtonModuleWatcher';
import ApplepayButton from './ApplepayButton';
class ApplepayManager {
constructor( buttonConfig, ppcpConfig ) {
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.ApplePayConfig = null;
this.buttons = [];
buttonModuleWatcher.watchContextBootstrap( ( bootstrap ) => {
const button = new ApplepayButton(
bootstrap.context,
bootstrap.handler,
buttonConfig,
ppcpConfig
);
this.buttons.push( button );
if ( this.ApplePayConfig ) {
button.init( this.ApplePayConfig );
}
} );
}
init() {
( async () => {
await this.config();
for ( const button of this.buttons ) {
button.init( this.ApplePayConfig );
}
} )();
}
reinit() {
for ( const button of this.buttons ) {
button.reinit();
}
}
/**
* Gets ApplePay configuration of the PayPal merchant.
* @return {Promise<null>}
*/
async config() {
this.ApplePayConfig = await paypal.Applepay().config();
return this.ApplePayConfig;
}
}
export default ApplepayManager;

View file

@ -0,0 +1,34 @@
import ApplepayButton from './ApplepayButton';
class ApplepayManagerBlockEditor {
constructor( buttonConfig, ppcpConfig ) {
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.applePayConfig = null;
}
init() {
( async () => {
await this.config();
} )();
}
async config() {
try {
this.applePayConfig = await paypal.Applepay().config();
const button = new ApplepayButton(
this.ppcpConfig.context,
null,
this.buttonConfig,
this.ppcpConfig
);
button.init( this.applePayConfig );
} catch ( error ) {
console.error( 'Failed to initialize Apple Pay:', error );
}
}
}
export default ApplepayManagerBlockEditor;

View file

@ -0,0 +1,80 @@
import ErrorHandler from '../../../../ppcp-button/resources/js/modules/ErrorHandler';
import CartActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler';
class BaseHandler {
constructor( buttonConfig, ppcpConfig ) {
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
}
isVaultV3Mode() {
return (
this.ppcpConfig?.save_payment_methods?.id_token && // vault v3
! this.ppcpConfig?.data_client_id?.paypal_subscriptions_enabled && // not PayPal Subscriptions mode
this.ppcpConfig?.can_save_vault_token
); // vault is enabled
}
validateContext() {
if ( this.ppcpConfig?.locations_with_subscription_product?.cart ) {
return this.isVaultV3Mode();
}
return true;
}
shippingAllowed() {
return this.buttonConfig.product.needShipping;
}
transactionInfo() {
return new Promise( ( resolve, reject ) => {
const endpoint = this.ppcpConfig.ajax.cart_script_params.endpoint;
const separator = endpoint.indexOf( '?' ) !== -1 ? '&' : '?';
fetch( endpoint + separator + 'shipping=1', {
method: 'GET',
credentials: 'same-origin',
} )
.then( ( result ) => result.json() )
.then( ( result ) => {
if ( ! result.success ) {
return;
}
// handle script reload
const data = result.data;
resolve( {
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.total_str,
chosenShippingMethods:
data.chosen_shipping_methods || null,
shippingPackages: data.shipping_packages || null,
} );
} );
} );
}
createOrder() {
return this.actionHandler().configuration().createOrder( null, null );
}
approveOrder( data, actions ) {
return this.actionHandler().configuration().onApprove( data, actions );
}
actionHandler() {
return new CartActionHandler( this.ppcpConfig, this.errorHandler() );
}
errorHandler() {
return new ErrorHandler(
this.ppcpConfig.labels.error.generic,
document.querySelector( '.woocommerce-notices-wrapper' )
);
}
}
export default BaseHandler;

View file

@ -0,0 +1,5 @@
import BaseHandler from './BaseHandler';
class CartBlockHandler extends BaseHandler {}
export default CartBlockHandler;

View file

@ -0,0 +1,5 @@
import BaseHandler from './BaseHandler';
class CartHandler extends BaseHandler {}
export default CartHandler;

View file

@ -0,0 +1,5 @@
import BaseHandler from './BaseHandler';
class CheckoutBlockHandler extends BaseHandler {}
export default CheckoutBlockHandler;

View file

@ -0,0 +1,15 @@
import Spinner from '../../../../ppcp-button/resources/js/modules/Helper/Spinner';
import CheckoutActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler';
import BaseHandler from './BaseHandler';
class CheckoutHandler extends BaseHandler {
actionHandler() {
return new CheckoutActionHandler(
this.ppcpConfig,
this.errorHandler(),
new Spinner()
);
}
}
export default CheckoutHandler;

View file

@ -0,0 +1,33 @@
import SingleProductHandler from './SingleProductHandler';
import CartHandler from './CartHandler';
import CheckoutHandler from './CheckoutHandler';
import CartBlockHandler from './CartBlockHandler';
import CheckoutBlockHandler from './CheckoutBlockHandler';
import MiniCartHandler from './MiniCartHandler';
import PreviewHandler from './PreviewHandler';
import PayNowHandler from './PayNowHandler';
class ContextHandlerFactory {
static create( context, buttonConfig, ppcpConfig ) {
switch ( context ) {
case 'product':
return new SingleProductHandler( buttonConfig, ppcpConfig );
case 'cart':
return new CartHandler( buttonConfig, ppcpConfig );
case 'checkout':
return new CheckoutHandler( buttonConfig, ppcpConfig );
case 'pay-now':
return new PayNowHandler( buttonConfig, ppcpConfig );
case 'mini-cart':
return new MiniCartHandler( buttonConfig, ppcpConfig );
case 'cart-block':
return new CartBlockHandler( buttonConfig, ppcpConfig );
case 'checkout-block':
return new CheckoutBlockHandler( buttonConfig, ppcpConfig );
case 'preview':
return new PreviewHandler( buttonConfig, ppcpConfig );
}
}
}
export default ContextHandlerFactory;

View file

@ -0,0 +1,5 @@
import BaseHandler from './BaseHandler';
class MiniCartHandler extends BaseHandler {}
export default MiniCartHandler;

View file

@ -0,0 +1,35 @@
import Spinner from '../../../../ppcp-button/resources/js/modules/Helper/Spinner';
import BaseHandler from './BaseHandler';
import CheckoutActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler';
class PayNowHandler extends BaseHandler {
validateContext() {
if ( this.ppcpConfig?.locations_with_subscription_product?.payorder ) {
return this.isVaultV3Mode();
}
return true;
}
transactionInfo() {
return new Promise( async ( resolve, reject ) => {
const data = this.ppcpConfig.pay_now;
resolve( {
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.total_str,
} );
} );
}
actionHandler() {
return new CheckoutActionHandler(
this.ppcpConfig,
this.errorHandler(),
new Spinner()
);
}
}
export default PayNowHandler;

View file

@ -0,0 +1,35 @@
import BaseHandler from './BaseHandler';
class PreviewHandler extends BaseHandler {
constructor( buttonConfig, ppcpConfig, externalHandler ) {
super( buttonConfig, ppcpConfig, externalHandler );
}
transactionInfo() {
// We need to return something as ApplePay button initialization expects valid data.
return {
countryCode: 'US',
currencyCode: 'USD',
totalPrice: '10.00',
totalPriceStatus: 'FINAL',
};
}
createOrder() {
throw new Error( 'Create order fail. This is just a preview.' );
}
approveOrder( data, actions ) {
throw new Error( 'Approve order fail. This is just a preview.' );
}
actionHandler() {
throw new Error( 'Action handler fail. This is just a preview.' );
}
errorHandler() {
throw new Error( 'Error handler fail. This is just a preview.' );
}
}
export default PreviewHandler;

View file

@ -0,0 +1,82 @@
import SingleProductActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler';
import SimulateCart from '../../../../ppcp-button/resources/js/modules/Helper/SimulateCart';
import ErrorHandler from '../../../../ppcp-button/resources/js/modules/ErrorHandler';
import UpdateCart from '../../../../ppcp-button/resources/js/modules/Helper/UpdateCart';
import BaseHandler from './BaseHandler';
class SingleProductHandler extends BaseHandler {
validateContext() {
if ( this.ppcpConfig?.locations_with_subscription_product?.product ) {
return this.isVaultV3Mode();
}
return true;
}
transactionInfo() {
const errorHandler = new ErrorHandler(
this.ppcpConfig.labels.error.generic,
document.querySelector( '.woocommerce-notices-wrapper' )
);
function form() {
return document.querySelector( 'form.cart' );
}
const actionHandler = new SingleProductActionHandler(
null,
null,
form(),
errorHandler
);
const hasSubscriptions =
PayPalCommerceGateway.data_client_id.has_subscriptions &&
PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled;
const products = hasSubscriptions
? actionHandler.getSubscriptionProducts()
: actionHandler.getProducts();
return new Promise( ( resolve, reject ) => {
new SimulateCart(
this.ppcpConfig.ajax.simulate_cart.endpoint,
this.ppcpConfig.ajax.simulate_cart.nonce
).simulate( ( data ) => {
resolve( {
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.total_str,
} );
}, products );
} );
}
createOrder() {
return this.actionHandler()
.configuration()
.createOrder( null, null, {
updateCartOptions: {
keepShipping: true,
},
} );
}
actionHandler() {
return new SingleProductActionHandler(
this.ppcpConfig,
new UpdateCart(
this.ppcpConfig.ajax.change_cart.endpoint,
this.ppcpConfig.ajax.change_cart.nonce
),
document.querySelector( 'form.cart' ),
this.errorHandler()
);
}
products() {
return this.actionHandler().getProducts();
}
}
export default SingleProductHandler;

View file

@ -0,0 +1,12 @@
export function createAppleErrors( errors ) {
const errorList = [];
for ( const error of errors ) {
const { contactField = null, code = null, message = null } = error;
const appleError = contactField
? new ApplePayError( code, contactField, message )
: new ApplePayError( code );
errorList.push( appleError );
}
return errorList;
}

Some files were not shown because too many files have changed in this diff Show more