mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Display PayPal fees in wc order
This commit is contained in:
parent
53de52e2d3
commit
819149057e
6 changed files with 145 additions and 2 deletions
|
@ -160,4 +160,47 @@ class Orders {
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get PayPal order by id.
|
||||||
|
*
|
||||||
|
* @param string $id PayPal order ID.
|
||||||
|
* @return array
|
||||||
|
* @throws RuntimeException If something went wrong with the request.
|
||||||
|
* @throws PayPalApiException If something went wrong with the PayPal API request.
|
||||||
|
*/
|
||||||
|
public function order( string $id ): array {
|
||||||
|
$bearer = $this->bearer->bearer();
|
||||||
|
$url = trailingslashit( $this->host ) . 'v2/checkout/orders/' . $id;
|
||||||
|
|
||||||
|
$args = array(
|
||||||
|
'headers' => array(
|
||||||
|
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $this->request( $url, $args );
|
||||||
|
if ( $response instanceof WP_Error ) {
|
||||||
|
throw new RuntimeException( $response->get_error_message() );
|
||||||
|
}
|
||||||
|
|
||||||
|
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||||
|
if ( $status_code !== 200 ) {
|
||||||
|
$body = json_decode( $response['body'] );
|
||||||
|
|
||||||
|
$message = $body->details[0]->description ?? '';
|
||||||
|
if ( $message ) {
|
||||||
|
throw new RuntimeException( $message );
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new PayPalApiException(
|
||||||
|
$body,
|
||||||
|
$status_code
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,14 @@ namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
|
||||||
|
|
||||||
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
|
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
|
||||||
use WC_Order;
|
use WC_Order;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\CaptureFactory;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Helper\FeesUpdater;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,6 +162,25 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
add_action(
|
||||||
|
'woocommerce_paypal_payments_payment_capture_completed_webhook_handler',
|
||||||
|
function( WC_Order $wc_order, string $order_id ) use ( $c ) {
|
||||||
|
$payment_methods = $c->get( 'ppcp-local-apms.payment-methods' );
|
||||||
|
if (
|
||||||
|
! $this->is_local_apm( $wc_order->get_payment_method(), $payment_methods )
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fees_updater = $c->get( 'wcgateway.helper.fees-updater' );
|
||||||
|
assert( $fees_updater instanceof FeesUpdater );
|
||||||
|
|
||||||
|
$fees_updater->update( $order_id, $wc_order );
|
||||||
|
},
|
||||||
|
10,
|
||||||
|
2
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -27,6 +27,7 @@ use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderReauthorizeAction;
|
use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderReauthorizeAction;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\CaptureCardPayment;
|
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\CaptureCardPayment;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Helper\FeesUpdater;
|
||||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
|
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
|
||||||
|
@ -1179,6 +1180,12 @@ return array(
|
||||||
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
||||||
return new RefundFeesUpdater( $order_endpoint, $logger );
|
return new RefundFeesUpdater( $order_endpoint, $logger );
|
||||||
},
|
},
|
||||||
|
'wcgateway.helper.fees-updater' => static function ( ContainerInterface $container ): FeesUpdater {
|
||||||
|
return new FeesUpdater(
|
||||||
|
$container->get( 'api.endpoint.orders' ),
|
||||||
|
$container->get( 'api.factory.capture' )
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
'button.helper.messages-disclaimers' => static function ( ContainerInterface $container ): MessagesDisclaimers {
|
'button.helper.messages-disclaimers' => static function ( ContainerInterface $container ): MessagesDisclaimers {
|
||||||
return new MessagesDisclaimers(
|
return new MessagesDisclaimers(
|
||||||
|
|
|
@ -195,7 +195,7 @@ class PayUponInvoice {
|
||||||
);
|
);
|
||||||
|
|
||||||
add_action(
|
add_action(
|
||||||
'ppcp_payment_capture_completed_webhook_handler',
|
'woocommerce_paypal_payments_payment_capture_completed_webhook_handler',
|
||||||
function ( WC_Order $wc_order, string $order_id ) {
|
function ( WC_Order $wc_order, string $order_id ) {
|
||||||
try {
|
try {
|
||||||
if ( $wc_order->get_payment_method() !== PayUponInvoiceGateway::ID ) {
|
if ( $wc_order->get_payment_method() !== PayUponInvoiceGateway::ID ) {
|
||||||
|
|
70
modules/ppcp-wc-gateway/src/Helper/FeesUpdater.php
Normal file
70
modules/ppcp-wc-gateway/src/Helper/FeesUpdater.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* The FeesUpdater helper.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\WcGateway\Helper;
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
|
||||||
|
|
||||||
|
use WC_Order;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\CaptureFactory;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class FeesUpdater
|
||||||
|
*/
|
||||||
|
class FeesUpdater {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The orders' endpoint.
|
||||||
|
*
|
||||||
|
* @var Orders
|
||||||
|
*/
|
||||||
|
private $orders_endpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The capture factory.
|
||||||
|
*
|
||||||
|
* @var CaptureFactory
|
||||||
|
*/
|
||||||
|
private $capture_factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FeesUpdater constructor.
|
||||||
|
*
|
||||||
|
* @param Orders $orders_endpoint The orders' endpoint.
|
||||||
|
* @param CaptureFactory $capture_factory The capture factory.
|
||||||
|
*/
|
||||||
|
public function __construct(Orders $orders_endpoint, CaptureFactory $capture_factory) {
|
||||||
|
$this->orders_endpoint = $orders_endpoint;
|
||||||
|
$this->capture_factory = $capture_factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the fees meta for a given order.
|
||||||
|
*
|
||||||
|
* @param string $order_id
|
||||||
|
* @param WC_Order $wc_order
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function update( string $order_id, WC_Order $wc_order ): void {
|
||||||
|
$order = $this->orders_endpoint->order( $order_id );
|
||||||
|
$body = json_decode( $order['body'] );
|
||||||
|
|
||||||
|
$capture = $this->capture_factory->from_paypal_response( $body->purchase_units[0]->payments->captures[0] );
|
||||||
|
$breakdown = $capture->seller_receivable_breakdown();
|
||||||
|
if ( $breakdown ) {
|
||||||
|
$wc_order->update_meta_data( PayPalGateway::FEES_META_KEY, $breakdown->to_array() );
|
||||||
|
$paypal_fee = $breakdown->paypal_fee();
|
||||||
|
if ( $paypal_fee ) {
|
||||||
|
$wc_order->update_meta_data( 'PayPal Transaction Fee', (string) $paypal_fee->value() );
|
||||||
|
}
|
||||||
|
|
||||||
|
$wc_order->save_meta_data();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -105,7 +105,7 @@ class PaymentCaptureCompleted implements RequestHandler {
|
||||||
/**
|
/**
|
||||||
* Allow access to the webhook logic before updating the WC order.
|
* Allow access to the webhook logic before updating the WC order.
|
||||||
*/
|
*/
|
||||||
do_action( 'ppcp_payment_capture_completed_webhook_handler', $wc_order, $order_id );
|
do_action( 'woocommerce_paypal_payments_payment_capture_completed_webhook_handler', $wc_order, $order_id );
|
||||||
|
|
||||||
if ( $wc_order->get_status() !== 'on-hold' ) {
|
if ( $wc_order->get_status() !== 'on-hold' ) {
|
||||||
return $this->success_response();
|
return $this->success_response();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue