Merge pull request #937 from woocommerce/PCP-736-1-9-0-test-5-missing-pui-refund-functionality-from-wc-order

Missing PUI refund functionality from WC order (736)
This commit is contained in:
Emili Castells 2022-11-02 17:04:03 +01:00 committed by GitHub
commit b420ec4d22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 3 deletions

View file

@ -2083,7 +2083,9 @@ return array(
$container->get( 'wcgateway.transaction-url-provider' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'wcgateway.pay-upon-invoice-helper' ),
$container->get( 'wcgateway.checkout-helper' )
$container->get( 'wcgateway.checkout-helper' ),
$container->get( 'onboarding.state' ),
$container->get( 'wcgateway.processor.refunds' )
);
},
'wcgateway.pay-upon-invoice-fraudnet-session-id' => static function ( ContainerInterface $container ): FraudNetSessionId {

View file

@ -17,10 +17,12 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
use WooCommerce\PayPalCommerce\WcGateway\Helper\CheckoutHelper;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceHelper;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
/**
* Class PayUponInvoiceGateway.
@ -87,6 +89,20 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
*/
protected $checkout_helper;
/**
* The onboarding state.
*
* @var State
*/
protected $state;
/**
* The refund processor.
*
* @var RefundProcessor
*/
protected $refund_processor;
/**
* PayUponInvoiceGateway constructor.
*
@ -98,6 +114,8 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
* @param LoggerInterface $logger The logger.
* @param PayUponInvoiceHelper $pui_helper The PUI helper.
* @param CheckoutHelper $checkout_helper The checkout helper.
* @param State $state The onboarding state.
* @param RefundProcessor $refund_processor The refund processor.
*/
public function __construct(
PayUponInvoiceOrderEndpoint $order_endpoint,
@ -107,7 +125,9 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
TransactionUrlProvider $transaction_url_provider,
LoggerInterface $logger,
PayUponInvoiceHelper $pui_helper,
CheckoutHelper $checkout_helper
CheckoutHelper $checkout_helper,
State $state,
RefundProcessor $refund_processor
) {
$this->id = self::ID;
@ -137,6 +157,12 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
$this->transaction_url_provider = $transaction_url_provider;
$this->pui_helper = $pui_helper;
$this->checkout_helper = $checkout_helper;
$this->state = $state;
if ( $state->current_state() === State::STATE_ONBOARDED ) {
$this->supports = array( 'refunds' );
}
$this->refund_processor = $refund_processor;
}
/**
@ -276,6 +302,22 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
}
}
/**
* Process refund.
*
* @param int $order_id Order ID.
* @param float $amount Refund amount.
* @param string $reason Refund reason.
* @return boolean True or false based on success, or a WP_Error object.
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
$order = wc_get_order( $order_id );
if ( ! is_a( $order, \WC_Order::class ) ) {
return false;
}
return $this->refund_processor->process( $order, (float) $amount, (string) $reason );
}
/**
* Return transaction url for this gateway and given order.
*