Render fees

This commit is contained in:
Alex P 2022-02-14 13:54:13 +02:00
parent ba9f6074e3
commit c8a55581a0
4 changed files with 195 additions and 0 deletions

View file

@ -19,6 +19,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\MessagesDisclaimers;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderAuthorizeAction;
@ -251,6 +252,9 @@ return array(
$settings = $container->get( 'wcgateway.settings' );
return new OrderTablePaymentStatusColumn( $settings );
},
'wcgateway.admin.fees-renderer' => static function ( ContainerInterface $container ): FeesRenderer {
return new FeesRenderer();
},
'wcgateway.settings.fields' => static function ( ContainerInterface $container ): array {

View file

@ -0,0 +1,82 @@
<?php
/**
* Renders the PayPal fees in the order details.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Admin
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Admin;
use WC_Order;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Class FeesRenderer
*/
class FeesRenderer {
/**
* Renders the PayPal fees in the order details.
*
* @param WC_Order $wc_order The order for which to render the fees.
*
* @return string
*/
public function render( WC_Order $wc_order ) : string {
$breakdown = $wc_order->get_meta( PayPalGateway::FEES_META_KEY );
if ( ! is_array( $breakdown ) ) {
return '';
}
$html = '';
$fee = $breakdown['paypal_fee'] ?? null;
if ( is_array( $fee ) ) {
$html .= $this->render_money_row(
__( 'PayPal Fee:', 'woocommerce-paypal-payments' ),
__( 'The fee PayPal collects for the transaction.', 'woocommerce-paypal-payments' ),
$fee['value'],
$fee['currency_code']
);
}
$net = $breakdown['net_amount'] ?? null;
if ( is_array( $net ) ) {
$html .= $this->render_money_row(
__( 'PayPal Payout:', 'woocommerce-paypal-payments' ),
__( 'The net total that will be credited to your PayPal account.', 'woocommerce-paypal-payments' ),
$net['value'],
$net['currency_code']
);
}
return $html;
}
/**
* Renders a row in the order price breakdown table.
*
* @param string $title The row title.
* @param string $tooltip The title tooltip.
* @param string|float $value The money value.
* @param string $currency The currency code.
* @return string
*/
private function render_money_row( string $title, string $tooltip, $value, string $currency ): string {
/**
* Bad type hint in WC phpdoc.
*
* @psalm-suppress InvalidScalarArgument
*/
return '
<tr>
<td class="label">' . wc_help_tip( $tooltip ) . ' ' . esc_html( $title ) . '
</td>
<td width="1%"></td>
<td class="total">
-&nbsp;' . wc_price( $value, array( 'currency' => $currency ) ) . '
</td>
</tr>';
}
}

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderAuthorizeAction;
@ -86,6 +87,22 @@ class WCGatewayModule implements ModuleInterface {
2
);
$fees_renderer = $c->get( 'wcgateway.admin.fees-renderer' );
assert( $fees_renderer instanceof FeesRenderer );
add_action(
'woocommerce_admin_order_totals_after_total',
function ( int $order_id ) use ( $fees_renderer ) {
$wc_order = wc_get_order( $order_id );
if ( ! $wc_order instanceof WC_Order ) {
return;
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $fees_renderer->render( $wc_order );
}
);
if ( $c->has( 'wcgateway.url' ) ) {
$assets = new SettingsPageAssets(
$c->get( 'wcgateway.url' ),