mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
phpcbf
This commit is contained in:
parent
61d24eede6
commit
cfbd3ae428
52 changed files with 3985 additions and 4147 deletions
|
@ -4,21 +4,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Assets;
|
||||
|
||||
class DisabledSmartButton implements SmartButtonInterface
|
||||
{
|
||||
class DisabledSmartButton implements SmartButtonInterface {
|
||||
|
||||
public function renderWrapper(): bool
|
||||
{
|
||||
|
||||
public function renderWrapper(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function enqueue(): bool
|
||||
{
|
||||
public function enqueue(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function canSaveVaultToken(): bool
|
||||
{
|
||||
public function canSaveVaultToken(): bool {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -20,8 +20,8 @@ use Inpsyde\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
//phpcs:disable Inpsyde.CodeQuality.PropertyPerClassLimit.TooManyProperties
|
||||
class SmartButton implements SmartButtonInterface
|
||||
{
|
||||
class SmartButton implements SmartButtonInterface {
|
||||
|
||||
private $moduleUrl;
|
||||
private $sessionHandler;
|
||||
private $settings;
|
||||
|
@ -64,8 +64,7 @@ class SmartButton implements SmartButtonInterface
|
|||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function renderWrapper(): bool
|
||||
{
|
||||
public function renderWrapper(): bool {
|
||||
|
||||
if ( ! $this->canSaveVaultToken() && $this->hasSubscription() ) {
|
||||
return false;
|
||||
|
@ -83,18 +82,17 @@ class SmartButton implements SmartButtonInterface
|
|||
) {
|
||||
add_action(
|
||||
'woocommerce_review_order_after_submit',
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'dccRenderer',
|
||||
],
|
||||
),
|
||||
11
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function renderMessageWrapperRegistrar(): bool
|
||||
{
|
||||
private function renderMessageWrapperRegistrar(): bool {
|
||||
|
||||
$notEnabledOnCart = $this->settings->has( 'message_cart_enabled' ) &&
|
||||
! $this->settings->get( 'message_cart_enabled' );
|
||||
|
@ -104,10 +102,10 @@ class SmartButton implements SmartButtonInterface
|
|||
) {
|
||||
add_action(
|
||||
'woocommerce_proceed_to_checkout',
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'messageRenderer',
|
||||
],
|
||||
),
|
||||
19
|
||||
);
|
||||
}
|
||||
|
@ -120,10 +118,10 @@ class SmartButton implements SmartButtonInterface
|
|||
) {
|
||||
add_action(
|
||||
'woocommerce_single_product_summary',
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'messageRenderer',
|
||||
],
|
||||
),
|
||||
30
|
||||
);
|
||||
}
|
||||
|
@ -133,18 +131,17 @@ class SmartButton implements SmartButtonInterface
|
|||
if ( ! $notEnabledOnCheckout ) {
|
||||
add_action(
|
||||
'woocommerce_review_order_after_submit',
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'messageRenderer',
|
||||
],
|
||||
),
|
||||
11
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function renderButtonWrapperRegistrar(): bool
|
||||
{
|
||||
private function renderButtonWrapperRegistrar(): bool {
|
||||
|
||||
$notEnabledOnCart = $this->settings->has( 'button_cart_enabled' ) &&
|
||||
! $this->settings->get( 'button_cart_enabled' );
|
||||
|
@ -154,10 +151,10 @@ class SmartButton implements SmartButtonInterface
|
|||
) {
|
||||
add_action(
|
||||
'woocommerce_proceed_to_checkout',
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'buttonRenderer',
|
||||
],
|
||||
),
|
||||
20
|
||||
);
|
||||
}
|
||||
|
@ -170,10 +167,10 @@ class SmartButton implements SmartButtonInterface
|
|||
) {
|
||||
add_action(
|
||||
'woocommerce_single_product_summary',
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'buttonRenderer',
|
||||
],
|
||||
),
|
||||
31
|
||||
);
|
||||
}
|
||||
|
@ -195,13 +192,12 @@ class SmartButton implements SmartButtonInterface
|
|||
);
|
||||
}
|
||||
|
||||
add_action('woocommerce_review_order_after_submit', [$this, 'buttonRenderer'], 10);
|
||||
add_action( 'woocommerce_review_order_after_submit', array( $this, 'buttonRenderer' ), 10 );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function enqueue(): bool
|
||||
{
|
||||
public function enqueue(): bool {
|
||||
$buttonsEnabled = $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' );
|
||||
if ( ! is_checkout() && ! $buttonsEnabled ) {
|
||||
return false;
|
||||
|
@ -212,13 +208,13 @@ class SmartButton implements SmartButtonInterface
|
|||
wp_enqueue_style(
|
||||
'ppcp-hosted-fields',
|
||||
$this->moduleUrl . '/assets/css/hosted-fields.css',
|
||||
[],
|
||||
array(),
|
||||
1
|
||||
);
|
||||
wp_enqueue_script(
|
||||
'ppcp-smart-button',
|
||||
$this->moduleUrl . '/assets/js/button.js',
|
||||
['jquery'],
|
||||
array( 'jquery' ),
|
||||
1,
|
||||
true
|
||||
);
|
||||
|
@ -231,13 +227,12 @@ class SmartButton implements SmartButtonInterface
|
|||
return true;
|
||||
}
|
||||
|
||||
public function buttonRenderer()
|
||||
{
|
||||
public function buttonRenderer() {
|
||||
$product = wc_get_product();
|
||||
if (
|
||||
! is_checkout() && is_a( $product, \WC_Product::class )
|
||||
&& (
|
||||
$product->is_type(['external', 'grouped'])
|
||||
$product->is_type( array( 'external', 'grouped' ) )
|
||||
|| ! $product->is_in_stock()
|
||||
)
|
||||
) {
|
||||
|
@ -247,21 +242,19 @@ class SmartButton implements SmartButtonInterface
|
|||
echo '<div id="ppc-button"></div>';
|
||||
}
|
||||
|
||||
public function messageRenderer()
|
||||
{
|
||||
public function messageRenderer() {
|
||||
|
||||
echo '<div id="ppcp-messages"></div>';
|
||||
}
|
||||
|
||||
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
private function messageValues(): array
|
||||
{
|
||||
private function messageValues(): array {
|
||||
|
||||
if (
|
||||
$this->settings->has( 'disable_funding' )
|
||||
&& in_array( 'credit', (array) $this->settings->get( 'disable_funding' ), true )
|
||||
) {
|
||||
return [];
|
||||
return array();
|
||||
}
|
||||
$placement = 'product';
|
||||
$product = wc_get_product();
|
||||
|
@ -318,33 +311,32 @@ class SmartButton implements SmartButtonInterface
|
|||
}
|
||||
|
||||
if ( ! $shouldShow ) {
|
||||
return [];
|
||||
return array();
|
||||
}
|
||||
|
||||
$values = [
|
||||
$values = array(
|
||||
'wrapper' => '#ppcp-messages',
|
||||
'amount' => $amount,
|
||||
'placement' => $placement,
|
||||
'style' => [
|
||||
'style' => array(
|
||||
'layout' => $layout,
|
||||
'logo' => [
|
||||
'logo' => array(
|
||||
'type' => $logoType,
|
||||
'position' => $logoPosition,
|
||||
],
|
||||
'text' => [
|
||||
),
|
||||
'text' => array(
|
||||
'color' => $textColor,
|
||||
],
|
||||
),
|
||||
'color' => $styleColor,
|
||||
'ratio' => $ratio,
|
||||
],
|
||||
];
|
||||
),
|
||||
);
|
||||
|
||||
return $values;
|
||||
}
|
||||
//phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
|
||||
public function dccRenderer()
|
||||
{
|
||||
public function dccRenderer() {
|
||||
|
||||
$id = 'ppcp-hosted-fields';
|
||||
$canRenderDcc = $this->dccApplies->forCountryCurrency()
|
||||
|
@ -396,8 +388,7 @@ class SmartButton implements SmartButtonInterface
|
|||
}
|
||||
// phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
|
||||
public function canSaveVaultToken(): bool
|
||||
{
|
||||
public function canSaveVaultToken(): bool {
|
||||
|
||||
if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) {
|
||||
return false;
|
||||
|
@ -408,8 +399,7 @@ class SmartButton implements SmartButtonInterface
|
|||
return is_user_logged_in();
|
||||
}
|
||||
|
||||
private function hasSubscription(): bool
|
||||
{
|
||||
private function hasSubscription(): bool {
|
||||
if ( ! $this->subscriptionHelper->acceptOnlyAutomaticPaymentGateways() ) {
|
||||
return false;
|
||||
}
|
||||
|
@ -420,61 +410,60 @@ class SmartButton implements SmartButtonInterface
|
|||
}
|
||||
|
||||
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
private function localizeScript(): array
|
||||
{
|
||||
private function localizeScript(): array {
|
||||
$this->requestData->enqueueNonceFix();
|
||||
$localize = [
|
||||
$localize = array(
|
||||
'script_attributes' => $this->attributes(),
|
||||
'data_client_id' => [
|
||||
'data_client_id' => array(
|
||||
'set_attribute' => ( is_checkout() && $this->dccIsEnabled() )
|
||||
|| $this->canSaveVaultToken(),
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
|
||||
'user' => get_current_user_id(),
|
||||
],
|
||||
),
|
||||
'redirect' => wc_get_checkout_url(),
|
||||
'context' => $this->context(),
|
||||
'ajax' => [
|
||||
'change_cart' => [
|
||||
'ajax' => array(
|
||||
'change_cart' => array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( ChangeCartEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( ChangeCartEndpoint::nonce() ),
|
||||
],
|
||||
'create_order' => [
|
||||
),
|
||||
'create_order' => array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( CreateOrderEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( CreateOrderEndpoint::nonce() ),
|
||||
],
|
||||
'approve_order' => [
|
||||
),
|
||||
'approve_order' => array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( ApproveOrderEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
'enforce_vault' => $this->hasSubscription(),
|
||||
'bn_codes' => $this->bnCodes(),
|
||||
'payer' => $this->payerData(),
|
||||
'button' => [
|
||||
'button' => array(
|
||||
'wrapper' => '#ppc-button',
|
||||
'mini_cart_wrapper' => '#ppc-button-minicart',
|
||||
'cancel_wrapper' => '#ppcp-cancel',
|
||||
'url' => $this->url(),
|
||||
'mini_cart_style' => [
|
||||
'mini_cart_style' => array(
|
||||
'layout' => $this->styleForContext( 'layout', 'mini-cart' ),
|
||||
'color' => $this->styleForContext( 'color', 'mini-cart' ),
|
||||
'shape' => $this->styleForContext( 'shape', 'mini-cart' ),
|
||||
'label' => $this->styleForContext( 'label', 'mini-cart' ),
|
||||
'tagline' => $this->styleForContext( 'tagline', 'mini-cart' ),
|
||||
],
|
||||
'style' => [
|
||||
),
|
||||
'style' => array(
|
||||
'layout' => $this->styleForContext( 'layout', $this->context() ),
|
||||
'color' => $this->styleForContext( 'color', $this->context() ),
|
||||
'shape' => $this->styleForContext( 'shape', $this->context() ),
|
||||
'label' => $this->styleForContext( 'label', $this->context() ),
|
||||
'tagline' => $this->styleForContext( 'tagline', $this->context() ),
|
||||
],
|
||||
],
|
||||
'hosted_fields' => [
|
||||
),
|
||||
),
|
||||
'hosted_fields' => array(
|
||||
'wrapper' => '#ppcp-hosted-fields',
|
||||
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart',
|
||||
'labels' => [
|
||||
'labels' => array(
|
||||
'credit_card_number' => '',
|
||||
'cvv' => '',
|
||||
'mm_yyyy' => __( 'MM/YYYY', 'woocommerce-paypal-commerce-gateway' ),
|
||||
|
@ -482,18 +471,18 @@ class SmartButton implements SmartButtonInterface
|
|||
'Unfortunatly, your credit card details are not valid.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
'messages' => $this->messageValues(),
|
||||
'labels' => [
|
||||
'error' => [
|
||||
'labels' => array(
|
||||
'error' => array(
|
||||
'generic' => __(
|
||||
'Something went wrong. Please try again or choose another payment source.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
],
|
||||
],
|
||||
];
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if ( $this->styleForContext( 'layout', 'mini-cart' ) !== 'horizontal' ) {
|
||||
unset( $localize['button']['mini_cart_style']['tagline'] );
|
||||
|
@ -507,8 +496,7 @@ class SmartButton implements SmartButtonInterface
|
|||
}
|
||||
//phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
|
||||
private function payerData(): ?array
|
||||
{
|
||||
private function payerData(): ?array {
|
||||
|
||||
$customer = WC()->customer;
|
||||
if ( ! is_user_logged_in() || ! is_a( $customer, \WC_Customer::class ) ) {
|
||||
|
@ -517,9 +505,8 @@ class SmartButton implements SmartButtonInterface
|
|||
return $this->payerFactory->fromCustomer( $customer )->toArray();
|
||||
}
|
||||
|
||||
private function url(): string
|
||||
{
|
||||
$params = [
|
||||
private function url(): string {
|
||||
$params = array(
|
||||
'client-id' => $this->clientId,
|
||||
'currency' => get_woocommerce_currency(),
|
||||
'locale' => get_user_locale(),
|
||||
|
@ -532,7 +519,7 @@ class SmartButton implements SmartButtonInterface
|
|||
'commit' => is_checkout() ? 'true' : 'false',
|
||||
'intent' => ( $this->settings->has( 'intent' ) ) ?
|
||||
$this->settings->get( 'intent' ) : 'capture',
|
||||
];
|
||||
);
|
||||
if (
|
||||
defined( 'WP_DEBUG' ) && \WP_DEBUG
|
||||
&& WC()->customer && WC()->customer->get_billing_country()
|
||||
|
@ -544,7 +531,7 @@ class SmartButton implements SmartButtonInterface
|
|||
$params['merchant-id'] = $payee->merchantId();
|
||||
}
|
||||
$disableFunding = $this->settings->has( 'disable_funding' ) ?
|
||||
$this->settings->get('disable_funding') : [];
|
||||
$this->settings->get( 'disable_funding' ) : array();
|
||||
$disableFunding[] = 'venmo';
|
||||
if ( ! is_checkout() ) {
|
||||
$disableFunding[] = 'card';
|
||||
|
@ -554,19 +541,17 @@ class SmartButton implements SmartButtonInterface
|
|||
return $smartButtonUrl;
|
||||
}
|
||||
|
||||
private function attributes(): array
|
||||
{
|
||||
return [
|
||||
private function attributes(): array {
|
||||
return array(
|
||||
'data-partner-attribution-id' => $this->bnCodeForContext( $this->context() ),
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $context
|
||||
* @return string
|
||||
*/
|
||||
private function bnCodeForContext(string $context): string
|
||||
{
|
||||
private function bnCodeForContext( string $context ): string {
|
||||
|
||||
$codes = $this->bnCodes();
|
||||
return ( isset( $codes[ $context ] ) ) ? $codes[ $context ] : '';
|
||||
|
@ -577,20 +562,18 @@ class SmartButton implements SmartButtonInterface
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
private function bnCodes(): array
|
||||
{
|
||||
private function bnCodes(): array {
|
||||
|
||||
return [
|
||||
return array(
|
||||
'checkout' => 'Woo_PPCP',
|
||||
'cart' => 'Woo_PPCP',
|
||||
'mini-cart' => 'Woo_PPCP',
|
||||
'product' => 'Woo_PPCP',
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
private function components(): array
|
||||
{
|
||||
$components = ['buttons'];
|
||||
private function components(): array {
|
||||
$components = array( 'buttons' );
|
||||
if ( $this->messagesApply->forCountry() ) {
|
||||
$components[] = 'messages';
|
||||
}
|
||||
|
@ -600,8 +583,7 @@ class SmartButton implements SmartButtonInterface
|
|||
return $components;
|
||||
}
|
||||
|
||||
private function context(): string
|
||||
{
|
||||
private function context(): string {
|
||||
$context = 'mini-cart';
|
||||
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
|
||||
$context = 'product';
|
||||
|
@ -615,14 +597,13 @@ class SmartButton implements SmartButtonInterface
|
|||
return $context;
|
||||
}
|
||||
|
||||
private function dccIsEnabled(): bool
|
||||
{
|
||||
private function dccIsEnabled(): bool {
|
||||
if ( ! $this->dccApplies->forCountryCurrency() ) {
|
||||
return false;
|
||||
}
|
||||
$keys = [
|
||||
$keys = array(
|
||||
'dcc_gateway_enabled' => 'is_checkout',
|
||||
];
|
||||
);
|
||||
foreach ( $keys as $key => $callback ) {
|
||||
if ( $this->settings->has( $key ) && $this->settings->get( $key ) && $callback() ) {
|
||||
return true;
|
||||
|
@ -631,16 +612,15 @@ class SmartButton implements SmartButtonInterface
|
|||
return false;
|
||||
}
|
||||
|
||||
private function styleForContext(string $style, string $context): string
|
||||
{
|
||||
$defaults = [
|
||||
private function styleForContext( string $style, string $context ): string {
|
||||
$defaults = array(
|
||||
'layout' => 'vertical',
|
||||
'size' => 'responsive',
|
||||
'color' => 'gold',
|
||||
'shape' => 'pill',
|
||||
'label' => 'paypal',
|
||||
'tagline' => true,
|
||||
];
|
||||
);
|
||||
|
||||
$value = isset( $defaults[ $style ] ) ?
|
||||
$defaults[ $style ] : '';
|
||||
|
|
|
@ -4,8 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Assets;
|
||||
|
||||
interface SmartButtonInterface
|
||||
{
|
||||
interface SmartButtonInterface {
|
||||
|
||||
public function renderWrapper(): bool;
|
||||
|
||||
public function enqueue(): bool;
|
||||
|
|
|
@ -16,11 +16,10 @@ use Inpsyde\PayPalCommerce\Button\Helper\EarlyOrderHandler;
|
|||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class ButtonModule implements ModuleInterface
|
||||
{
|
||||
class ButtonModule implements ModuleInterface {
|
||||
|
||||
public function setup(): ServiceProviderInterface
|
||||
{
|
||||
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
|
@ -30,8 +29,7 @@ class ButtonModule implements ModuleInterface
|
|||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function run(ContainerInterface $container)
|
||||
{
|
||||
public function run( ContainerInterface $container ) {
|
||||
/**
|
||||
* @var SmartButton $smartButton
|
||||
*/
|
||||
|
@ -45,11 +43,14 @@ class ButtonModule implements ModuleInterface
|
|||
$smartButton->renderWrapper();
|
||||
}
|
||||
);
|
||||
add_action('wp_enqueue_scripts', static function () use ($container) {
|
||||
add_action(
|
||||
'wp_enqueue_scripts',
|
||||
static function () use ( $container ) {
|
||||
|
||||
$smartButton = $container->get( 'button.smart-button' );
|
||||
$smartButton->enqueue();
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'woocommerce_create_order',
|
||||
|
@ -68,8 +69,7 @@ class ButtonModule implements ModuleInterface
|
|||
$this->registerAjaxEndpoints( $container );
|
||||
}
|
||||
|
||||
private function registerAjaxEndpoints(ContainerInterface $container)
|
||||
{
|
||||
private function registerAjaxEndpoints( ContainerInterface $container ) {
|
||||
add_action(
|
||||
'wc_ajax_' . DataClientIdEndpoint::ENDPOINT,
|
||||
static function () use ( $container ) {
|
||||
|
|
|
@ -12,8 +12,8 @@ use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
|
|||
use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure;
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
|
||||
class ApproveOrderEndpoint implements EndpointInterface
|
||||
{
|
||||
class ApproveOrderEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-approve-order';
|
||||
|
||||
|
@ -34,19 +34,17 @@ class ApproveOrderEndpoint implements EndpointInterface
|
|||
$this->threedSecure = $threedSecure;
|
||||
}
|
||||
|
||||
public static function nonce(): string
|
||||
{
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(): bool
|
||||
{
|
||||
public function handleRequest(): bool {
|
||||
try {
|
||||
$data = $this->requestData->readRequest( $this->nonce() );
|
||||
if ( ! isset( $data['order_id'] ) ) {
|
||||
throw new RuntimeException(
|
||||
__("No order id given", "woocommerce-paypal-commerce-gateway")
|
||||
__( 'No order id given', 'woocommerce-paypal-commerce-gateway' )
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -98,12 +96,12 @@ class ApproveOrderEndpoint implements EndpointInterface
|
|||
return true;
|
||||
} catch ( \RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a($error, PayPalApiException::class) ? $error->details() : [],
|
||||
]
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
|
||||
|
||||
class ChangeCartEndpoint implements EndpointInterface
|
||||
{
|
||||
class ChangeCartEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-change-cart';
|
||||
|
||||
|
@ -35,43 +35,40 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
$this->productDataStore = $productDataStore;
|
||||
}
|
||||
|
||||
public static function nonce(): string
|
||||
{
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
public function handleRequest(): bool
|
||||
{
|
||||
public function handleRequest(): bool {
|
||||
try {
|
||||
return $this->handleData();
|
||||
} catch ( RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a($error, PayPalApiException::class) ? $error->details() : [],
|
||||
]
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function handleData(): bool
|
||||
{
|
||||
private function handleData(): bool {
|
||||
$data = $this->requestData->readRequest( $this->nonce() );
|
||||
$products = $this->productsFromData( $data );
|
||||
if ( ! $products ) {
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => '',
|
||||
'message' => __(
|
||||
'Necessary fields not defined. Action aborted.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'code' => 0,
|
||||
'details' => [],
|
||||
]
|
||||
'details' => array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
@ -97,8 +94,7 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
return $success;
|
||||
}
|
||||
|
||||
private function handleError(): bool
|
||||
{
|
||||
private function handleError(): bool {
|
||||
|
||||
$message = __(
|
||||
'Something went wrong. Action aborted',
|
||||
|
@ -117,20 +113,19 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
}
|
||||
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => '',
|
||||
'message' => $message,
|
||||
'code' => 0,
|
||||
'details' => [],
|
||||
]
|
||||
'details' => array(),
|
||||
)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
private function productsFromData(array $data): ?array
|
||||
{
|
||||
private function productsFromData( array $data ): ?array {
|
||||
|
||||
$products = [];
|
||||
$products = array();
|
||||
|
||||
if (
|
||||
! isset( $data['products'] )
|
||||
|
@ -148,17 +143,16 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
if ( ! $wcProduct ) {
|
||||
return null;
|
||||
}
|
||||
$products[] = [
|
||||
$products[] = array(
|
||||
'product' => $wcProduct,
|
||||
'quantity' => (int) $product['quantity'],
|
||||
'variations' => isset( $product['variations'] ) ? $product['variations'] : null,
|
||||
];
|
||||
);
|
||||
}
|
||||
return $products;
|
||||
}
|
||||
|
||||
private function addProduct(\WC_Product $product, int $quantity): bool
|
||||
{
|
||||
private function addProduct( \WC_Product $product, int $quantity ): bool {
|
||||
return false !== $this->cart->add_to_cart( $product->get_id(), $quantity );
|
||||
}
|
||||
|
||||
|
@ -168,7 +162,7 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
array $postVariations
|
||||
): bool {
|
||||
|
||||
$variations = [];
|
||||
$variations = array();
|
||||
foreach ( $postVariations as $key => $value ) {
|
||||
$variations[ $value['name'] ] = $value['value'];
|
||||
}
|
||||
|
@ -184,8 +178,7 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
);
|
||||
}
|
||||
|
||||
private function generatePurchaseUnits(): array
|
||||
{
|
||||
private function generatePurchaseUnits(): array {
|
||||
return array_map(
|
||||
static function ( PurchaseUnit $lineItem ): array {
|
||||
return $lineItem->toArray();
|
||||
|
|
|
@ -18,8 +18,8 @@ use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
class CreateOrderEndpoint implements EndpointInterface
|
||||
{
|
||||
class CreateOrderEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-create-order';
|
||||
|
||||
|
@ -51,13 +51,11 @@ class CreateOrderEndpoint implements EndpointInterface
|
|||
$this->earlyOrderHandler = $earlyOrderHandler;
|
||||
}
|
||||
|
||||
public static function nonce(): string
|
||||
{
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
public function handleRequest(): bool
|
||||
{
|
||||
public function handleRequest(): bool {
|
||||
try {
|
||||
$data = $this->requestData->readRequest( $this->nonce() );
|
||||
$purchaseUnits = $this->repository->all();
|
||||
|
@ -66,7 +64,7 @@ class CreateOrderEndpoint implements EndpointInterface
|
|||
if ( isset( $data['payer']['phone']['phone_number']['national_number'] ) ) {
|
||||
// make sure the phone number contains only numbers and is max 14. chars long.
|
||||
$number = $data['payer']['phone']['phone_number']['national_number'];
|
||||
$number = preg_replace("/[^0-9]/", "", $number);
|
||||
$number = preg_replace( '/[^0-9]/', '', $number );
|
||||
$number = substr( $number, 0, 14 );
|
||||
$data['payer']['phone']['phone_number']['national_number'] = $number;
|
||||
}
|
||||
|
@ -95,19 +93,18 @@ class CreateOrderEndpoint implements EndpointInterface
|
|||
return true;
|
||||
} catch ( \RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a($error, PayPalApiException::class) ? $error->details() : [],
|
||||
]
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function validateForm(string $formValues, Order $order)
|
||||
{
|
||||
private function validateForm( string $formValues, Order $order ) {
|
||||
$this->order = $order;
|
||||
$parsedValues = wp_parse_args( $formValues );
|
||||
$_POST = $parsedValues;
|
||||
|
@ -115,10 +112,10 @@ class CreateOrderEndpoint implements EndpointInterface
|
|||
|
||||
add_filter(
|
||||
'woocommerce_after_checkout_validation',
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'afterCheckoutValidation',
|
||||
],
|
||||
),
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
@ -126,8 +123,7 @@ class CreateOrderEndpoint implements EndpointInterface
|
|||
$checkout->process_checkout();
|
||||
}
|
||||
|
||||
public function afterCheckoutValidation(array $data, \WP_Error $errors): array
|
||||
{
|
||||
public function afterCheckoutValidation( array $data, \WP_Error $errors ): array {
|
||||
|
||||
$order = $this->order;
|
||||
if ( ! $errors->errors ) {
|
||||
|
@ -146,12 +142,12 @@ class CreateOrderEndpoint implements EndpointInterface
|
|||
}
|
||||
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => '',
|
||||
'message' => $errors->get_error_message(),
|
||||
'code' => (int) $errors->get_error_code(),
|
||||
'details' => [],
|
||||
]
|
||||
'details' => array(),
|
||||
)
|
||||
);
|
||||
return $data;
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@ use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
class DataClientIdEndpoint implements EndpointInterface
|
||||
{
|
||||
class DataClientIdEndpoint implements EndpointInterface {
|
||||
|
||||
|
||||
public const ENDPOINT = 'ppc-data-client-id';
|
||||
|
||||
|
@ -24,31 +24,31 @@ class DataClientIdEndpoint implements EndpointInterface
|
|||
$this->identityToken = $identityToken;
|
||||
}
|
||||
|
||||
public static function nonce(): string
|
||||
{
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
public function handleRequest(): bool
|
||||
{
|
||||
public function handleRequest(): bool {
|
||||
try {
|
||||
$this->requestData->readRequest( $this->nonce() );
|
||||
$userId = get_current_user_id();
|
||||
$token = $this->identityToken->generateForCustomer( $userId );
|
||||
wp_send_json([
|
||||
wp_send_json(
|
||||
array(
|
||||
'token' => $token->token(),
|
||||
'expiration' => $token->expirationTimestamp(),
|
||||
'user' => get_current_user_id(),
|
||||
]);
|
||||
)
|
||||
);
|
||||
return true;
|
||||
} catch ( RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a($error, PayPalApiException::class) ? $error->details() : [],
|
||||
]
|
||||
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
|
||||
)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
interface EndpointInterface
|
||||
{
|
||||
interface EndpointInterface {
|
||||
|
||||
|
||||
public static function nonce(): string;
|
||||
|
||||
|
|
|
@ -6,21 +6,18 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
|||
|
||||
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
|
||||
|
||||
class RequestData
|
||||
{
|
||||
class RequestData {
|
||||
|
||||
public function enqueueNonceFix()
|
||||
{
|
||||
add_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100);
|
||||
|
||||
public function enqueueNonceFix() {
|
||||
add_filter( 'nonce_user_logged_out', array( $this, 'nonceFix' ), 100 );
|
||||
}
|
||||
|
||||
public function dequeueNonceFix()
|
||||
{
|
||||
remove_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100);
|
||||
public function dequeueNonceFix() {
|
||||
remove_filter( 'nonce_user_logged_out', array( $this, 'nonceFix' ), 100 );
|
||||
}
|
||||
|
||||
public function readRequest(string $nonce): array
|
||||
{
|
||||
public function readRequest( string $nonce ): array {
|
||||
$stream = file_get_contents( 'php://input' );
|
||||
$json = json_decode( $stream, true );
|
||||
$this->enqueueNonceFix();
|
||||
|
@ -28,7 +25,7 @@ class RequestData
|
|||
! isset( $json['nonce'] )
|
||||
|| ! wp_verify_nonce( $json['nonce'], $nonce )
|
||||
) {
|
||||
remove_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100);
|
||||
remove_filter( 'nonce_user_logged_out', array( $this, 'nonceFix' ), 100 );
|
||||
throw new RuntimeException(
|
||||
__( 'Could not validate nonce.', 'woocommerce-paypal-commerce-gateway' )
|
||||
);
|
||||
|
@ -48,14 +45,12 @@ class RequestData
|
|||
* @see https://github.com/woocommerce/woocommerce/blob/69e3835041113bee80379c1037e97e26815a699b/includes/class-wc-session-handler.php#L288-L296
|
||||
* @return int
|
||||
*/
|
||||
public function nonceFix(): int
|
||||
{
|
||||
public function nonceFix(): int {
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function sanitize(array $assocArray): array
|
||||
{
|
||||
$data = [];
|
||||
private function sanitize( array $assocArray ): array {
|
||||
$data = array();
|
||||
foreach ( (array) $assocArray as $rawKey => $rawValue ) {
|
||||
if ( ! is_array( $rawValue ) ) {
|
||||
$data[ sanitize_text_field( urldecode( (string) $rawKey ) ) ] = sanitize_text_field( urldecode( (string) $rawValue ) );
|
||||
|
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Exception;
|
||||
|
||||
class RuntimeException extends \RuntimeException
|
||||
{
|
||||
class RuntimeException extends \RuntimeException {
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -13,8 +13,8 @@ use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use Inpsyde\PayPalCommerce\Webhooks\Handler\PrefixTrait;
|
||||
|
||||
class EarlyOrderHandler
|
||||
{
|
||||
class EarlyOrderHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
private $state;
|
||||
|
@ -34,14 +34,12 @@ class EarlyOrderHandler
|
|||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function shouldCreateEarlyOrder(): bool
|
||||
{
|
||||
public function shouldCreateEarlyOrder(): bool {
|
||||
return $this->state->currentState() === State::STATE_ONBOARDED;
|
||||
}
|
||||
|
||||
//phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
public function determineWcOrderId(int $value = null): ?int
|
||||
{
|
||||
public function determineWcOrderId( int $value = null ): ?int {
|
||||
|
||||
if ( ! isset( $_REQUEST['ppcp-resume-order'] ) ) {
|
||||
return $value;
|
||||
|
@ -67,8 +65,7 @@ class EarlyOrderHandler
|
|||
}
|
||||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
public function registerForOrder(Order $order): bool
|
||||
{
|
||||
public function registerForOrder( Order $order ): bool {
|
||||
|
||||
$success = (bool) add_action(
|
||||
'woocommerce_checkout_order_processed',
|
||||
|
@ -78,15 +75,15 @@ class EarlyOrderHandler
|
|||
wp_send_json_success( $order->toArray() );
|
||||
} catch ( \RuntimeException $error ) {
|
||||
wp_send_json_error(
|
||||
[
|
||||
array(
|
||||
'name' => is_a( $error, PayPalApiException::class ) ?
|
||||
$error->name() : '',
|
||||
'message' => $error->getMessage(),
|
||||
'code' => $error->getCode(),
|
||||
'details' => is_a( $error, PayPalApiException::class ) ?
|
||||
$error->details() : [],
|
||||
$error->details() : array(),
|
||||
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -95,8 +92,7 @@ class EarlyOrderHandler
|
|||
return $success;
|
||||
}
|
||||
|
||||
public function configureSessionAndOrder(int $orderId, Order $order): Order
|
||||
{
|
||||
public function configureSessionAndOrder( int $orderId, Order $order ): Order {
|
||||
|
||||
/**
|
||||
* Set the order id in our session in order for
|
||||
|
|
|
@ -4,15 +4,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Helper;
|
||||
|
||||
class MessagesApply
|
||||
{
|
||||
class MessagesApply {
|
||||
|
||||
private $countries = [
|
||||
|
||||
private $countries = array(
|
||||
'US',
|
||||
];
|
||||
);
|
||||
|
||||
public function forCountry(): bool
|
||||
{
|
||||
public function forCountry(): bool {
|
||||
$region = wc_get_base_location();
|
||||
$country = $region['country'];
|
||||
return in_array( $country, $this->countries, true );
|
||||
|
|
|
@ -7,8 +7,8 @@ namespace Inpsyde\PayPalCommerce\Button\Helper;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult as AuthResult;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
|
||||
class ThreeDSecure
|
||||
{
|
||||
class ThreeDSecure {
|
||||
|
||||
|
||||
public const NO_DECISION = 0;
|
||||
public const PROCCEED = 1;
|
||||
|
@ -22,8 +22,7 @@ class ThreeDSecure
|
|||
* @param Order $order
|
||||
* @return int
|
||||
*/
|
||||
public function proceedWithOrder(Order $order): int
|
||||
{
|
||||
public function proceedWithOrder( Order $order ): int {
|
||||
if ( ! $order->paymentSource() ) {
|
||||
return self::NO_DECISION;
|
||||
}
|
||||
|
@ -50,8 +49,7 @@ class ThreeDSecure
|
|||
/**
|
||||
* @return int
|
||||
*/
|
||||
private function noLiabilityShift(AuthResult $result): int
|
||||
{
|
||||
private function noLiabilityShift( AuthResult $result ): int {
|
||||
|
||||
if (
|
||||
$result->enrollmentStatus() === AuthResult::ENROLLMENT_STATUS_BYPASS
|
||||
|
|
|
@ -8,8 +8,8 @@ use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
|
|||
use Inpsyde\PayPalCommerce\Onboarding\Endpoint\LoginSellerEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Onboarding\State;
|
||||
|
||||
class OnboardingAssets
|
||||
{
|
||||
class OnboardingAssets {
|
||||
|
||||
|
||||
private $moduleUrl;
|
||||
private $state;
|
||||
|
@ -25,21 +25,20 @@ class OnboardingAssets
|
|||
$this->loginSellerEndpoint = $loginSellerEndpoint;
|
||||
}
|
||||
|
||||
public function register(): bool
|
||||
{
|
||||
public function register(): bool {
|
||||
|
||||
$url = $this->moduleUrl . '/assets/css/onboarding.css';
|
||||
wp_register_style(
|
||||
'ppcp-onboarding',
|
||||
$url,
|
||||
[],
|
||||
array(),
|
||||
1
|
||||
);
|
||||
$url = $this->moduleUrl . '/assets/js/settings.js';
|
||||
wp_register_script(
|
||||
'ppcp-settings',
|
||||
$url,
|
||||
[],
|
||||
array(),
|
||||
1,
|
||||
true
|
||||
);
|
||||
|
@ -51,28 +50,27 @@ class OnboardingAssets
|
|||
wp_register_script(
|
||||
'ppcp-onboarding',
|
||||
$url,
|
||||
['jquery'],
|
||||
array( 'jquery' ),
|
||||
1,
|
||||
true
|
||||
);
|
||||
wp_localize_script(
|
||||
'ppcp-onboarding',
|
||||
'PayPalCommerceGatewayOnboarding',
|
||||
[
|
||||
array(
|
||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( LoginSellerEndpoint::ENDPOINT ) ),
|
||||
'nonce' => wp_create_nonce( $this->loginSellerEndpoint::nonce() ),
|
||||
'error' => __(
|
||||
'We could not properly onboard you. Please reload and try again.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function enqueue(): bool
|
||||
{
|
||||
public function enqueue(): bool {
|
||||
wp_enqueue_style( 'ppcp-onboarding' );
|
||||
wp_enqueue_script( 'ppcp-settings' );
|
||||
if ( ! $this->shouldRenderOnboardingScript() ) {
|
||||
|
@ -83,8 +81,7 @@ class OnboardingAssets
|
|||
return true;
|
||||
}
|
||||
|
||||
private function shouldRenderOnboardingScript(): bool
|
||||
{
|
||||
private function shouldRenderOnboardingScript(): bool {
|
||||
// phpcs:disable Inpsyde.CodeQuality.VariablesName.SnakeCaseVar
|
||||
global $current_section;
|
||||
if ( $current_section !== 'ppcp-gateway' ) {
|
||||
|
|
|
@ -14,8 +14,8 @@ use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
|||
use Inpsyde\PayPalCommerce\Webhooks\WebhookRegistrar;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
class LoginSellerEndpoint implements EndpointInterface
|
||||
{
|
||||
class LoginSellerEndpoint implements EndpointInterface {
|
||||
|
||||
public const ENDPOINT = 'ppc-login-seller';
|
||||
|
||||
private $requestData;
|
||||
|
@ -38,13 +38,11 @@ class LoginSellerEndpoint implements EndpointInterface
|
|||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
public static function nonce(): string
|
||||
{
|
||||
public static function nonce(): string {
|
||||
return self::ENDPOINT;
|
||||
}
|
||||
|
||||
public function handleRequest(): bool
|
||||
{
|
||||
public function handleRequest(): bool {
|
||||
|
||||
try {
|
||||
$data = $this->requestData->readRequest( $this->nonce() );
|
||||
|
|
|
@ -6,27 +6,24 @@ namespace Inpsyde\PayPalCommerce\Onboarding;
|
|||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class Environment
|
||||
{
|
||||
class Environment {
|
||||
|
||||
|
||||
public const PRODUCTION = 'production';
|
||||
public const SANDBOX = 'sandbox';
|
||||
|
||||
private $settings;
|
||||
public function __construct(ContainerInterface $settings)
|
||||
{
|
||||
public function __construct( ContainerInterface $settings ) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function currentEnvironment(): string
|
||||
{
|
||||
public function currentEnvironment(): string {
|
||||
return (
|
||||
$this->settings->has( 'sandbox_on' ) && $this->settings->get( 'sandbox_on' )
|
||||
) ? self::SANDBOX : self::PRODUCTION;
|
||||
}
|
||||
|
||||
public function currentEnvironmentIs(string $environment): bool
|
||||
{
|
||||
public function currentEnvironmentIs( string $environment ): bool {
|
||||
return $this->currentEnvironment() === $environment;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,18 +12,16 @@ use Inpsyde\PayPalCommerce\Onboarding\Render\OnboardingRenderer;
|
|||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class OnboardingModule implements ModuleInterface
|
||||
{
|
||||
public function setup(): ServiceProviderInterface
|
||||
{
|
||||
class OnboardingModule implements ModuleInterface {
|
||||
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
public function run(ContainerInterface $container)
|
||||
{
|
||||
public function run( ContainerInterface $container ) {
|
||||
|
||||
$assetLoader = $container->get( 'onboarding.assets' );
|
||||
/**
|
||||
|
@ -31,17 +29,17 @@ class OnboardingModule implements ModuleInterface
|
|||
*/
|
||||
add_action(
|
||||
'admin_enqueue_scripts',
|
||||
[
|
||||
array(
|
||||
$assetLoader,
|
||||
'register',
|
||||
]
|
||||
)
|
||||
);
|
||||
add_action(
|
||||
'woocommerce_settings_checkout',
|
||||
[
|
||||
array(
|
||||
$assetLoader,
|
||||
'enqueue',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
add_filter(
|
||||
|
|
|
@ -7,22 +7,20 @@ namespace Inpsyde\PayPalCommerce\Onboarding\Render;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
class OnboardingRenderer
|
||||
{
|
||||
class OnboardingRenderer {
|
||||
|
||||
|
||||
private $partnerReferrals;
|
||||
public function __construct(PartnerReferrals $partnerReferrals)
|
||||
{
|
||||
public function __construct( PartnerReferrals $partnerReferrals ) {
|
||||
$this->partnerReferrals = $partnerReferrals;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
public function render() {
|
||||
try {
|
||||
$url = add_query_arg(
|
||||
[
|
||||
array(
|
||||
'displayMode' => 'minibrowser',
|
||||
],
|
||||
),
|
||||
$this->partnerReferrals->signupLink()
|
||||
);
|
||||
?>
|
||||
|
@ -32,12 +30,14 @@ class OnboardingRenderer
|
|||
data-paypal-onboard-complete="onboardingCallback"
|
||||
href="<?php echo esc_url( $url ); ?>"
|
||||
data-paypal-button="true"
|
||||
><?php
|
||||
>
|
||||
<?php
|
||||
esc_html_e(
|
||||
'Connect to PayPal',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
);
|
||||
?></a>
|
||||
?>
|
||||
</a>
|
||||
<script
|
||||
id="paypal-js"
|
||||
src="https://www.sandbox.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js"
|
||||
|
|
|
@ -6,8 +6,8 @@ namespace Inpsyde\PayPalCommerce\Onboarding;
|
|||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class State
|
||||
{
|
||||
class State {
|
||||
|
||||
public const STATE_START = 0;
|
||||
public const STATE_PROGRESSIVE = 4;
|
||||
public const STATE_ONBOARDED = 8;
|
||||
|
@ -23,13 +23,12 @@ class State
|
|||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function currentState(): int
|
||||
{
|
||||
public function currentState(): int {
|
||||
$value = self::STATE_START;
|
||||
/**
|
||||
* Having provided the merchant email means, we are at least
|
||||
* in the progressive phase of our onboarding.
|
||||
**/
|
||||
*/
|
||||
if (
|
||||
$this->settings->has( 'merchant_email' )
|
||||
&& is_email( $this->settings->get( 'merchant_email' ) )
|
||||
|
|
|
@ -4,11 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Subscription\Helper;
|
||||
|
||||
class SubscriptionHelper
|
||||
{
|
||||
class SubscriptionHelper {
|
||||
|
||||
public function currentProductIsSubscription(): bool
|
||||
{
|
||||
|
||||
public function currentProductIsSubscription(): bool {
|
||||
if ( ! $this->pluginIsActive() ) {
|
||||
return false;
|
||||
}
|
||||
|
@ -16,8 +15,7 @@ class SubscriptionHelper
|
|||
return is_a( $product, \WC_Product::class ) && $product->is_type( 'subscription' );
|
||||
}
|
||||
|
||||
public function cartContainsSubscription(): bool
|
||||
{
|
||||
public function cartContainsSubscription(): bool {
|
||||
if ( ! $this->pluginIsActive() ) {
|
||||
return false;
|
||||
}
|
||||
|
@ -38,8 +36,7 @@ class SubscriptionHelper
|
|||
return false;
|
||||
}
|
||||
|
||||
public function acceptOnlyAutomaticPaymentGateways(): bool
|
||||
{
|
||||
public function acceptOnlyAutomaticPaymentGateways(): bool {
|
||||
|
||||
if ( ! $this->pluginIsActive() ) {
|
||||
return false;
|
||||
|
@ -53,8 +50,7 @@ class SubscriptionHelper
|
|||
return ! $acceptManualRenewals;
|
||||
}
|
||||
|
||||
public function pluginIsActive(): bool
|
||||
{
|
||||
public function pluginIsActive(): bool {
|
||||
|
||||
return class_exists( \WC_Subscriptions::class );
|
||||
}
|
||||
|
|
|
@ -14,8 +14,8 @@ use Inpsyde\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class RenewalHandler
|
||||
{
|
||||
class RenewalHandler {
|
||||
|
||||
|
||||
private $logger;
|
||||
private $repository;
|
||||
|
@ -37,8 +37,7 @@ class RenewalHandler
|
|||
$this->payerFactory = $payerFactory;
|
||||
}
|
||||
|
||||
public function renew(\WC_Order $wcOrder)
|
||||
{
|
||||
public function renew( \WC_Order $wcOrder ) {
|
||||
|
||||
$this->logger->log(
|
||||
'info',
|
||||
|
@ -47,9 +46,9 @@ class RenewalHandler
|
|||
__( 'Start moneytransfer for order %d', 'woocommerce-paypal-commerce-gateway' ),
|
||||
(int) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
|
@ -66,9 +65,9 @@ class RenewalHandler
|
|||
(int) $wcOrder->get_id(),
|
||||
$error->getMessage()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
\WC_Subscriptions_Manager::process_subscription_payment_failure_on_order( $wcOrder );
|
||||
return;
|
||||
|
@ -83,14 +82,13 @@ class RenewalHandler
|
|||
),
|
||||
(int) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function processOrder(\WC_Order $wcOrder)
|
||||
{
|
||||
private function processOrder( \WC_Order $wcOrder ) {
|
||||
|
||||
$userId = (int) $wcOrder->get_customer_id();
|
||||
$customer = new \WC_Customer( $userId );
|
||||
|
@ -101,7 +99,7 @@ class RenewalHandler
|
|||
$purchaseUnits = $this->purchaseUnitFactory->fromWcOrder( $wcOrder );
|
||||
$payer = $this->payerFactory->fromCustomer( $customer );
|
||||
$order = $this->orderEndpoint->createForPurchaseUnits(
|
||||
[$purchaseUnits],
|
||||
array( $purchaseUnits ),
|
||||
$payer,
|
||||
$token,
|
||||
(string) $wcOrder->get_id()
|
||||
|
@ -109,8 +107,7 @@ class RenewalHandler
|
|||
$this->captureOrder( $order, $wcOrder );
|
||||
}
|
||||
|
||||
private function getTokenForCustomer(\WC_Customer $customer, \WC_Order $wcOrder): ?PaymentToken
|
||||
{
|
||||
private function getTokenForCustomer( \WC_Customer $customer, \WC_Order $wcOrder ): ?PaymentToken {
|
||||
|
||||
$token = $this->repository->forUserId( (int) $customer->get_id() );
|
||||
if ( ! $token ) {
|
||||
|
@ -124,18 +121,17 @@ class RenewalHandler
|
|||
),
|
||||
(int) $customer->get_id()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'customer' => $customer,
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
\WC_Subscriptions_Manager::process_subscription_payment_failure_on_order( $wcOrder );
|
||||
}
|
||||
return $token;
|
||||
}
|
||||
|
||||
private function captureOrder(Order $order, \WC_Order $wcOrder)
|
||||
{
|
||||
private function captureOrder( Order $order, \WC_Order $wcOrder ) {
|
||||
|
||||
if ( $order->intent() === 'CAPTURE' && $order->status()->is( OrderStatus::COMPLETED ) ) {
|
||||
$wcOrder->update_status(
|
||||
|
|
|
@ -9,8 +9,8 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
|
||||
class PaymentTokenRepository
|
||||
{
|
||||
class PaymentTokenRepository {
|
||||
|
||||
|
||||
public const USER_META = 'ppcp-vault-token';
|
||||
private $factory;
|
||||
|
@ -24,8 +24,7 @@ class PaymentTokenRepository
|
|||
$this->endpoint = $endpoint;
|
||||
}
|
||||
|
||||
public function forUserId(int $id): ?PaymentToken
|
||||
{
|
||||
public function forUserId( int $id ): ?PaymentToken {
|
||||
try {
|
||||
$token = (array) get_user_meta( $id, self::USER_META, true );
|
||||
if ( ! $token || ! isset( $token['id'] ) ) {
|
||||
|
@ -39,14 +38,12 @@ class PaymentTokenRepository
|
|||
}
|
||||
}
|
||||
|
||||
public function deleteToken(int $userId, PaymentToken $token): bool
|
||||
{
|
||||
public function deleteToken( int $userId, PaymentToken $token ): bool {
|
||||
delete_user_meta( $userId, self::USER_META );
|
||||
return $this->endpoint->deleteToken( $token );
|
||||
}
|
||||
|
||||
private function fetchForUserId(int $id): PaymentToken
|
||||
{
|
||||
private function fetchForUserId( int $id ): PaymentToken {
|
||||
|
||||
$tokens = $this->endpoint->forUser( $id );
|
||||
$token = current( $tokens );
|
||||
|
|
|
@ -10,11 +10,10 @@ use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
|||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class SubscriptionModule implements ModuleInterface
|
||||
{
|
||||
class SubscriptionModule implements ModuleInterface {
|
||||
|
||||
public function setup(): ServiceProviderInterface
|
||||
{
|
||||
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
|
@ -24,8 +23,7 @@ class SubscriptionModule implements ModuleInterface
|
|||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function run(ContainerInterface $container)
|
||||
{
|
||||
public function run( ContainerInterface $container ) {
|
||||
add_action(
|
||||
'woocommerce_scheduled_subscription_payment_' . PayPalGateway::ID,
|
||||
static function ( $amount, $order ) use ( $container ) {
|
||||
|
|
|
@ -7,20 +7,18 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Admin;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
class OrderTablePaymentStatusColumn
|
||||
{
|
||||
class OrderTablePaymentStatusColumn {
|
||||
|
||||
private const COLUMN_KEY = 'ppcp_payment_status';
|
||||
private const INTENT = 'authorize';
|
||||
private const AFTER_COLUMN_KEY = 'order_status';
|
||||
private $settings;
|
||||
|
||||
public function __construct(Settings $settings)
|
||||
{
|
||||
public function __construct( Settings $settings ) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function register(array $columns): array
|
||||
{
|
||||
public function register( array $columns ): array {
|
||||
if ( ! $this->settings->has( 'intent' ) || $this->settings->get( 'intent' ) !== self::INTENT ) {
|
||||
return $columns;
|
||||
}
|
||||
|
@ -30,17 +28,16 @@ class OrderTablePaymentStatusColumn
|
|||
|
||||
$columns = array_merge(
|
||||
array_slice( $columns, 0, $toInsertPosition ),
|
||||
[
|
||||
array(
|
||||
self::COLUMN_KEY => __( 'Payment Captured', 'woocommerce-paypal-commerce-gateway' ),
|
||||
],
|
||||
),
|
||||
array_slice( $columns, $toInsertPosition )
|
||||
);
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function render(string $column, int $wcOrderId)
|
||||
{
|
||||
public function render( string $column, int $wcOrderId ) {
|
||||
if ( ! $this->settings->has( 'intent' ) || $this->settings->get( 'intent' ) !== self::INTENT ) {
|
||||
return;
|
||||
}
|
||||
|
@ -63,19 +60,16 @@ class OrderTablePaymentStatusColumn
|
|||
$this->renderIncompletedStatus();
|
||||
}
|
||||
|
||||
private function renderForOrder(\WC_Order $order): bool
|
||||
{
|
||||
private function renderForOrder( \WC_Order $order ): bool {
|
||||
return ! empty( $order->get_meta( PayPalGateway::CAPTURED_META_KEY ) );
|
||||
}
|
||||
|
||||
private function isCaptured(\WC_Order $wcOrder): bool
|
||||
{
|
||||
private function isCaptured( \WC_Order $wcOrder ): bool {
|
||||
$captured = $wcOrder->get_meta( PayPalGateway::CAPTURED_META_KEY );
|
||||
return wc_string_to_bool( $captured );
|
||||
}
|
||||
|
||||
private function renderCompletedStatus()
|
||||
{
|
||||
private function renderCompletedStatus() {
|
||||
printf(
|
||||
'<span class="dashicons dashicons-yes">
|
||||
<span class="screen-reader-text">%s</span>
|
||||
|
@ -84,8 +78,7 @@ class OrderTablePaymentStatusColumn
|
|||
);
|
||||
}
|
||||
|
||||
private function renderIncompletedStatus()
|
||||
{
|
||||
private function renderIncompletedStatus() {
|
||||
printf(
|
||||
'<mark class="onbackorder">%s</mark>',
|
||||
esc_html__( 'Not captured', 'woocommerce-paypal-commerce-gateway' )
|
||||
|
|
|
@ -6,10 +6,9 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Admin;
|
|||
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
|
||||
class PaymentStatusOrderDetail
|
||||
{
|
||||
public function render(int $wcOrderId)
|
||||
{
|
||||
class PaymentStatusOrderDetail {
|
||||
|
||||
public function render( int $wcOrderId ) {
|
||||
$wcOrder = new \WC_Order( $wcOrderId );
|
||||
$intent = $wcOrder->get_meta( PayPalGateway::INTENT_META_KEY );
|
||||
$captured = $wcOrder->get_meta( PayPalGateway::CAPTURED_META_KEY );
|
||||
|
|
|
@ -11,10 +11,10 @@ use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
|||
* Service that fills checkout address fields
|
||||
* with address selected via PayPal
|
||||
*/
|
||||
class CheckoutPayPalAddressPreset
|
||||
{
|
||||
class CheckoutPayPalAddressPreset {
|
||||
|
||||
private $shippingCache = [];
|
||||
|
||||
private $shippingCache = array();
|
||||
|
||||
/**
|
||||
* @var SessionHandler
|
||||
|
@ -24,8 +24,7 @@ class CheckoutPayPalAddressPreset
|
|||
/**
|
||||
* @param SessionHandler $sessionHandler
|
||||
*/
|
||||
public function __construct(SessionHandler $sessionHandler)
|
||||
{
|
||||
public function __construct( SessionHandler $sessionHandler ) {
|
||||
$this->sessionHandler = $sessionHandler;
|
||||
}
|
||||
|
||||
|
@ -36,8 +35,7 @@ class CheckoutPayPalAddressPreset
|
|||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function filterCheckoutFiled($defaultValue, $fieldId): ?string
|
||||
{
|
||||
public function filterCheckoutFiled( $defaultValue, $fieldId ): ?string {
|
||||
if ( ! is_string( $defaultValue ) ) {
|
||||
$defaultValue = null;
|
||||
}
|
||||
|
@ -49,8 +47,7 @@ class CheckoutPayPalAddressPreset
|
|||
return $this->readPresetForField( $fieldId ) ?? $defaultValue;
|
||||
}
|
||||
|
||||
private function readPresetForField(string $fieldId): ?string
|
||||
{
|
||||
private function readPresetForField( string $fieldId ): ?string {
|
||||
$order = $this->sessionHandler->order();
|
||||
if ( ! $order ) {
|
||||
return null;
|
||||
|
@ -59,24 +56,24 @@ class CheckoutPayPalAddressPreset
|
|||
$shipping = $this->readShippingFromOrder();
|
||||
$payer = $order->payer();
|
||||
|
||||
$addressMap = [
|
||||
$addressMap = array(
|
||||
'billing_address_1' => 'addressLine1',
|
||||
'billing_address_2' => 'addressLine2',
|
||||
'billing_postcode' => 'postalCode',
|
||||
'billing_country' => 'countryCode',
|
||||
'billing_city' => 'adminArea2',
|
||||
'billing_state' => 'adminArea1',
|
||||
];
|
||||
$payerNameMap = [
|
||||
);
|
||||
$payerNameMap = array(
|
||||
'billing_last_name' => 'surname',
|
||||
'billing_first_name' => 'givenName',
|
||||
];
|
||||
$payerMap = [
|
||||
);
|
||||
$payerMap = array(
|
||||
'billing_email' => 'emailAddress',
|
||||
];
|
||||
$payerPhoneMap = [
|
||||
);
|
||||
$payerPhoneMap = array(
|
||||
'billing_phone' => 'nationalNumber',
|
||||
];
|
||||
);
|
||||
|
||||
if ( array_key_exists( $fieldId, $addressMap ) && $shipping ) {
|
||||
return $shipping->address()->{$addressMap[ $fieldId ]}() ?: null;
|
||||
|
@ -102,8 +99,7 @@ class CheckoutPayPalAddressPreset
|
|||
return null;
|
||||
}
|
||||
|
||||
private function readShippingFromOrder(): ?Shipping
|
||||
{
|
||||
private function readShippingFromOrder(): ?Shipping {
|
||||
$order = $this->sessionHandler->order();
|
||||
if ( ! $order ) {
|
||||
return null;
|
||||
|
|
|
@ -9,8 +9,8 @@ use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class DisableGateways
|
||||
{
|
||||
class DisableGateways {
|
||||
|
||||
|
||||
private $sessionHandler;
|
||||
private $settings;
|
||||
|
@ -23,8 +23,7 @@ class DisableGateways
|
|||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function handler(array $methods): array
|
||||
{
|
||||
public function handler( array $methods ): array {
|
||||
if ( ! isset( $methods[ PayPalGateway::ID ] ) && ! isset( $methods[ CreditCardGateway::ID ] ) ) {
|
||||
return $methods;
|
||||
}
|
||||
|
@ -46,18 +45,16 @@ class DisableGateways
|
|||
}
|
||||
|
||||
if ( $this->isCreditCard() ) {
|
||||
return [CreditCardGateway::ID => $methods[CreditCardGateway::ID]];
|
||||
return array( CreditCardGateway::ID => $methods[ CreditCardGateway::ID ] );
|
||||
}
|
||||
return [PayPalGateway::ID => $methods[PayPalGateway::ID]];
|
||||
return array( PayPalGateway::ID => $methods[ PayPalGateway::ID ] );
|
||||
}
|
||||
|
||||
private function needsToDisableGateways(): bool
|
||||
{
|
||||
private function needsToDisableGateways(): bool {
|
||||
return $this->sessionHandler->order() !== null;
|
||||
}
|
||||
|
||||
private function isCreditCard(): bool
|
||||
{
|
||||
private function isCreditCard(): bool {
|
||||
$order = $this->sessionHandler->order();
|
||||
if ( ! $order ) {
|
||||
return false;
|
||||
|
|
|
@ -3,20 +3,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\WcGateway\Endpoint;
|
||||
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Inpsyde\PayPalCommerce\Webhooks\Handler\PrefixTrait;
|
||||
|
||||
class ReturnUrlEndpoint
|
||||
{
|
||||
class ReturnUrlEndpoint {
|
||||
|
||||
use PrefixTrait;
|
||||
public const ENDPOINT = 'ppc-return-url';
|
||||
|
||||
private $gateway;
|
||||
private $orderEndpoint;
|
||||
public function __construct(PayPalGateway $gateway, OrderEndpoint $orderEndpoint, string $prefix)
|
||||
{
|
||||
public function __construct( PayPalGateway $gateway, OrderEndpoint $orderEndpoint, string $prefix ) {
|
||||
$this->gateway = $gateway;
|
||||
$this->orderEndpoint = $orderEndpoint;
|
||||
$this->prefix = $prefix;
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Exception;
|
|||
use Exception;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class NotFoundException extends Exception implements NotFoundExceptionInterface
|
||||
{
|
||||
class NotFoundException extends Exception implements NotFoundExceptionInterface {
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -15,8 +15,8 @@ use Psr\Container\ContainerInterface;
|
|||
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
|
||||
//phpcs:disable Inpsyde.CodeQuality.NoAccessors.NoGetter
|
||||
//phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
|
||||
class CreditCardGateway extends PayPalGateway
|
||||
{
|
||||
class CreditCardGateway extends PayPalGateway {
|
||||
|
||||
public const ID = 'ppcp-credit-card-gateway';
|
||||
|
||||
private $moduleUrl;
|
||||
|
@ -43,7 +43,7 @@ class CreditCardGateway extends PayPalGateway
|
|||
&& $this->config->has( 'vault_enabled' )
|
||||
&& $this->config->get( 'vault_enabled' )
|
||||
) {
|
||||
$this->supports = [
|
||||
$this->supports = array(
|
||||
'products',
|
||||
'subscriptions',
|
||||
'subscription_cancellation',
|
||||
|
@ -55,7 +55,7 @@ class CreditCardGateway extends PayPalGateway
|
|||
'subscription_payment_method_change_customer',
|
||||
'subscription_payment_method_change_admin',
|
||||
'multiple_subscriptions',
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
$this->method_title = __(
|
||||
|
@ -76,32 +76,30 @@ class CreditCardGateway extends PayPalGateway
|
|||
|
||||
add_action(
|
||||
'woocommerce_update_options_payment_gateways_' . $this->id,
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'process_admin_options',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$this->moduleUrl = $moduleUrl;
|
||||
}
|
||||
|
||||
public function init_form_fields()
|
||||
{
|
||||
$this->form_fields = [
|
||||
'enabled' => [
|
||||
public function init_form_fields() {
|
||||
$this->form_fields = array(
|
||||
'enabled' => array(
|
||||
'title' => __( 'Enable/Disable', 'woocommerce-paypal-commerce-gateway' ),
|
||||
'type' => 'checkbox',
|
||||
'label' => __( 'Enable Credit Card Payments', 'woocommerce-paypal-commerce-gateway' ),
|
||||
'default' => 'no',
|
||||
],
|
||||
'ppcp' => [
|
||||
),
|
||||
'ppcp' => array(
|
||||
'type' => 'ppcp',
|
||||
],
|
||||
];
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function generate_ppcp_html(): string
|
||||
{
|
||||
public function generate_ppcp_html(): string {
|
||||
|
||||
ob_start();
|
||||
$this->settingsRenderer->render( true );
|
||||
|
@ -110,14 +108,13 @@ class CreditCardGateway extends PayPalGateway
|
|||
return $content;
|
||||
}
|
||||
|
||||
public function get_title()
|
||||
{
|
||||
public function get_title() {
|
||||
|
||||
if ( is_admin() ) {
|
||||
return parent::get_title();
|
||||
}
|
||||
$title = parent::get_title();
|
||||
$icons = $this->config->has('card_icons') ? (array) $this->config->get('card_icons') : [];
|
||||
$icons = $this->config->has( 'card_icons' ) ? (array) $this->config->get( 'card_icons' ) : array();
|
||||
if ( empty( $icons ) ) {
|
||||
return $title;
|
||||
}
|
||||
|
@ -136,9 +133,8 @@ class CreditCardGateway extends PayPalGateway
|
|||
return $title . implode( '', $images );
|
||||
}
|
||||
|
||||
private function cardLabels(): array
|
||||
{
|
||||
return [
|
||||
private function cardLabels(): array {
|
||||
return array(
|
||||
'visa' => _x(
|
||||
'Visa',
|
||||
'Name of credit card',
|
||||
|
@ -174,6 +170,6 @@ class CreditCardGateway extends PayPalGateway
|
|||
'Name of credit card',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
];
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,8 +23,8 @@ use Psr\Container\ContainerInterface;
|
|||
|
||||
//phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
|
||||
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
|
||||
class PayPalGateway extends \WC_Payment_Gateway
|
||||
{
|
||||
class PayPalGateway extends \WC_Payment_Gateway {
|
||||
|
||||
|
||||
public const ID = 'ppcp-gateway';
|
||||
public const CAPTURED_META_KEY = '_ppcp_paypal_captured';
|
||||
|
@ -60,7 +60,7 @@ class PayPalGateway extends \WC_Payment_Gateway
|
|||
&& $this->config->has( 'vault_enabled' )
|
||||
&& $this->config->get( 'vault_enabled' )
|
||||
) {
|
||||
$this->supports = [
|
||||
$this->supports = array(
|
||||
'products',
|
||||
'subscriptions',
|
||||
'subscription_cancellation',
|
||||
|
@ -72,7 +72,7 @@ class PayPalGateway extends \WC_Payment_Gateway
|
|||
'subscription_payment_method_change_customer',
|
||||
'subscription_payment_method_change_admin',
|
||||
'multiple_subscriptions',
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
$this->method_title = __( 'PayPal', 'woocommerce-paypal-commerce-gateway' );
|
||||
|
@ -90,36 +90,33 @@ class PayPalGateway extends \WC_Payment_Gateway
|
|||
|
||||
add_action(
|
||||
'woocommerce_update_options_payment_gateways_' . $this->id,
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'process_admin_options',
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function needs_setup(): bool
|
||||
{
|
||||
public function needs_setup(): bool {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function init_form_fields()
|
||||
{
|
||||
$this->form_fields = [
|
||||
'enabled' => [
|
||||
public function init_form_fields() {
|
||||
$this->form_fields = array(
|
||||
'enabled' => array(
|
||||
'title' => __( 'Enable/Disable', 'woocommerce-paypal-commerce-gateway' ),
|
||||
'type' => 'checkbox',
|
||||
'label' => __( 'Enable PayPal Payments', 'woocommerce-paypal-commerce-gateway' ),
|
||||
'default' => 'no',
|
||||
],
|
||||
'ppcp' => [
|
||||
),
|
||||
'ppcp' => array(
|
||||
'type' => 'ppcp',
|
||||
],
|
||||
];
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function process_payment($orderId): ?array
|
||||
{
|
||||
public function process_payment( $orderId ): ?array {
|
||||
global $woocommerce;
|
||||
$wcOrder = wc_get_order( $orderId );
|
||||
if ( ! is_a( $wcOrder, \WC_Order::class ) ) {
|
||||
|
@ -131,19 +128,19 @@ class PayPalGateway extends \WC_Payment_Gateway
|
|||
*/
|
||||
//phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wcOrder->has_status( 'processing' ) ) {
|
||||
return [
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => $this->get_return_url( $wcOrder ),
|
||||
];
|
||||
);
|
||||
}
|
||||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
try {
|
||||
if ( $this->orderProcessor->process( $wcOrder, $woocommerce ) ) {
|
||||
return [
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => $this->get_return_url( $wcOrder ),
|
||||
];
|
||||
);
|
||||
}
|
||||
} catch ( PayPalApiException $error ) {
|
||||
if ( $error->hasDetail( 'INSTRUMENT_DECLINED' ) ) {
|
||||
|
@ -151,10 +148,10 @@ class PayPalGateway extends \WC_Payment_Gateway
|
|||
'https://www.sandbox.paypal.com/' : 'https://www.paypal.com/';
|
||||
$url = $host . 'checkoutnow?token=' . $this->sessionHandler->order()->id();
|
||||
|
||||
return [
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => $url,
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
$this->sessionHandler->destroySessionData();
|
||||
|
@ -170,8 +167,7 @@ class PayPalGateway extends \WC_Payment_Gateway
|
|||
return null;
|
||||
}
|
||||
|
||||
public function captureAuthorizedPayment(\WC_Order $wcOrder): bool
|
||||
{
|
||||
public function captureAuthorizedPayment( \WC_Order $wcOrder ): bool {
|
||||
$isProcessed = $this->authorizedPayments->process( $wcOrder );
|
||||
$this->renderAuthorizationMessageForStatus( $this->authorizedPayments->lastStatus() );
|
||||
|
||||
|
@ -201,23 +197,21 @@ class PayPalGateway extends \WC_Payment_Gateway
|
|||
return false;
|
||||
}
|
||||
|
||||
private function renderAuthorizationMessageForStatus(string $status)
|
||||
{
|
||||
private function renderAuthorizationMessageForStatus( string $status ) {
|
||||
|
||||
$messageMapping = [
|
||||
$messageMapping = array(
|
||||
AuthorizedPaymentsProcessor::SUCCESSFUL => AuthorizeOrderActionNotice::SUCCESS,
|
||||
AuthorizedPaymentsProcessor::ALREADY_CAPTURED => AuthorizeOrderActionNotice::ALREADY_CAPTURED,
|
||||
AuthorizedPaymentsProcessor::INACCESSIBLE => AuthorizeOrderActionNotice::NO_INFO,
|
||||
AuthorizedPaymentsProcessor::NOT_FOUND => AuthorizeOrderActionNotice::NOT_FOUND,
|
||||
];
|
||||
);
|
||||
$displayMessage = ( isset( $messageMapping[ $status ] ) ) ?
|
||||
$messageMapping[ $status ]
|
||||
: AuthorizeOrderActionNotice::FAILED;
|
||||
$this->notice->displayMessage( $displayMessage );
|
||||
}
|
||||
|
||||
public function generate_ppcp_html(): string
|
||||
{
|
||||
public function generate_ppcp_html(): string {
|
||||
|
||||
ob_start();
|
||||
$this->settingsRenderer->render( false );
|
||||
|
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\WcGateway\Gateway;
|
||||
|
||||
interface WcGatewayInterface
|
||||
{
|
||||
interface WcGatewayInterface {
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Notice;
|
|||
|
||||
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
|
||||
|
||||
class AuthorizeOrderActionNotice
|
||||
{
|
||||
class AuthorizeOrderActionNotice {
|
||||
|
||||
public const QUERY_PARAM = 'ppcp-authorized-message';
|
||||
|
||||
public const NO_INFO = 81;
|
||||
|
@ -16,8 +16,7 @@ class AuthorizeOrderActionNotice
|
|||
public const SUCCESS = 84;
|
||||
public const NOT_FOUND = 85;
|
||||
|
||||
public function message(): ?Message
|
||||
{
|
||||
public function message(): ?Message {
|
||||
|
||||
$message = $this->currentMessage();
|
||||
if ( ! $message ) {
|
||||
|
@ -27,55 +26,53 @@ class AuthorizeOrderActionNotice
|
|||
return new Message( $message['message'], $message['type'] );
|
||||
}
|
||||
|
||||
private function currentMessage(): array
|
||||
{
|
||||
$messages[self::NO_INFO] = [
|
||||
private function currentMessage(): array {
|
||||
$messages[ self::NO_INFO ] = array(
|
||||
'message' => __(
|
||||
'Could not retrieve information. Try again later.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'type' => 'error',
|
||||
];
|
||||
$messages[self::ALREADY_CAPTURED] = [
|
||||
);
|
||||
$messages[ self::ALREADY_CAPTURED ] = array(
|
||||
'message' => __(
|
||||
'Payment already captured.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'type' => 'error',
|
||||
];
|
||||
$messages[self::FAILED] = [
|
||||
);
|
||||
$messages[ self::FAILED ] = array(
|
||||
'message' => __(
|
||||
'Failed to capture. Try again later.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'type' => 'error',
|
||||
];
|
||||
$messages[self::NOT_FOUND] = [
|
||||
);
|
||||
$messages[ self::NOT_FOUND ] = array(
|
||||
'message' => __(
|
||||
'Could not find payment to process.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'type' => 'error',
|
||||
];
|
||||
$messages[self::SUCCESS] = [
|
||||
);
|
||||
$messages[ self::SUCCESS ] = array(
|
||||
'message' => __(
|
||||
'Payment successfully captured.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'type' => 'success',
|
||||
];
|
||||
);
|
||||
|
||||
//phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
if ( ! isset( $_GET[ self::QUERY_PARAM ] ) ) { // Input ok.
|
||||
return [];
|
||||
return array();
|
||||
}
|
||||
$messageId = absint( $_GET[ self::QUERY_PARAM ] ); // Input ok.
|
||||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
return (isset($messages[$messageId])) ? $messages[$messageId] : [];
|
||||
return ( isset( $messages[ $messageId ] ) ) ? $messages[ $messageId ] : array();
|
||||
}
|
||||
|
||||
public function displayMessage(int $messageCode): void
|
||||
{
|
||||
public function displayMessage( int $messageCode ): void {
|
||||
add_filter(
|
||||
'redirect_post_location',
|
||||
static function ( $location ) use ( $messageCode ) {
|
||||
|
|
|
@ -9,19 +9,17 @@ use Inpsyde\PayPalCommerce\Onboarding\State;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class ConnectAdminNotice
|
||||
{
|
||||
class ConnectAdminNotice {
|
||||
|
||||
private $state;
|
||||
private $settings;
|
||||
|
||||
public function __construct(State $state, ContainerInterface $settings)
|
||||
{
|
||||
public function __construct( State $state, ContainerInterface $settings ) {
|
||||
$this->state = $state;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function connectMessage(): ?Message
|
||||
{
|
||||
public function connectMessage(): ?Message {
|
||||
if ( ! $this->shouldDisplay() ) {
|
||||
return null;
|
||||
}
|
||||
|
@ -38,8 +36,7 @@ class ConnectAdminNotice
|
|||
return new Message( $message, 'warning' );
|
||||
}
|
||||
|
||||
protected function shouldDisplay(): bool
|
||||
{
|
||||
protected function shouldDisplay(): bool {
|
||||
// TODO: decide on what condition to display
|
||||
return $this->state->currentState() < State::STATE_PROGRESSIVE;
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
|
||||
class AuthorizedPaymentsProcessor
|
||||
{
|
||||
class AuthorizedPaymentsProcessor {
|
||||
|
||||
public const SUCCESSFUL = 'SUCCESSFUL';
|
||||
public const ALREADY_CAPTURED = 'ALREADY_CAPTURED';
|
||||
public const FAILED = 'FAILED';
|
||||
|
@ -32,8 +32,7 @@ class AuthorizedPaymentsProcessor
|
|||
$this->paymentsEndpoint = $paymentsEndpoint;
|
||||
}
|
||||
|
||||
public function process(\WC_Order $wcOrder): bool
|
||||
{
|
||||
public function process( \WC_Order $wcOrder ): bool {
|
||||
try {
|
||||
$order = $this->payPalOrderFromWcOrder( $wcOrder );
|
||||
} catch ( Exception $exception ) {
|
||||
|
@ -63,21 +62,18 @@ class AuthorizedPaymentsProcessor
|
|||
return true;
|
||||
}
|
||||
|
||||
public function lastStatus(): string
|
||||
{
|
||||
public function lastStatus(): string {
|
||||
|
||||
return $this->lastStatus;
|
||||
}
|
||||
|
||||
private function payPalOrderFromWcOrder(\WC_Order $wcOrder): Order
|
||||
{
|
||||
private function payPalOrderFromWcOrder( \WC_Order $wcOrder ): Order {
|
||||
$orderId = $wcOrder->get_meta( PayPalGateway::ORDER_ID_META_KEY );
|
||||
return $this->orderEndpoint->order( $orderId );
|
||||
}
|
||||
|
||||
private function allAuthorizations(Order $order): array
|
||||
{
|
||||
$authorizations = [];
|
||||
private function allAuthorizations( Order $order ): array {
|
||||
$authorizations = array();
|
||||
foreach ( $order->purchaseUnits() as $purchaseUnit ) {
|
||||
foreach ( $purchaseUnit->payments()->authorizations() as $authorization ) {
|
||||
$authorizations[] = $authorization;
|
||||
|
@ -87,13 +83,11 @@ class AuthorizedPaymentsProcessor
|
|||
return $authorizations;
|
||||
}
|
||||
|
||||
private function areAuthorizationToCapture(Authorization ...$authorizations): bool
|
||||
{
|
||||
private function areAuthorizationToCapture( Authorization ...$authorizations ): bool {
|
||||
return (bool) count( $this->authorizationsToCapture( ...$authorizations ) );
|
||||
}
|
||||
|
||||
private function captureAuthorizations(Authorization ...$authorizations)
|
||||
{
|
||||
private function captureAuthorizations( Authorization ...$authorizations ) {
|
||||
$uncapturedAuthorizations = $this->authorizationsToCapture( ...$authorizations );
|
||||
foreach ( $uncapturedAuthorizations as $authorization ) {
|
||||
$this->paymentsEndpoint->capture( $authorization->id() );
|
||||
|
@ -104,8 +98,7 @@ class AuthorizedPaymentsProcessor
|
|||
* @param Authorization ...$authorizations
|
||||
* @return Authorization[]
|
||||
*/
|
||||
private function authorizationsToCapture(Authorization ...$authorizations): array
|
||||
{
|
||||
private function authorizationsToCapture( Authorization ...$authorizations ): array {
|
||||
return array_filter(
|
||||
$authorizations,
|
||||
static function ( Authorization $authorization ): bool {
|
||||
|
|
|
@ -15,8 +15,8 @@ use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
class OrderProcessor
|
||||
{
|
||||
class OrderProcessor {
|
||||
|
||||
private $sessionHandler;
|
||||
private $cartRepository;
|
||||
private $orderEndpoint;
|
||||
|
@ -49,8 +49,7 @@ class OrderProcessor
|
|||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function process(\WC_Order $wcOrder, \WooCommerce $woocommerce): bool
|
||||
{
|
||||
public function process( \WC_Order $wcOrder, \WooCommerce $woocommerce ): bool {
|
||||
$order = $this->sessionHandler->order();
|
||||
$wcOrder->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $order->id() );
|
||||
$wcOrder->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
|
||||
|
@ -105,8 +104,7 @@ class OrderProcessor
|
|||
return true;
|
||||
}
|
||||
|
||||
private function captureAuthorizedDownloads(Order $order): bool
|
||||
{
|
||||
private function captureAuthorizedDownloads( Order $order ): bool {
|
||||
if (
|
||||
! $this->settings->has( 'capture_for_virtual_only' )
|
||||
|| ! $this->settings->get( 'capture_for_virtual_only' )
|
||||
|
@ -133,21 +131,18 @@ class OrderProcessor
|
|||
return true;
|
||||
}
|
||||
|
||||
public function lastError(): string
|
||||
{
|
||||
public function lastError(): string {
|
||||
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
public function patchOrder(\WC_Order $wcOrder, Order $order): Order
|
||||
{
|
||||
public function patchOrder( \WC_Order $wcOrder, Order $order ): Order {
|
||||
$updatedOrder = $this->orderFactory->fromWcOrder( $wcOrder, $order );
|
||||
$order = $this->orderEndpoint->patchOrderWith( $order, $updatedOrder );
|
||||
return $order;
|
||||
}
|
||||
|
||||
private function orderIsApproved(Order $order): bool
|
||||
{
|
||||
private function orderIsApproved( Order $order ): bool {
|
||||
|
||||
if ( $order->status()->is( OrderStatus::APPROVED ) ) {
|
||||
return true;
|
||||
|
@ -159,10 +154,10 @@ class OrderProcessor
|
|||
|
||||
$isApproved = in_array(
|
||||
$this->threedSecure->proceedWithOrder( $order ),
|
||||
[
|
||||
array(
|
||||
ThreeDSecure::NO_DECISION,
|
||||
ThreeDSecure::PROCCEED,
|
||||
],
|
||||
),
|
||||
true
|
||||
);
|
||||
return $isApproved;
|
||||
|
|
|
@ -8,54 +8,48 @@ use Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGatewayInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class Settings implements ContainerInterface
|
||||
{
|
||||
public const KEY = 'woocommerce-ppcp-settings';
|
||||
private $settings = [];
|
||||
class Settings implements ContainerInterface {
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
public const KEY = 'woocommerce-ppcp-settings';
|
||||
private $settings = array();
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
|
||||
// phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
|
||||
public function get($id)
|
||||
{
|
||||
public function get( $id ) {
|
||||
if ( ! $this->has( $id ) ) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return $this->settings[ $id ];
|
||||
}
|
||||
|
||||
public function has($id)
|
||||
{
|
||||
public function has( $id ) {
|
||||
$this->load();
|
||||
return array_key_exists( $id, $this->settings );
|
||||
}
|
||||
|
||||
public function set($id, $value)
|
||||
{
|
||||
public function set( $id, $value ) {
|
||||
$this->load();
|
||||
$this->settings[ $id ] = $value;
|
||||
}
|
||||
|
||||
public function persist()
|
||||
{
|
||||
public function persist() {
|
||||
|
||||
update_option( self::KEY, $this->settings );
|
||||
}
|
||||
|
||||
public function reset(): bool
|
||||
{
|
||||
public function reset(): bool {
|
||||
$this->load();
|
||||
$fieldsToReset = [
|
||||
$fieldsToReset = array(
|
||||
'enabled',
|
||||
'dcc_gateway_enabled',
|
||||
'intent',
|
||||
'client_id',
|
||||
'client_secret',
|
||||
'merchant_email',
|
||||
];
|
||||
);
|
||||
foreach ( $fieldsToReset as $id ) {
|
||||
$this->settings[ $id ] = null;
|
||||
}
|
||||
|
@ -63,15 +57,14 @@ class Settings implements ContainerInterface
|
|||
return true;
|
||||
}
|
||||
|
||||
private function load(): bool
|
||||
{
|
||||
private function load(): bool {
|
||||
|
||||
if ( $this->settings ) {
|
||||
return false;
|
||||
}
|
||||
$this->settings = get_option(self::KEY, []);
|
||||
$this->settings = get_option( self::KEY, array() );
|
||||
|
||||
$defaults = [
|
||||
$defaults = array(
|
||||
'title' => __( 'PayPal', 'woocommerce-paypal-commerce-gateway' ),
|
||||
'description' => __(
|
||||
'Pay via PayPal.',
|
||||
|
@ -86,7 +79,7 @@ class Settings implements ContainerInterface
|
|||
'Pay with your credit card.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
];
|
||||
);
|
||||
foreach ( $defaults as $key => $value ) {
|
||||
if ( isset( $this->settings[ $key ] ) ) {
|
||||
continue;
|
||||
|
|
|
@ -11,8 +11,8 @@ use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
|||
use Inpsyde\PayPalCommerce\Webhooks\WebhookRegistrar;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
class SettingsListener
|
||||
{
|
||||
class SettingsListener {
|
||||
|
||||
|
||||
public const NONCE = 'ppcp-settings';
|
||||
private $settings;
|
||||
|
@ -35,8 +35,7 @@ class SettingsListener
|
|||
$this->state = $state;
|
||||
}
|
||||
|
||||
public function listen()
|
||||
{
|
||||
public function listen() {
|
||||
|
||||
if ( ! $this->isValidUpdateRequest() ) {
|
||||
return;
|
||||
|
@ -62,7 +61,7 @@ class SettingsListener
|
|||
/**
|
||||
* Sanitization is done at a later stage.
|
||||
*/
|
||||
$rawData = (isset($_POST['ppcp'])) ? (array) wp_unslash($_POST['ppcp']) : [];
|
||||
$rawData = ( isset( $_POST['ppcp'] ) ) ? (array) wp_unslash( $_POST['ppcp'] ) : array();
|
||||
$settings = $this->retrieveSettingsFromRawData( $rawData );
|
||||
if ( $_GET['section'] === PayPalGateway::ID ) {
|
||||
$settings['enabled'] = isset( $_POST['woocommerce_ppcp-gateway_enabled'] )
|
||||
|
@ -87,8 +86,7 @@ class SettingsListener
|
|||
//phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
}
|
||||
|
||||
private function maybeRegisterWebhooks(array $settings)
|
||||
{
|
||||
private function maybeRegisterWebhooks( array $settings ) {
|
||||
|
||||
if ( ! $this->settings->has( 'client_id' ) && $settings['client_id'] ) {
|
||||
$this->webhookRegistrar->register();
|
||||
|
@ -109,12 +107,11 @@ class SettingsListener
|
|||
//phpcs:disable Inpsyde.CodeQuality.NestingLevel.MaxExceeded
|
||||
//phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
||||
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
private function retrieveSettingsFromRawData(array $rawData): array
|
||||
{
|
||||
private function retrieveSettingsFromRawData( array $rawData ): array {
|
||||
if ( ! isset( $_GET['section'] ) ) {
|
||||
return [];
|
||||
return array();
|
||||
}
|
||||
$settings = [];
|
||||
$settings = array();
|
||||
foreach ( $this->settingFields as $key => $config ) {
|
||||
if ( ! in_array( $this->state->currentState(), $config['screens'], true ) ) {
|
||||
continue;
|
||||
|
@ -147,8 +144,8 @@ class SettingsListener
|
|||
$settings[ $key ] = sanitize_text_field( $rawData[ $key ] );
|
||||
break;
|
||||
case 'ppcp-multiselect':
|
||||
$values = isset($rawData[$key]) ? (array) $rawData[$key] : [];
|
||||
$valuesToSave = [];
|
||||
$values = isset( $rawData[ $key ] ) ? (array) $rawData[ $key ] : array();
|
||||
$valuesToSave = array();
|
||||
foreach ( $values as $index => $rawValue ) {
|
||||
$value = sanitize_text_field( $rawValue );
|
||||
if ( ! in_array( $value, array_keys( $config['options'] ), true ) ) {
|
||||
|
@ -173,14 +170,13 @@ class SettingsListener
|
|||
//phpcs:enable Inpsyde.CodeQuality.NestingLevel.MaxExceeded
|
||||
//phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
|
||||
|
||||
private function isValidUpdateRequest(): bool
|
||||
{
|
||||
private function isValidUpdateRequest(): bool {
|
||||
|
||||
if (
|
||||
! isset( $_REQUEST['section'] )
|
||||
|| ! in_array(
|
||||
sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ),
|
||||
['ppcp-gateway', 'ppcp-credit-card-gateway'],
|
||||
array( 'ppcp-gateway', 'ppcp-credit-card-gateway' ),
|
||||
true
|
||||
)
|
||||
) {
|
||||
|
|
|
@ -9,8 +9,8 @@ use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply;
|
|||
use Inpsyde\PayPalCommerce\Onboarding\State;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class SettingsRenderer
|
||||
{
|
||||
class SettingsRenderer {
|
||||
|
||||
|
||||
private $settings;
|
||||
private $state;
|
||||
|
@ -33,14 +33,13 @@ class SettingsRenderer
|
|||
}
|
||||
|
||||
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
|
||||
public function renderMultiSelect($field, $key, $config, $value): string
|
||||
{
|
||||
public function renderMultiSelect( $field, $key, $config, $value ): string {
|
||||
|
||||
if ( $config['type'] !== 'ppcp-multiselect' ) {
|
||||
return $field;
|
||||
}
|
||||
|
||||
$options = [];
|
||||
$options = array();
|
||||
foreach ( $config['options'] as $optionKey => $optionValue ) {
|
||||
$selected = ( in_array( $optionKey, $value, true ) ) ? 'selected="selected"' : '';
|
||||
|
||||
|
@ -63,8 +62,7 @@ class SettingsRenderer
|
|||
return $html;
|
||||
}
|
||||
|
||||
public function renderPassword($field, $key, $config, $value): string
|
||||
{
|
||||
public function renderPassword( $field, $key, $config, $value ): string {
|
||||
|
||||
if ( $config['type'] !== 'ppcp-password' ) {
|
||||
return $field;
|
||||
|
@ -86,8 +84,7 @@ class SettingsRenderer
|
|||
return $html;
|
||||
}
|
||||
|
||||
public function renderTextInput($field, $key, $config, $value): string
|
||||
{
|
||||
public function renderTextInput( $field, $key, $config, $value ): string {
|
||||
|
||||
if ( $config['type'] !== 'ppcp-text-input' ) {
|
||||
return $field;
|
||||
|
@ -109,8 +106,7 @@ class SettingsRenderer
|
|||
return $html;
|
||||
}
|
||||
|
||||
public function renderHeading($field, $key, $config, $value): string
|
||||
{
|
||||
public function renderHeading( $field, $key, $config, $value ): string {
|
||||
|
||||
if ( $config['type'] !== 'ppcp-heading' ) {
|
||||
return $field;
|
||||
|
@ -128,8 +124,7 @@ class SettingsRenderer
|
|||
|
||||
//phpcs:disable Inpsyde.CodeQuality.NestingLevel.High
|
||||
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function render(bool $isDcc)
|
||||
{
|
||||
public function render( bool $isDcc ) {
|
||||
|
||||
$nonce = wp_create_nonce( SettingsListener::NONCE );
|
||||
?>
|
||||
|
@ -139,10 +134,10 @@ class SettingsRenderer
|
|||
if ( ! in_array( $this->state->currentState(), $config['screens'], true ) ) {
|
||||
continue;
|
||||
}
|
||||
if ($isDcc && ! in_array($config['gateway'], ['all', 'dcc'], true)) {
|
||||
if ( $isDcc && ! in_array( $config['gateway'], array( 'all', 'dcc' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
if (! $isDcc && ! in_array($config['gateway'], ['all', 'paypal'], true)) {
|
||||
if ( ! $isDcc && ! in_array( $config['gateway'], array( 'all', 'paypal' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
|
@ -176,17 +171,22 @@ class SettingsRenderer
|
|||
class="woocommerce-help-tip"
|
||||
data-tip="<?php echo esc_attr( $config['description'] ); ?>"
|
||||
></span>
|
||||
<?php unset($config['description']);
|
||||
endif; ?>
|
||||
<?php
|
||||
unset( $config['description'] );
|
||||
endif;
|
||||
?>
|
||||
</th>
|
||||
<?php endif; ?>
|
||||
<<?php echo esc_attr($thTd); ?> colspan="<?php echo (int) $colspan; ?>"><?php
|
||||
<<?php echo esc_attr( $thTd ); ?> colspan="<?php echo (int) $colspan; ?>">
|
||||
<?php
|
||||
$config['type'] === 'ppcp-text' ?
|
||||
$this->renderText( $config )
|
||||
: woocommerce_form_field($key, $config, $value); ?>
|
||||
: woocommerce_form_field( $key, $config, $value );
|
||||
?>
|
||||
</<?php echo esc_attr( $thTd ); ?>>
|
||||
</tr>
|
||||
<?php endforeach;
|
||||
<?php
|
||||
endforeach;
|
||||
|
||||
if ( $isDcc ) :
|
||||
?>
|
||||
|
@ -211,7 +211,8 @@ class SettingsRenderer
|
|||
'<a href = "#">',
|
||||
'</a>'
|
||||
)
|
||||
); ?>
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -220,8 +221,7 @@ class SettingsRenderer
|
|||
}
|
||||
//phpcs:enable Inpsyde.CodeQuality.NestingLevel.High
|
||||
|
||||
private function renderText(array $config)
|
||||
{
|
||||
private function renderText( array $config ) {
|
||||
echo wp_kses_post( $config['text'] );
|
||||
if ( isset( $config['hidden'] ) ) {
|
||||
$value = $this->settings->has( $config['hidden'] ) ?
|
||||
|
|
|
@ -23,18 +23,16 @@ use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
|
|||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class WcGatewayModule implements ModuleInterface
|
||||
{
|
||||
public function setup(): ServiceProviderInterface
|
||||
{
|
||||
class WcGatewayModule implements ModuleInterface {
|
||||
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
public function run(ContainerInterface $container)
|
||||
{
|
||||
public function run( ContainerInterface $container ) {
|
||||
$this->registerPaymentGateway( $container );
|
||||
$this->registerOrderFunctionality( $container );
|
||||
$this->registerColumns( $container );
|
||||
|
@ -83,8 +81,7 @@ class WcGatewayModule implements ModuleInterface
|
|||
);
|
||||
}
|
||||
|
||||
private function ajaxGatewayEnabler(ContainerInterface $container)
|
||||
{
|
||||
private function ajaxGatewayEnabler( ContainerInterface $container ) {
|
||||
add_action(
|
||||
'wp_ajax_woocommerce_toggle_gateway_enabled',
|
||||
static function () use ( $container ) {
|
||||
|
@ -121,8 +118,7 @@ class WcGatewayModule implements ModuleInterface
|
|||
);
|
||||
}
|
||||
|
||||
private function registerPaymentGateWay(ContainerInterface $container)
|
||||
{
|
||||
private function registerPaymentGateWay( ContainerInterface $container ) {
|
||||
|
||||
add_filter(
|
||||
'woocommerce_payment_gateways',
|
||||
|
@ -176,8 +172,7 @@ class WcGatewayModule implements ModuleInterface
|
|||
);
|
||||
}
|
||||
|
||||
private function registerOrderFunctionality(ContainerInterface $container)
|
||||
{
|
||||
private function registerOrderFunctionality( ContainerInterface $container ) {
|
||||
add_filter(
|
||||
'woocommerce_order_actions',
|
||||
static function ( $orderActions ): array {
|
||||
|
@ -201,8 +196,7 @@ class WcGatewayModule implements ModuleInterface
|
|||
);
|
||||
}
|
||||
|
||||
private function registerColumns(ContainerInterface $container)
|
||||
{
|
||||
private function registerColumns( ContainerInterface $container ) {
|
||||
add_action(
|
||||
'woocommerce_order_actions_start',
|
||||
static function ( $wcOrderId ) use ( $container ) {
|
||||
|
@ -239,8 +233,7 @@ class WcGatewayModule implements ModuleInterface
|
|||
);
|
||||
}
|
||||
|
||||
private function registerCheckoutAddressPreset(ContainerInterface $container): void
|
||||
{
|
||||
private function registerCheckoutAddressPreset( ContainerInterface $container ): void {
|
||||
add_filter(
|
||||
'woocommerce_checkout_get_value',
|
||||
static function ( ...$args ) use ( $container ) {
|
||||
|
|
|
@ -8,36 +8,32 @@ use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class CheckoutOrderApproved implements RequestHandler
|
||||
{
|
||||
class CheckoutOrderApproved implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
private $logger;
|
||||
private $orderEndpoint;
|
||||
|
||||
public function __construct(LoggerInterface $logger, string $prefix, OrderEndpoint $orderEndpoint)
|
||||
{
|
||||
public function __construct( LoggerInterface $logger, string $prefix, OrderEndpoint $orderEndpoint ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
$this->orderEndpoint = $orderEndpoint;
|
||||
}
|
||||
|
||||
public function eventTypes(): array
|
||||
{
|
||||
return [
|
||||
public function eventTypes(): array {
|
||||
return array(
|
||||
'CHECKOUT.ORDER.APPROVED',
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||
{
|
||||
public function responsibleForRequest( \WP_REST_Request $request ): bool {
|
||||
return in_array( $request['event_type'], $this->eventTypes(), true );
|
||||
}
|
||||
|
||||
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
public function handleRequest( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$customIds = array_filter(
|
||||
array_map(
|
||||
static function ( array $purchaseUnit ): string {
|
||||
|
@ -45,7 +41,7 @@ class CheckoutOrderApproved implements RequestHandler
|
|||
(string) $purchaseUnit['custom_id'] : '';
|
||||
},
|
||||
isset( $request['resource']['purchase_units'] ) ?
|
||||
(array) $request['resource']['purchase_units'] : []
|
||||
(array) $request['resource']['purchase_units'] : array()
|
||||
),
|
||||
static function ( string $orderId ): bool {
|
||||
return ! empty( $orderId );
|
||||
|
@ -64,9 +60,9 @@ class CheckoutOrderApproved implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -87,9 +83,9 @@ class CheckoutOrderApproved implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -110,25 +106,25 @@ class CheckoutOrderApproved implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
$wcOrderIds = array_map(
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'sanitizeCustomId',
|
||||
],
|
||||
),
|
||||
$customIds
|
||||
);
|
||||
$args = [
|
||||
$args = array(
|
||||
'post__in' => $wcOrderIds,
|
||||
'limit' => -1,
|
||||
];
|
||||
);
|
||||
$wcOrders = wc_get_orders( $args );
|
||||
if ( ! $wcOrders ) {
|
||||
$message = sprintf(
|
||||
|
@ -139,9 +135,9 @@ class CheckoutOrderApproved implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -152,7 +148,7 @@ class CheckoutOrderApproved implements RequestHandler
|
|||
__( 'Payment received.', 'woocommerce-paypal-commerce-gateway' )
|
||||
: __( 'Payment can be captured.', 'woocommerce-paypal-commerce-gateway' );
|
||||
foreach ( $wcOrders as $wcOrder ) {
|
||||
if (! in_array($wcOrder->get_status(), ['pending', 'on-hold'], true)) {
|
||||
if ( ! in_array( $wcOrder->get_status(), array( 'pending', 'on-hold' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
/**
|
||||
|
@ -172,10 +168,10 @@ class CheckoutOrderApproved implements RequestHandler
|
|||
),
|
||||
(string) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
$response['success'] = true;
|
||||
|
|
|
@ -6,33 +6,29 @@ namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
|||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class CheckoutOrderCompleted implements RequestHandler
|
||||
{
|
||||
class CheckoutOrderCompleted implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
private $logger;
|
||||
public function __construct(LoggerInterface $logger, string $prefix)
|
||||
{
|
||||
public function __construct( LoggerInterface $logger, string $prefix ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function eventTypes(): array
|
||||
{
|
||||
return [
|
||||
public function eventTypes(): array {
|
||||
return array(
|
||||
'CHECKOUT.ORDER.COMPLETED',
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||
{
|
||||
public function responsibleForRequest( \WP_REST_Request $request ): bool {
|
||||
return in_array( $request['event_type'], $this->eventTypes(), true );
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
public function handleRequest( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$customIds = array_filter(
|
||||
array_map(
|
||||
static function ( array $purchaseUnit ): string {
|
||||
|
@ -40,7 +36,7 @@ class CheckoutOrderCompleted implements RequestHandler
|
|||
(string) $purchaseUnit['custom_id'] : '';
|
||||
},
|
||||
isset( $request['resource']['purchase_units'] ) ?
|
||||
(array) $request['resource']['purchase_units'] : []
|
||||
(array) $request['resource']['purchase_units'] : array()
|
||||
),
|
||||
static function ( string $orderId ): bool {
|
||||
return ! empty( $orderId );
|
||||
|
@ -59,25 +55,25 @@ class CheckoutOrderCompleted implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
$orderIds = array_map(
|
||||
[
|
||||
array(
|
||||
$this,
|
||||
'sanitizeCustomId',
|
||||
],
|
||||
),
|
||||
$customIds
|
||||
);
|
||||
$args = [
|
||||
$args = array(
|
||||
'post__in' => $orderIds,
|
||||
'limit' => -1,
|
||||
];
|
||||
);
|
||||
$wcOrders = wc_get_orders( $args );
|
||||
if ( ! $wcOrders ) {
|
||||
$message = sprintf(
|
||||
|
@ -88,16 +84,16 @@ class CheckoutOrderCompleted implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
foreach ( $wcOrders as $wcOrder ) {
|
||||
if (! in_array($wcOrder->get_status(), ['pending', 'on-hold'], true)) {
|
||||
if ( ! in_array( $wcOrder->get_status(), array( 'pending', 'on-hold' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
/**
|
||||
|
@ -117,10 +113,10 @@ class CheckoutOrderCompleted implements RequestHandler
|
|||
),
|
||||
(string) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
$response['success'] = true;
|
||||
|
|
|
@ -7,31 +7,27 @@ namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
|||
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class PaymentCaptureCompleted implements RequestHandler
|
||||
{
|
||||
class PaymentCaptureCompleted implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
private $logger;
|
||||
public function __construct(LoggerInterface $logger, string $prefix)
|
||||
{
|
||||
public function __construct( LoggerInterface $logger, string $prefix ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function eventTypes(): array
|
||||
{
|
||||
return ['PAYMENT.CAPTURE.COMPLETED'];
|
||||
public function eventTypes(): array {
|
||||
return array( 'PAYMENT.CAPTURE.COMPLETED' );
|
||||
}
|
||||
|
||||
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||
{
|
||||
public function responsibleForRequest( \WP_REST_Request $request ): bool {
|
||||
return in_array( $request['event_type'], $this->eventTypes(), true );
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
public function handleRequest( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$orderId = isset( $request['resource']['custom_id'] ) ?
|
||||
$this->sanitizeCustomId( $request['resource']['custom_id'] ) : 0;
|
||||
if ( ! $orderId ) {
|
||||
|
@ -46,9 +42,9 @@ class PaymentCaptureCompleted implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -67,9 +63,9 @@ class PaymentCaptureCompleted implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -96,10 +92,10 @@ class PaymentCaptureCompleted implements RequestHandler
|
|||
),
|
||||
(string) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['success'] = true;
|
||||
return rest_ensure_response( $response );
|
||||
|
|
|
@ -6,31 +6,27 @@ namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
|||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class PaymentCaptureRefunded implements RequestHandler
|
||||
{
|
||||
class PaymentCaptureRefunded implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
private $logger;
|
||||
public function __construct(LoggerInterface $logger, string $prefix)
|
||||
{
|
||||
public function __construct( LoggerInterface $logger, string $prefix ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function eventTypes(): array
|
||||
{
|
||||
return ['PAYMENT.CAPTURE.REFUNDED'];
|
||||
public function eventTypes(): array {
|
||||
return array( 'PAYMENT.CAPTURE.REFUNDED' );
|
||||
}
|
||||
|
||||
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||
{
|
||||
public function responsibleForRequest( \WP_REST_Request $request ): bool {
|
||||
return in_array( $request['event_type'], $this->eventTypes(), true );
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
public function handleRequest( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$orderId = isset( $request['resource']['custom_id'] ) ?
|
||||
$this->sanitizeCustomId( $request['resource']['custom_id'] ) : 0;
|
||||
if ( ! $orderId ) {
|
||||
|
@ -45,9 +41,9 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -63,9 +59,9 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -74,10 +70,12 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
/**
|
||||
* @var \WC_Order $wcOrder
|
||||
*/
|
||||
$refund = wc_create_refund([
|
||||
$refund = wc_create_refund(
|
||||
array(
|
||||
'order_id' => $wcOrder->get_id(),
|
||||
'amount' => $request['resource']['amount']['value'],
|
||||
]);
|
||||
)
|
||||
);
|
||||
if ( is_wp_error( $refund ) ) {
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
|
@ -86,10 +84,10 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
__( 'Order %s could not be refunded', 'woocommerce-paypal-commerce-gateway' ),
|
||||
(string) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
'error' => $refund,
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$response['message'] = $refund->get_error_message();
|
||||
|
@ -107,10 +105,10 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
(string) $wcOrder->get_id(),
|
||||
(string) $refund->get_amount()
|
||||
),
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['success'] = true;
|
||||
return rest_ensure_response( $response );
|
||||
|
|
|
@ -6,35 +6,31 @@ namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
|||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class PaymentCaptureReversed implements RequestHandler
|
||||
{
|
||||
class PaymentCaptureReversed implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
private $logger;
|
||||
public function __construct(LoggerInterface $logger, string $prefix)
|
||||
{
|
||||
public function __construct( LoggerInterface $logger, string $prefix ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function eventTypes(): array
|
||||
{
|
||||
return [
|
||||
public function eventTypes(): array {
|
||||
return array(
|
||||
'PAYMENT.CAPTURE.REVERSED',
|
||||
'PAYMENT.ORDER.CANCELLED',
|
||||
'PAYMENT.CAPTURE.DENIED',
|
||||
];
|
||||
);
|
||||
}
|
||||
|
||||
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||
{
|
||||
public function responsibleForRequest( \WP_REST_Request $request ): bool {
|
||||
return in_array( $request['event_type'], $this->eventTypes(), true );
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
public function handleRequest( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$orderId = isset( $request['resource']['custom_id'] ) ?
|
||||
$this->sanitizeCustomId( $request['resource']['custom_id'] ) : 0;
|
||||
if ( ! $orderId ) {
|
||||
|
@ -49,9 +45,9 @@ class PaymentCaptureReversed implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -67,9 +63,9 @@ class PaymentCaptureReversed implements RequestHandler
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response['message'] = $message;
|
||||
return rest_ensure_response( $response );
|
||||
|
@ -95,10 +91,10 @@ class PaymentCaptureReversed implements RequestHandler
|
|||
$this->logger->log(
|
||||
$response['success'] ? 'info' : 'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
'order' => $wcOrder,
|
||||
]
|
||||
)
|
||||
);
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
|
|
@ -4,13 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
trait PrefixTrait
|
||||
{
|
||||
trait PrefixTrait {
|
||||
|
||||
|
||||
private $prefix = '';
|
||||
|
||||
private function sanitizeCustomId(string $customId): int
|
||||
{
|
||||
private function sanitizeCustomId( string $customId ): int {
|
||||
|
||||
$orderId = str_replace( $this->prefix, '', $customId );
|
||||
return (int) $orderId;
|
||||
|
|
|
@ -4,8 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
interface RequestHandler
|
||||
{
|
||||
interface RequestHandler {
|
||||
|
||||
|
||||
public function eventTypes(): array;
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@ use Inpsyde\PayPalCommerce\ApiClient\Factory\WebhookFactory;
|
|||
use Inpsyde\PayPalCommerce\Webhooks\Handler\RequestHandler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class IncomingWebhookEndpoint
|
||||
{
|
||||
class IncomingWebhookEndpoint {
|
||||
|
||||
|
||||
public const NAMESPACE = 'paypal/v1';
|
||||
public const ROUTE = 'incoming';
|
||||
|
@ -35,35 +35,33 @@ class IncomingWebhookEndpoint
|
|||
$this->verifyRequest = $verifyRequest;
|
||||
}
|
||||
|
||||
public function register(): bool
|
||||
{
|
||||
public function register(): bool {
|
||||
return (bool) register_rest_route(
|
||||
self::NAMESPACE,
|
||||
self::ROUTE,
|
||||
[
|
||||
'methods' => [
|
||||
array(
|
||||
'methods' => array(
|
||||
'POST',
|
||||
],
|
||||
'callback' => [
|
||||
),
|
||||
'callback' => array(
|
||||
$this,
|
||||
'handleRequest',
|
||||
],
|
||||
'permission_callback' => [
|
||||
),
|
||||
'permission_callback' => array(
|
||||
$this,
|
||||
'verifyRequest',
|
||||
],
|
||||
),
|
||||
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function verifyRequest(): bool
|
||||
{
|
||||
public function verifyRequest(): bool {
|
||||
if ( ! $this->verifyRequest ) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
$data = (array) get_option(WebhookRegistrar::KEY, []);
|
||||
$data = (array) get_option( WebhookRegistrar::KEY, array() );
|
||||
$webhook = $this->webhookFactory->fromArray( $data );
|
||||
$result = $this->webhookEndpoint->verifyCurrentRequestForWebhook( $webhook );
|
||||
if ( ! $result ) {
|
||||
|
@ -89,8 +87,7 @@ class IncomingWebhookEndpoint
|
|||
}
|
||||
}
|
||||
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
public function handleRequest( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
|
||||
foreach ( $this->handlers as $handler ) {
|
||||
if ( $handler->responsibleForRequest( $request ) ) {
|
||||
|
@ -102,10 +99,10 @@ class IncomingWebhookEndpoint
|
|||
__( 'Webhook has been handled by %s', 'woocommerce-paypal-commerce-gateway' ),
|
||||
( $handler->eventTypes() ) ? current( $handler->eventTypes() ) : ''
|
||||
),
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
'response' => $response,
|
||||
]
|
||||
)
|
||||
);
|
||||
return $response;
|
||||
}
|
||||
|
@ -119,22 +116,23 @@ class IncomingWebhookEndpoint
|
|||
$this->logger->log(
|
||||
'warning',
|
||||
$message,
|
||||
[
|
||||
array(
|
||||
'request' => $request,
|
||||
]
|
||||
)
|
||||
);
|
||||
$response = array(
|
||||
'success' => false,
|
||||
'message' => $message,
|
||||
);
|
||||
$response = ['success' => false, 'message' => $message];
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
public function url(): string
|
||||
{
|
||||
public function url(): string {
|
||||
return rest_url( self::NAMESPACE . '/' . self::ROUTE );
|
||||
}
|
||||
|
||||
public function handledEventTypes(): array
|
||||
{
|
||||
$eventTypes = [];
|
||||
public function handledEventTypes(): array {
|
||||
$eventTypes = array();
|
||||
foreach ( $this->handlers as $handler ) {
|
||||
$eventTypes = array_merge( $eventTypes, $handler->eventTypes() );
|
||||
}
|
||||
|
|
|
@ -13,18 +13,16 @@ use Inpsyde\PayPalCommerce\Onboarding\Render\OnboardingRenderer;
|
|||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class WebhookModule implements ModuleInterface
|
||||
{
|
||||
public function setup(): ServiceProviderInterface
|
||||
{
|
||||
class WebhookModule implements ModuleInterface {
|
||||
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
public function run(ContainerInterface $container)
|
||||
{
|
||||
public function run( ContainerInterface $container ) {
|
||||
add_action(
|
||||
'rest_api_init',
|
||||
static function () use ( $container ) {
|
||||
|
|
|
@ -8,8 +8,8 @@ use Inpsyde\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\WebhookFactory;
|
||||
|
||||
class WebhookRegistrar
|
||||
{
|
||||
class WebhookRegistrar {
|
||||
|
||||
|
||||
public const EVENT_HOOK = 'ppcp-register-event';
|
||||
public const KEY = 'ppcp-webhook';
|
||||
|
@ -28,8 +28,7 @@ class WebhookRegistrar
|
|||
$this->restEndpoint = $restEndpoint;
|
||||
}
|
||||
|
||||
public function register(): bool
|
||||
{
|
||||
public function register(): bool {
|
||||
$webhook = $this->webhookFactory->forUrlAndEvents(
|
||||
$this->restEndpoint->url(),
|
||||
$this->restEndpoint->handledEventTypes()
|
||||
|
@ -54,9 +53,8 @@ class WebhookRegistrar
|
|||
}
|
||||
}
|
||||
|
||||
public function unregister(): bool
|
||||
{
|
||||
$data = (array) get_option(self::KEY, []);
|
||||
public function unregister(): bool {
|
||||
$data = (array) get_option( self::KEY, array() );
|
||||
if ( ! $data ) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -9,16 +9,14 @@ use Dhii\Modular\Module\ModuleInterface;
|
|||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class PluginModule implements ModuleInterface
|
||||
{
|
||||
class PluginModule implements ModuleInterface {
|
||||
|
||||
public function setup(): ServiceProviderInterface
|
||||
{
|
||||
return new ServiceProvider([], []);
|
||||
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider( array(), array() );
|
||||
}
|
||||
|
||||
public function run(ContainerInterface $container)
|
||||
{
|
||||
public function run( ContainerInterface $container ) {
|
||||
// TODO: Implement run() method.
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue