resolve merge conflict

This commit is contained in:
David Remer 2020-10-05 15:35:37 +03:00
commit 2608f19f6b
29 changed files with 1339 additions and 529 deletions

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway;
use Dhii\Data\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
@ -26,6 +27,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsListener;
@ -37,9 +39,11 @@ return array(
$order_processor = $container->get( 'wcgateway.order-processor' );
$settings_renderer = $container->get( 'wcgateway.settings.render' );
$authorized_payments = $container->get( 'wcgateway.processor.authorized-payments' );
$notice = $container->get( 'wcgateway.notice.authorize-order-action' );
$settings = $container->get( 'wcgateway.settings' );
$notice = $container->get( 'wcgateway.notice.authorize-order-action' );
$settings = $container->get( 'wcgateway.settings' );
$session_handler = $container->get( 'session.handler' );
$refund_processor = $container->get( 'wcgateway.processor.refunds' );
$state = $container->get( 'onboarding.state' );
return new PayPalGateway(
$settings_renderer,
@ -47,7 +51,9 @@ return array(
$authorized_payments,
$notice,
$settings,
$session_handler
$session_handler,
$refund_processor,
$state
);
},
'wcgateway.credit-card-gateway' => static function ( $container ): CreditCardGateway {
@ -58,6 +64,8 @@ return array(
$settings = $container->get( 'wcgateway.settings' );
$module_url = $container->get( 'wcgateway.url' );
$session_handler = $container->get( 'session.handler' );
$refund_processor = $container->get( 'wcgateway.processor.refunds' );
$state = $container->get( 'onboarding.state' );
return new CreditCardGateway(
$settings_renderer,
$order_processor,
@ -65,7 +73,9 @@ return array(
$notice,
$settings,
$module_url,
$session_handler
$session_handler,
$refund_processor,
$state
);
},
'wcgateway.disabler' => static function ( $container ): DisableGateways {
@ -133,6 +143,11 @@ return array(
$settings
);
},
'wcgateway.processor.refunds' => static function ( $container ): RefundProcessor {
$order_endpoint = $container->get( 'api.endpoint.order' );
$payments_endpoint = $container->get( 'api.endpoint.payments' );
return new RefundProcessor( $order_endpoint, $payments_endpoint );
},
'wcgateway.processor.authorized-payments' => static function ( $container ): AuthorizedPaymentsProcessor {
$order_endpoint = $container->get( 'api.endpoint.order' );
$payments_endpoint = $container->get( 'api.endpoint.payments' );
@ -1714,6 +1729,25 @@ return array(
if ( 'GB' === $country ) {
unset( $fields['disable_funding']['options']['card'] );
}
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
/**
* Depending on your store location, some credit cards can't be used.
* Here, we filter them out.
*
* The DCC Applies object.
*
* @var DccApplies $dcc_applies
*/
$card_options = $fields['disable_cards']['options'];
foreach ( $card_options as $card => $label ) {
if ( $dcc_applies->can_process_card( $card ) ) {
continue;
}
unset( $card_options[ $card ] );
}
$fields['disable_cards']['options'] = $card_options;
$fields['card_icons']['options'] = $card_options;
return $fields;
},

View file

@ -9,10 +9,12 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use Psr\Container\ContainerInterface;
@ -32,6 +34,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
private $module_url;
/**
* The refund processor.
*
* @var RefundProcessor
*/
private $refund_processor;
/**
* CreditCardGateway constructor.
*
@ -42,6 +51,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param ContainerInterface $config The settings.
* @param string $module_url The URL to the module.
* @param SessionHandler $session_handler The Session Handler.
* @param RefundProcessor $refund_processor The refund processor.
* @param State $state The state.
*/
public function __construct(
SettingsRenderer $settings_renderer,
@ -50,7 +61,9 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
AuthorizeOrderActionNotice $notice,
ContainerInterface $config,
string $module_url,
SessionHandler $session_handler
SessionHandler $session_handler,
RefundProcessor $refund_processor,
State $state
) {
$this->id = self::ID;
@ -60,6 +73,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->settings_renderer = $settings_renderer;
$this->config = $config;
$this->session_handler = $session_handler;
$this->refund_processor = $refund_processor;
if ( $state->current_state() === State::STATE_ONBOARDED ) {
$this->supports = array( 'refunds' );
}
if (
defined( 'PPCP_FLAG_SUBSCRIPTION' )
&& PPCP_FLAG_SUBSCRIPTION
@ -67,6 +85,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
&& $this->config->get( 'vault_enabled' )
) {
$this->supports = array(
'refunds',
'products',
'subscriptions',
'subscription_cancellation',
@ -211,4 +230,24 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
public function is_available() : bool {
return $this->config->has( 'dcc_enabled' ) && $this->config->get( 'dcc_enabled' );
}
/**
* 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 );
}
}

View file

@ -10,10 +10,12 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use Psr\Container\ContainerInterface;
@ -72,6 +74,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/
protected $session_handler;
/**
* The Refund Processor.
*
* @var RefundProcessor
*/
private $refund_processor;
/**
* PayPalGateway constructor.
*
@ -81,6 +90,8 @@ class PayPalGateway extends \WC_Payment_Gateway {
* @param AuthorizeOrderActionNotice $notice The Order Action Notice object.
* @param ContainerInterface $config The settings.
* @param SessionHandler $session_handler The Session Handler.
* @param RefundProcessor $refund_processor The Refund Processor.
* @param State $state The state.
*/
public function __construct(
SettingsRenderer $settings_renderer,
@ -88,7 +99,9 @@ class PayPalGateway extends \WC_Payment_Gateway {
AuthorizedPaymentsProcessor $authorized_payments_processor,
AuthorizeOrderActionNotice $notice,
ContainerInterface $config,
SessionHandler $session_handler
SessionHandler $session_handler,
RefundProcessor $refund_processor,
State $state
) {
$this->id = self::ID;
@ -98,6 +111,11 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->settings_renderer = $settings_renderer;
$this->config = $config;
$this->session_handler = $session_handler;
$this->refund_processor = $refund_processor;
if ( $state->current_state() === State::STATE_ONBOARDED ) {
$this->supports = array( 'refunds' );
}
if (
defined( 'PPCP_FLAG_SUBSCRIPTION' )
&& PPCP_FLAG_SUBSCRIPTION
@ -105,6 +123,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
&& $this->config->get( 'vault_enabled' )
) {
$this->supports = array(
'refunds',
'products',
'subscriptions',
'subscription_cancellation',
@ -302,4 +321,23 @@ class PayPalGateway extends \WC_Payment_Gateway {
&& self::ID === sanitize_text_field( wp_unslash( $_GET['section'] ) );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
/**
* 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 );
}
}

View file

@ -0,0 +1,99 @@
<?php
/**
* Processes refunds started in the WooCommerce environment.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Processor
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Class RefundProcessor
*/
class RefundProcessor {
/**
* The order endpoint.
*
* @var OrderEndpoint
*/
private $order_endpoint;
/**
* The payments endpoint.
*
* @var PaymentsEndpoint
*/
private $payments_endpoint;
/**
* RefundProcessor constructor.
*
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint.
*/
public function __construct( OrderEndpoint $order_endpoint, PaymentsEndpoint $payments_endpoint ) {
$this->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint;
}
/**
* Processes a refund.
*
* @param \WC_Order $wc_order The WooCommerce order.
* @param float|null $amount The refund amount.
* @param string $reason The reason for the refund.
*
* @return bool
*/
public function process( \WC_Order $wc_order, float $amount = null, string $reason = '' ) : bool {
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $order_id ) {
return false;
}
try {
$order = $this->order_endpoint->order( $order_id );
if ( ! $order ) {
return false;
}
$purchase_units = $order->purchase_units();
if ( ! $purchase_units ) {
return false;
}
$payments = $purchase_units[0]->payments();
if ( ! $payments ) {
return false;
}
$captures = $payments->captures();
if ( ! $captures ) {
return false;
}
$capture = $captures[0];
$refund = new Refund(
$capture,
$capture->invoice_id(),
$reason,
new Amount(
new Money( $amount, get_woocommerce_currency() )
)
);
return $this->payments_endpoint->refund( $refund );
} catch ( RuntimeException $error ) {
return false;
}
}
}

View file

@ -460,19 +460,6 @@ class SettingsRenderer {
'paypal-payments-for-woocommerce'
);
?>
<a
href="https://developer.paypal.com/docs/platforms/checkout/reference/country-availability-advanced-cards/"
target="_blank"
rel="noreferrer noopener"
>
<?php
esc_html_e(
'Click here to see, in which countries this option is currently available.',
'paypal-payments-for-woocommerce'
);
?>
</a>
</p>
</td>
</tr>