This commit is contained in:
David Remer 2020-07-28 07:15:04 +03:00
parent e5494bfc8b
commit c98e10d2a1
16 changed files with 89 additions and 50 deletions

View file

@ -17,7 +17,9 @@ class DisabledSmartButton implements SmartButtonInterface
return true;
}
public function canSaveVaultToken(): bool {
public function canSaveVaultToken(): bool
{
return false;
}
}

View file

@ -11,5 +11,4 @@ interface SmartButtonInterface
public function enqueue(): bool;
public function canSaveVaultToken(): bool;
}

View file

@ -31,12 +31,17 @@ class OnboardingAssets
$url = $this->moduleUrl . '/assets/css/onboarding.css';
wp_register_style(
'ppcp-onboarding',
$url
$url,
[],
1
);
$url = $this->moduleUrl . '/assets/js/settings.js';
wp_register_script(
'ppcp-settings',
$url
$url,
[],
1,
true
);
if (!$this->shouldRenderOnboardingScript()) {
return false;
@ -63,7 +68,6 @@ class OnboardingAssets
]
);
return true;
}

View file

@ -1,6 +1,7 @@
<?php
declare(strict_types=1);
return [
];
];

View file

@ -9,10 +9,10 @@ use Inpsyde\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
use Psr\Container\ContainerInterface;
return [
'subscription.helper' => function(ContainerInterface $container) : SubscriptionHelper {
'subscription.helper' => static function (ContainerInterface $container): SubscriptionHelper {
return new SubscriptionHelper();
},
'subscription.renewal-handler' => function(ContainerInterface $container) : RenewalHandler {
'subscription.renewal-handler' => static function (ContainerInterface $container): RenewalHandler {
$logger = $container->get('woocommerce.logger.woocommerce');
$repository = $container->get('subscription.repository.payment-token');
$endpoint = $container->get('api.endpoint.order');
@ -26,9 +26,9 @@ return [
$payerFactory
);
},
'subscription.repository.payment-token' => function(ContainerInterface $container) : PaymentTokenRepository {
'subscription.repository.payment-token' => static function (ContainerInterface $container): PaymentTokenRepository {
$factory = $container->get('api.factory.payment-token');
$endpoint = $container->get('api.endpoint.payment-token');
return new PaymentTokenRepository($factory, $endpoint);
}
},
];

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Subscription\Helper;
@ -6,7 +7,7 @@ namespace Inpsyde\PayPalCommerce\Subscription\Helper;
class SubscriptionHelper
{
public function currentProductIsSubscription() : bool
public function currentProductIsSubscription(): bool
{
if (! $this->pluginIsActive()) {
return false;
@ -15,7 +16,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;
@ -37,15 +38,24 @@ class SubscriptionHelper
return false;
}
public function acceptOnlyAutomaticPaymentGateways() : bool {
public function acceptOnlyAutomaticPaymentGateways(): bool
{
if (! $this->pluginIsActive()) {
return false;
}
$accept_manual_renewals = ( 'no' !== get_option( \WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals', 'no' ) ) ? true : false;
return ! $accept_manual_renewals;
$acceptManualRenewals = ( 'no' !== get_option(
//phpcs:disable Inpsyde.CodeQuality.VariablesName.SnakeCaseVar
\WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals',
//phpcs:enable Inpsyde.CodeQuality.VariablesName.SnakeCaseVar
'no'
) ) ? true : false;
return ! $acceptManualRenewals;
}
public function pluginIsActive() {
public function pluginIsActive(): bool
{
return class_exists(\WC_Subscriptions::class);
}
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Subscription;
@ -36,7 +37,9 @@ class RenewalHandler
$this->payerFactory = $payerFactory;
}
public function renew(\WC_Order $wcOrder) {
public function renew(\WC_Order $wcOrder)
{
$this->logger->log(
'info',
sprintf(
@ -50,16 +53,7 @@ class RenewalHandler
);
try {
$userId = (int)$wcOrder->get_customer_id();
$customer = new \WC_Customer($userId);
$token = $this->getTokenForCustomer($customer, $wcOrder);
if (! $token) {
return;
}
$purchaseUnits = $this->purchaseUnitFactory->fromWcOrder($wcOrder);
$payer = $this->payerFactory->fromCustomer($customer);
$order = $this->orderEndpoint->createForPurchaseUnits([$purchaseUnits], $payer, $token, (string) $wcOrder->get_id());
$this->captureOrder($order, $wcOrder);
$this->processOrder($wcOrder);
} catch (\Exception $error) {
$this->logger->log(
'error',
@ -92,7 +86,28 @@ class RenewalHandler
);
}
private function getTokenForCustomer(\WC_Customer $customer, \WC_Order $wcOrder) : ?PaymentToken {
private function processOrder(\WC_Order $wcOrder)
{
$userId = (int)$wcOrder->get_customer_id();
$customer = new \WC_Customer($userId);
$token = $this->getTokenForCustomer($customer, $wcOrder);
if (! $token) {
return;
}
$purchaseUnits = $this->purchaseUnitFactory->fromWcOrder($wcOrder);
$payer = $this->payerFactory->fromCustomer($customer);
$order = $this->orderEndpoint->createForPurchaseUnits(
[$purchaseUnits],
$payer,
$token,
(string) $wcOrder->get_id()
);
$this->captureOrder($order, $wcOrder);
}
private function getTokenForCustomer(\WC_Customer $customer, \WC_Order $wcOrder): ?PaymentToken
{
$token = $this->repository->forUserId((int) $customer->get_id());
if (!$token) {
@ -113,7 +128,8 @@ class RenewalHandler
return $token;
}
private function captureOrder(Order $order, \WC_Order $wcOrder) {
private function captureOrder(Order $order, \WC_Order $wcOrder)
{
if ($order->intent() === 'CAPTURE') {
$order = $this->orderEndpoint->capture($order);
@ -122,14 +138,14 @@ class RenewalHandler
'processing',
__('Payment received.', 'woocommerce-paypal-commerce-gateway')
);
\WC_Subscriptions_Manager::process_subscription_payments_on_order( $wcOrder );
\WC_Subscriptions_Manager::process_subscription_payments_on_order($wcOrder);
}
}
if ($order->intent() === 'AUTHORIZE') {
$this->orderEndpoint->authorize($order);
$wcOrder->update_meta_data(WcGateway::CAPTURED_META_KEY, 'false');
\WC_Subscriptions_Manager::process_subscription_payments_on_order( $wcOrder );
\WC_Subscriptions_Manager::process_subscription_payments_on_order($wcOrder);
}
}
}

View file

@ -1,9 +1,9 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Subscription\Repository;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Entity\PaymentToken;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
@ -18,13 +18,13 @@ class PaymentTokenRepository
public function __construct(
PaymentTokenFactory $factory,
PaymentTokenEndpoint $endpoint
)
{
) {
$this->factory = $factory;
$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);
@ -39,13 +39,14 @@ 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);
@ -53,4 +54,4 @@ class PaymentTokenRepository
update_user_meta($id, self::USER_META, $tokenArray);
return $token;
}
}
}

View file

@ -38,6 +38,5 @@ class SubscriptionModule implements ModuleInterface
10,
2
);
}
}

View file

@ -72,6 +72,7 @@ class SettingsListener
}
//phpcs:disable Inpsyde.CodeQuality.NestingLevel.MaxExceeded
//phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
private function retrieveSettingsFromRawData(array $rawData): array
{
$settings = [];
@ -117,6 +118,7 @@ class SettingsListener
return $settings;
}
//phpcs:enable Inpsyde.CodeQuality.NestingLevel.MaxExceeded
//phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
private function isValidUpdateRequest(): bool
{

View file

@ -8,8 +8,8 @@ use Psr\Log\LoggerInterface;
class CheckoutOrderCompleted implements RequestHandler
{
use PrefixTrait;
private $logger;
public function __construct(LoggerInterface $logger, string $prefix)
{

View file

@ -9,8 +9,8 @@ use Psr\Log\LoggerInterface;
class PaymentCaptureCompleted implements RequestHandler
{
use PrefixTrait;
private $logger;
public function __construct(LoggerInterface $logger, string $prefix)
{
@ -32,7 +32,8 @@ class PaymentCaptureCompleted implements RequestHandler
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
{
$response = ['success' => false];
$orderId = isset($request['resource']['custom_id']) ? $this->sanitizeCustomId($request['resource']['custom_id']) : 0;
$orderId = isset($request['resource']['custom_id']) ?
$this->sanitizeCustomId($request['resource']['custom_id']) : 0;
if (! $orderId) {
$message = sprintf(
// translators: %s is the PayPal webhook Id.

View file

@ -8,8 +8,8 @@ use Psr\Log\LoggerInterface;
class PaymentCaptureRefunded implements RequestHandler
{
use PrefixTrait;
private $logger;
public function __construct(LoggerInterface $logger, string $prefix)
{
@ -31,7 +31,8 @@ class PaymentCaptureRefunded implements RequestHandler
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
{
$response = ['success' => false];
$orderId = isset($request['resource']['custom_id']) ? $this->sanitizeCustomId($request['resource']['custom_id']) : 0;
$orderId = isset($request['resource']['custom_id']) ?
$this->sanitizeCustomId($request['resource']['custom_id']) : 0;
if (! $orderId) {
$message = sprintf(
// translators: %s is the PayPal webhook Id.

View file

@ -8,8 +8,8 @@ use Psr\Log\LoggerInterface;
class PaymentCaptureReversed implements RequestHandler
{
use PrefixTrait;
private $logger;
public function __construct(LoggerInterface $logger, string $prefix)
{
@ -35,7 +35,8 @@ class PaymentCaptureReversed implements RequestHandler
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
{
$response = ['success' => false];
$orderId = isset($request['resource']['custom_id']) ? $this->sanitizeCustomId($request['resource']['custom_id']) : 0;
$orderId = isset($request['resource']['custom_id']) ?
$this->sanitizeCustomId($request['resource']['custom_id']) : 0;
if (! $orderId) {
$message = sprintf(
// translators: %s is the PayPal webhook Id.

View file

@ -1,16 +1,18 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
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;
}
}
}