2020-09-01 09:00:45 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The API module.
|
|
|
|
*
|
2020-09-11 14:11:10 +03:00
|
|
|
* @package WooCommerce\PayPalCommerce\ApiClient
|
2020-09-01 09:00:45 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
namespace WooCommerce\PayPalCommerce\ApiClient;
|
2020-09-01 09:00:45 +03:00
|
|
|
|
2023-08-08 15:20:40 +01:00
|
|
|
use WC_Order;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderTransient;
|
2022-11-09 10:11:31 +02:00
|
|
|
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;
|
2023-08-08 15:20:40 +01:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
2020-09-01 09:00:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ApiModule
|
|
|
|
*/
|
|
|
|
class ApiModule implements ModuleInterface {
|
|
|
|
|
|
|
|
/**
|
2021-08-30 08:08:41 +02:00
|
|
|
* {@inheritDoc}
|
2020-09-01 09:00:45 +03:00
|
|
|
*/
|
|
|
|
public function setup(): ServiceProviderInterface {
|
|
|
|
return new ServiceProvider(
|
|
|
|
require __DIR__ . '/../services.php',
|
|
|
|
require __DIR__ . '/../extensions.php'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-30 08:08:41 +02:00
|
|
|
* {@inheritDoc}
|
2020-09-01 09:00:45 +03:00
|
|
|
*/
|
2021-08-30 08:10:43 +02:00
|
|
|
public function run( ContainerInterface $c ): void {
|
2021-08-13 16:27:52 +02:00
|
|
|
add_action(
|
|
|
|
'woocommerce_after_calculate_totals',
|
|
|
|
function ( \WC_Cart $cart ) {
|
|
|
|
$fees = $cart->fees_api()->get_fees();
|
2021-08-16 11:08:58 +02:00
|
|
|
WC()->session->set( 'ppcp_fees', $fees );
|
2021-08-13 16:27:52 +02:00
|
|
|
}
|
|
|
|
);
|
2023-08-16 11:48:56 +01:00
|
|
|
add_filter(
|
|
|
|
'ppcp_create_order_request_body_data',
|
|
|
|
function( array $data ) use ( $c ) {
|
|
|
|
|
|
|
|
foreach ( $data['purchase_units'] as $purchase_unit_index => $purchase_unit ) {
|
|
|
|
foreach ( $purchase_unit['items'] as $item_index => $item ) {
|
|
|
|
$data['purchase_units'][ $purchase_unit_index ]['items'][ $item_index ]['name'] =
|
2023-09-18 11:56:28 +01:00
|
|
|
apply_filters( 'woocommerce_paypal_payments_cart_line_item_name', $item['name'], $item['cart_item_key'] ?? null );
|
2023-08-16 11:48:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
);
|
2023-08-08 15:20:40 +01:00
|
|
|
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
|
|
|
|
);
|
2020-09-16 10:18:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the key for the module.
|
|
|
|
*
|
|
|
|
* @return string|void
|
|
|
|
*/
|
|
|
|
public function getKey() {
|
2020-09-01 09:00:45 +03:00
|
|
|
}
|
|
|
|
}
|