woocommerce-paypal-payments/modules/ppcp-api-client/src/ApiModule.php

94 lines
2.4 KiB
PHP
Raw Normal View History

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
use WC_Order;
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;
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 {
add_action(
'woocommerce_after_calculate_totals',
function ( \WC_Cart $cart ) {
$fees = $cart->fees_api()->get_fees();
WC()->session->set( 'ppcp_fees', $fees );
}
);
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'] =
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
);
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
}
}