Merge branch 'trunk' into PCP-892-combine-webhooks-status-page-into-new-connection-tab

# Conflicts:
#	modules/ppcp-wc-gateway/connection-tab-settings.php
This commit is contained in:
Narek Zakarian 2022-09-06 19:15:24 +04:00
commit 58aa50d147
17 changed files with 1008 additions and 1046 deletions

View file

@ -12,8 +12,16 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use RuntimeException;
use stdClass;
use WC_Customer;
use WC_Order;
use WC_Order_Item_Fee;
use WC_Order_Item_Product;
use WC_Product;
use WC_Tax;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
@ -92,18 +100,19 @@ class PayUponInvoiceOrderEndpoint {
*
* @param PurchaseUnit[] $items The purchase unit items for the order.
* @param PaymentSource $payment_source The payment source.
* @param WC_Order $wc_order The WC order.
* @return Order
* @throws RuntimeException When there is a problem with the payment source.
* @throws PayPalApiException When there is a problem creating the order.
*/
public function create( array $items, PaymentSource $payment_source ): Order {
public function create( array $items, PaymentSource $payment_source, WC_Order $wc_order ): Order {
$data = array(
'intent' => 'CAPTURE',
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
'purchase_units' => array_map(
static function ( PurchaseUnit $item ): array {
return $item->to_array();
return $item->to_array( false );
},
$items
),
@ -112,8 +121,7 @@ class PayUponInvoiceOrderEndpoint {
),
);
$data = $this->ensure_tax( $data );
$data = $this->ensure_tax_rate( $data );
$data = $this->ensure_taxes( $wc_order, $data );
$data = $this->ensure_shipping( $data, $payment_source->to_array() );
$bearer = $this->bearer->bearer();
@ -195,45 +203,6 @@ class PayUponInvoiceOrderEndpoint {
return $json;
}
/**
* Ensures items contains tax.
*
* @param array $data The data.
* @return array
*/
private function ensure_tax( array $data ): array {
$items_count = count( $data['purchase_units'][0]['items'] );
for ( $i = 0; $i < $items_count; $i++ ) {
if ( ! isset( $data['purchase_units'][0]['items'][ $i ]['tax'] ) ) {
$data['purchase_units'][0]['items'][ $i ]['tax'] = array(
'currency_code' => 'EUR',
'value' => '0.00',
);
}
}
return $data;
}
/**
* Ensures items contains tax rate.
*
* @param array $data The data.
* @return array
*/
private function ensure_tax_rate( array $data ): array {
$items_count = count( $data['purchase_units'][0]['items'] );
for ( $i = 0; $i < $items_count; $i++ ) {
if ( ! isset( $data['purchase_units'][0]['items'][ $i ]['tax_rate'] ) ) {
$data['purchase_units'][0]['items'][ $i ]['tax_rate'] = '0.00';
}
}
return $data;
}
/**
* Ensures purchase units contains shipping by using payment source data.
*
@ -255,4 +224,51 @@ class PayUponInvoiceOrderEndpoint {
return $data;
}
/**
* Ensure items contains taxes.
*
* @param WC_Order $wc_order The WC order.
* @param array $data The data.
* @return array
*/
private function ensure_taxes( WC_Order $wc_order, array $data ): array {
$tax_total = $data['purchase_units'][0]['amount']['breakdown']['tax_total']['value'];
$item_total = $data['purchase_units'][0]['amount']['breakdown']['item_total']['value'];
$shipping = $data['purchase_units'][0]['amount']['breakdown']['shipping']['value'];
$order_tax_total = $wc_order->get_total_tax();
$tax_rate = round( ( $order_tax_total / $item_total ) * 100, 1 );
$item_name = $data['purchase_units'][0]['items'][0]['name'];
$item_currency = $data['purchase_units'][0]['items'][0]['unit_amount']['currency_code'];
$item_description = $data['purchase_units'][0]['items'][0]['description'];
$item_sku = $data['purchase_units'][0]['items'][0]['sku'];
unset( $data['purchase_units'][0]['items'] );
$data['purchase_units'][0]['items'][0] = array(
'name' => $item_name,
'unit_amount' => array(
'currency_code' => $item_currency,
'value' => $item_total,
),
'quantity' => 1,
'description' => $item_description,
'sku' => $item_sku,
'category' => 'PHYSICAL_GOODS',
'tax' => array(
'currency_code' => 'EUR',
'value' => $tax_total,
),
'tax_rate' => number_format( $tax_rate, 2, '.', '' ),
);
$total_amount = $data['purchase_units'][0]['amount']['value'];
$breakdown_total = $item_total + $tax_total + $shipping;
$diff = round( $total_amount - $breakdown_total, 2 );
if ( $diff === -0.01 || $diff === 0.01 ) {
$data['purchase_units'][0]['amount']['value'] = number_format( $breakdown_total, 2, '.', '' );
}
return $data;
}
}

View file

@ -268,9 +268,11 @@ class PurchaseUnit {
/**
* Returns the object as array.
*
* @param bool $ditch_items_when_mismatch Whether ditch items when mismatch or not.
*
* @return array
*/
public function to_array(): array {
public function to_array( bool $ditch_items_when_mismatch = true ): array {
$purchase_unit = array(
'reference_id' => $this->reference_id(),
'amount' => $this->amount()->to_array(),
@ -282,7 +284,7 @@ class PurchaseUnit {
$this->items()
),
);
if ( $this->ditch_items_when_mismatch( $this->amount(), ...$this->items() ) ) {
if ( $ditch_items_when_mismatch && $this->ditch_items_when_mismatch( $this->amount(), ...$this->items() ) ) {
unset( $purchase_unit['items'] );
unset( $purchase_unit['amount']['breakdown'] );
}

View file

@ -1,5 +0,0 @@
{
"plugins": [
"babel-plugin-transform-object-rest-spread"
]
}

View file

@ -7,16 +7,15 @@
"deepmerge": "^4.2.2"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"babel-loader": "^8.1.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"@babel/core": "^7.19",
"@babel/preset-env": "^7.19",
"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.55.0",
"webpack-cli": "^4.8.0"
"webpack": "^5.74",
"webpack-cli": "^4.10"
},
"scripts": {
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",

File diff suppressed because it is too large Load diff

View file

@ -247,6 +247,8 @@ function ppcp_onboarding_productionCallback(...args) {
}
);
sandboxSwitchElement.checked = ! sandboxSwitchElement.checked;
isDisconnecting = true;
document.querySelector('.woocommerce-save-button').click();

View file

@ -78,7 +78,7 @@ class MetaBoxRenderer {
return;
}
$tracking_info = $this->order_tracking_endpoint->get_tracking_information( $post->ID );
$tracking_info = $this->order_tracking_endpoint->get_tracking_information( $wc_order->get_id() );
$tracking_is_not_added = empty( $tracking_info );

View file

@ -357,7 +357,7 @@ return function ( ContainerInterface $container, array $fields ): array {
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'requirements' => array( 'dcc' ),
'gateway' => Settings::CONNECTION_TAB_ID,
),
'ppcp_pui_status' => array(

View file

@ -2144,13 +2144,13 @@ return array(
return 'https://www.paypal.com/bizsignup/entry?country.x=DE&product=payment_methods&capabilities=PAY_UPON_INVOICE';
},
'wcgateway.settings.connection.dcc-status-text' => static function ( ContainerInterface $container ): string {
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
assert( $dcc_applies instanceof DccApplies );
$dcc_product_status = $container->get( 'wcgateway.helper.dcc-product-status' );
assert( $dcc_product_status instanceof DCCProductStatus );
$environment = $container->get( 'onboarding.environment' );
assert( $environment instanceof Environment );
$dcc_enabled = $dcc_applies->for_country_currency() || $dcc_applies->for_wc_payments();
$dcc_enabled = $dcc_product_status->dcc_is_active();
$enabled_status_text = esc_html__( 'Status: Enabled', 'woocommerce-paypal-payments' );
$disabled_status_text = esc_html__( 'Status: Not yet enabled', 'woocommerce-paypal-payments' );
@ -2183,9 +2183,7 @@ return array(
$environment = $container->get( 'onboarding.environment' );
assert( $environment instanceof Environment );
$shop_country = $container->get( 'api.shop.country' );
$pui_enabled = 'DE' === $shop_country && $pui_product_status->pui_is_active();
$pui_enabled = $pui_product_status->pui_is_active();
$enabled_status_text = esc_html__( 'Status: Enabled', 'woocommerce-paypal-payments' );
$disabled_status_text = esc_html__( 'Status: Not yet enabled', 'woocommerce-paypal-payments' );

View file

@ -226,7 +226,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
$payment_source = $this->payment_source_factory->from_wc_order( $wc_order, $birth_date );
try {
$order = $this->order_endpoint->create( array( $purchase_unit ), $payment_source );
$order = $this->order_endpoint->create( array( $purchase_unit ), $payment_source, $wc_order );
$this->add_paypal_meta( $wc_order, $order, $this->environment );
as_schedule_single_action(

View file

@ -62,6 +62,7 @@ class DCCProductStatus {
if ( is_bool( $this->current_status_cache ) ) {
return $this->current_status_cache;
}
if ( $this->settings->has( 'products_dcc_enabled' ) && $this->settings->get( 'products_dcc_enabled' ) ) {
$this->current_status_cache = true;
return true;

View file

@ -174,7 +174,7 @@ class SettingsListener {
/**
* The URL opened at the end of onboarding after saving the merchant ID/email.
*/
$redirect_url = apply_filters( 'woocommerce_paypal_payments_onboarding_redirect_url', admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' ) );
$redirect_url = apply_filters( 'woocommerce_paypal_payments_onboarding_redirect_url', admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway&ppcp-tab=ppcp-connection' ) );
if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) {
$redirect_url = add_query_arg( 'ppcp-onboarding-error', '1', $redirect_url );
}
@ -259,7 +259,7 @@ class SettingsListener {
$credentials_change_status = null; // Cannot detect on Card Processing page.
if ( PayPalGateway::ID === $this->page_id ) {
if ( PayPalGateway::ID === $this->page_id || Settings::CONNECTION_TAB_ID === $this->page_id ) {
$settings['enabled'] = isset( $_POST['woocommerce_ppcp-gateway_enabled'] )
&& 1 === absint( $_POST['woocommerce_ppcp-gateway_enabled'] );
@ -267,7 +267,6 @@ class SettingsListener {
}
// phpcs:enable phpcs:disable WordPress.Security.NonceVerification.Missing
// phpcs:enable phpcs:disable WordPress.Security.NonceVerification.Missing
if ( $credentials_change_status ) {
if ( self::CREDENTIALS_UNCHANGED !== $credentials_change_status ) {
$this->settings->set( 'products_dcc_enabled', null );

View file

@ -390,12 +390,6 @@ $data_rows_html
) {
continue;
}
if (
in_array( 'dcc', $config['requirements'], true )
&& ! $this->dcc_product_status->dcc_is_active()
) {
continue;
}
if (
in_array( 'messages', $config['requirements'], true )
&& ! $this->messages_apply->for_country()