Add paypal refund fees to order template

This commit is contained in:
Pedro Silva 2023-08-03 15:53:51 +01:00
parent 5675ebee48
commit af3e68a017
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
3 changed files with 84 additions and 5 deletions

View file

@ -25,6 +25,19 @@ class FeesRenderer {
*/
public function render( WC_Order $wc_order ) : string {
$breakdown = $wc_order->get_meta( PayPalGateway::FEES_META_KEY );
$refund_breakdown = $wc_order->get_meta( PayPalGateway::REFUND_FEES_META_KEY ) ?: array();
$refund_breakdown = [
'paypal_fee' => [
'value' => 0.25,
'currency_code' => 'EUR',
],
'net_amount' => [
'value' => 6.75,
'currency_code' => 'EUR',
],
];
if ( ! is_array( $breakdown ) ) {
return '';
}
@ -42,6 +55,44 @@ class FeesRenderer {
);
}
$refund_total = 0;
$refund_currency = null;
$refund_fee = $refund_breakdown['paypal_fee'] ?? null;
if ( is_array( $refund_fee ) ) {
$refund_total += $refund_fee['value'];
$refund_currency = $refund_fee['currency_code'];
$html .= $this->render_money_row(
__( 'PayPal Refund Fee:', 'woocommerce-paypal-payments' ),
__( 'The fee PayPal collects for the refund transactions.', 'woocommerce-paypal-payments' ),
$refund_fee['value'],
$refund_fee['currency_code'],
true,
'refunded-total'
);
}
$refund_amount = $refund_breakdown['net_amount'] ?? null;
if ( is_array( $refund_amount ) ) {
$refund_total += $refund_amount['value'];
if ( null === $refund_currency ) {
$refund_currency = $refund_fee['currency_code'];
} else if ( $refund_currency !== $refund_fee['currency_code'] ) {
$refund_currency = false;
}
$html .= $this->render_money_row(
__( 'PayPal Refunded:', 'woocommerce-paypal-payments' ),
__( 'The net amount that was refunded.', 'woocommerce-paypal-payments' ),
$refund_amount['value'],
$refund_amount['currency_code'],
true,
'refunded-total'
);
}
$net = $breakdown['net_amount'] ?? null;
if ( is_array( $net ) ) {
$html .= $this->render_money_row(
@ -50,6 +101,15 @@ class FeesRenderer {
$net['value'],
$net['currency_code']
);
if ( ( $refund_total > 0.0 && $refund_currency === $net['currency_code'] ) ) {
$html .= $this->render_money_row(
__( 'PayPal Net Total:', 'woocommerce-paypal-payments' ),
__( 'The net total that will be credited to your PayPal account minus the refunds.', 'woocommerce-paypal-payments' ),
$net['value'] - $refund_total,
$net['currency_code']
);
}
}
return $html;
@ -63,9 +123,10 @@ class FeesRenderer {
* @param string|float $value The money value.
* @param string $currency The currency code.
* @param bool $negative Whether to add the minus sign.
* @param string $html_class Html class to add to the elements.
* @return string
*/
private function render_money_row( string $title, string $tooltip, $value, string $currency, bool $negative = false ): string {
private function render_money_row( string $title, string $tooltip, $value, string $currency, bool $negative = false, string $html_class = '' ): string {
/**
* Bad type hint in WC phpdoc.
*
@ -73,10 +134,10 @@ class FeesRenderer {
*/
return '
<tr>
<td class="label">' . wc_help_tip( $tooltip ) . ' ' . esc_html( $title ) . '
<td class="' . trim( 'label ' . $html_class ) . '">' . wc_help_tip( $tooltip ) . ' ' . esc_html( $title ) . '
</td>
<td width="1%"></td>
<td class="total">
<td class="' . trim( 'total ' . $html_class ) . '">
' .
( $negative ? ' - ' : '' ) .
wc_price( $value, array( 'currency' => $currency ) ) . '

View file

@ -48,6 +48,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
const ORDER_PAYMENT_MODE_META_KEY = '_ppcp_paypal_payment_mode';
const ORDER_PAYMENT_SOURCE_META_KEY = '_ppcp_paypal_payment_source';
const FEES_META_KEY = '_ppcp_paypal_fees';
const REFUND_FEES_META_KEY = '_ppcp_paypal_refund_fees';
const REFUNDS_META_KEY = '_ppcp_refunds';
/**

View file

@ -208,6 +208,15 @@ class IncomingWebhookEndpoint {
public function handle_request( \WP_REST_Request $request ): \WP_REST_Response {
$event = $this->event_from_request( $request );
$this->logger->debug(
sprintf(
'Webhook %s received of type %s and by resource "%s"',
$event->id(),
$event->event_type(),
$event->resource_type()
)
);
$this->last_webhook_event_storage->save( $event );
if ( $this->simulation->is_simulation_event( $event ) ) {
@ -218,11 +227,19 @@ class IncomingWebhookEndpoint {
foreach ( $this->handlers as $handler ) {
if ( $handler->responsible_for_request( $request ) ) {
$this->logger->debug(
sprintf(
'Webhook is going to be handled by %s on %s',
( $handler->event_types() ) ? current( $handler->event_types() ) : '',
get_class( $handler )
)
);
$response = $handler->handle_request( $request );
$this->logger->info(
sprintf(
'Webhook has been handled by %s',
( $handler->event_types() ) ? current( $handler->event_types() ) : ''
'Webhook has been handled by %s on %s',
( $handler->event_types() ) ? current( $handler->event_types() ) : '',
get_class( $handler )
)
);
return $response;