Add refunds support for bancontact gateway and fix tests

This commit is contained in:
Emili Castells Guasch 2024-08-20 12:25:22 +02:00
parent fee2b0ff0f
commit 3dc3026c29
5 changed files with 84 additions and 44 deletions

View file

@ -92,6 +92,11 @@ class GooglePayGateway extends WC_Payment_Gateway {
) {
$this->id = self::ID;
$this->supports = array(
'refunds',
'products',
);
$this->method_title = __( 'Google Pay (via PayPal) ', 'woocommerce-paypal-payments' );
$this->method_description = __( 'The separate payment gateway with the Google Pay button. If disabled, the button is included in the PayPal gateway.', 'woocommerce-paypal-payments' );

View file

@ -12,7 +12,7 @@ namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
'ppcp-local-apms.url' => static function ( ContainerInterface $container ): string {
'ppcp-local-apms.url' => static function ( ContainerInterface $container ): string {
/**
* The path cannot be false.
*
@ -23,17 +23,19 @@ return array(
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},
'ppcp-local-apms.bancontact.wc-gateway' => static function ( ContainerInterface $container ): BancontactGateway {
'ppcp-local-apms.bancontact.wc-gateway' => static function ( ContainerInterface $container ): BancontactGateway {
return new BancontactGateway(
$container->get( 'api.endpoint.orders' ),
$container->get( 'api.factory.purchase-unit' )
$container->get( 'api.factory.purchase-unit' ),
$container->get( 'wcgateway.processor.refunds' ),
$container->get( 'wcgateway.transaction-url-provider' )
);
},
'ppcp-local-apms.bancontact.payment-method' => static function(ContainerInterface $container): BancontactPaymentMethod {
'ppcp-local-apms.bancontact.payment-method' => static function( ContainerInterface $container ): BancontactPaymentMethod {
return new BancontactPaymentMethod(
$container->get('ppcp-local-apms.url'),
$container->get( 'ppcp-local-apms.url' ),
$container->get( 'ppcp.asset-version' ),
$container->get('ppcp-local-apms.bancontact.wc-gateway')
$container->get( 'ppcp-local-apms.bancontact.wc-gateway' )
);
}
},
);

View file

@ -13,6 +13,8 @@ use WC_Payment_Gateway;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
/**
* Class BancontactGateway
@ -35,18 +37,41 @@ class BancontactGateway extends WC_Payment_Gateway {
*/
private $purchase_unit_factory;
/**
* The Refund Processor.
*
* @var RefundProcessor
*/
private $refund_processor;
/**
* Service able to provide transaction url for an order.
*
* @var TransactionUrlProvider
*/
protected $transaction_url_provider;
/**
* BancontactGateway constructor.
*
* @param Orders $orders_endpoint PayPal Orders endpoint.
* @param PurchaseUnitFactory $purchase_unit_factory Purchase unit factory.
* @param Orders $orders_endpoint PayPal Orders endpoint.
* @param PurchaseUnitFactory $purchase_unit_factory Purchase unit factory.
* @param RefundProcessor $refund_processor The Refund Processor.
* @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order.
*/
public function __construct(
Orders $orders_endpoint,
PurchaseUnitFactory $purchase_unit_factory
PurchaseUnitFactory $purchase_unit_factory,
RefundProcessor $refund_processor,
TransactionUrlProvider $transaction_url_provider
) {
$this->id = self::ID;
$this->supports = array(
'refunds',
'products',
);
$this->method_title = __( 'Bancontact', 'woocommerce-paypal-payments' );
$this->method_description = __( 'Bancontact', 'woocommerce-paypal-payments' );
@ -60,8 +85,10 @@ class BancontactGateway extends WC_Payment_Gateway {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
$this->orders_endpoint = $orders_endpoint;
$this->purchase_unit_factory = $purchase_unit_factory;
$this->orders_endpoint = $orders_endpoint;
$this->purchase_unit_factory = $purchase_unit_factory;
$this->refund_processor = $refund_processor;
$this->transaction_url_provider = $transaction_url_provider;
}
/**
@ -164,4 +191,36 @@ class BancontactGateway extends WC_Payment_Gateway {
'redirect' => esc_url( $payer_action ),
);
}
/**
* Process refund.
*
* If the gateway declares 'refunds' support, this will allow it to refund.
* a passed in amount.
*
* @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.
*
* @param \WC_Order $order WC order to get transaction url by.
*
* @return string
*/
public function get_transaction_url( $order ): string {
$this->view_transaction_url = $this->transaction_url_provider->get_transaction_url_base( $order );
return parent::get_transaction_url( $order );
}
}

View file

@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Mockery;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\expect;
class OrdersTest extends TestCase
{
public function test_create() {
$bearer = Mockery::mock(Bearer::class);
$token = Mockery::mock(Token::class);
$token->shouldReceive('token')->andReturn('');
$bearer->shouldReceive('bearer')->andReturn($token);
$sut = new Orders('', $bearer);
expect('wp_remote_get')->andReturn([]);
$this->assertEquals([], $sut->create([]));
}
}

View file

@ -11,7 +11,11 @@ class OrdersTest extends TestCase
$host = 'https://api-m.sandbox.paypal.com';
$container = $this->getContainer();
$orders = new Orders($host, $container->get('api.bearer'));
$orders = new Orders(
$host,
$container->get('api.bearer'),
$container->get( 'woocommerce.logger.woocommerce' )
);
$requestBody = [
"intent" => "CAPTURE",
@ -38,11 +42,7 @@ class OrdersTest extends TestCase
]
];
$headers = array(
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
);
$result = $orders->create($requestBody, $headers);
$result = $orders->create($requestBody);
$this->assertEquals(200, $result['response']['code']);
}