mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
introduce installation prefix
This commit is contained in:
parent
6a4b406568
commit
8f39b71e7f
9 changed files with 84 additions and 14 deletions
|
@ -35,6 +35,10 @@ return [
|
||||||
$settings = $container->get('wcgateway.settings');
|
$settings = $container->get('wcgateway.settings');
|
||||||
return $settings->has('client_secret') ? (string) $settings->get('client_secret') : '';
|
return $settings->has('client_secret') ? (string) $settings->get('client_secret') : '';
|
||||||
},
|
},
|
||||||
|
'api.prefix' => static function (ContainerInterface $container): string {
|
||||||
|
$settings = $container->get('wcgateway.settings');
|
||||||
|
return $settings->has('prefix') ? (string) $settings->get('prefix') : 'WC-';
|
||||||
|
},
|
||||||
'api.endpoint.order' => static function (ContainerInterface $container): OrderEndpoint {
|
'api.endpoint.order' => static function (ContainerInterface $container): OrderEndpoint {
|
||||||
$orderFactory = $container->get('api.factory.order');
|
$orderFactory = $container->get('api.factory.order');
|
||||||
$patchCollectionFactory = $container->get('api.factory.patch-collection-factory');
|
$patchCollectionFactory = $container->get('api.factory.patch-collection-factory');
|
||||||
|
|
|
@ -529,6 +529,19 @@ return [
|
||||||
],
|
],
|
||||||
'requirements' => [],
|
'requirements' => [],
|
||||||
],
|
],
|
||||||
|
'prefix' => [
|
||||||
|
'title' => __('Installation prefix', 'woocommerce-paypal-commerce-gateway'),
|
||||||
|
'type' => 'text',
|
||||||
|
'desc_tip' => true,
|
||||||
|
'description' => __('If you use your PayPal account with more than one installation, please use a distinct prefix to seperate those installations. Please do not use numbers in your prefix.', 'woocommerce-paypal-commerce-gateway'),
|
||||||
|
'default' => 'WC-',
|
||||||
|
'screens' => [
|
||||||
|
State::STATE_START,
|
||||||
|
State::STATE_PROGRESSIVE,
|
||||||
|
State::STATE_ONBOARDED,
|
||||||
|
],
|
||||||
|
'requirements' => [],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -86,6 +86,12 @@ class SettingsListener
|
||||||
case 'text':
|
case 'text':
|
||||||
$settings[$key] = isset($rawData[$key]) ? sanitize_text_field($rawData[$key]) : '';
|
$settings[$key] = isset($rawData[$key]) ? sanitize_text_field($rawData[$key]) : '';
|
||||||
break;
|
break;
|
||||||
|
case 'password':
|
||||||
|
if (empty($rawData[$key])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$settings[$key] = sanitize_text_field($rawData[$key]);
|
||||||
|
break;
|
||||||
case 'ppcp-multiselect':
|
case 'ppcp-multiselect':
|
||||||
$values = isset($rawData[$key]) ? (array) $rawData[$key] : [];
|
$values = isset($rawData[$key]) ? (array) $rawData[$key] : [];
|
||||||
$valuesToSave = [];
|
$valuesToSave = [];
|
||||||
|
|
|
@ -39,11 +39,12 @@ return [
|
||||||
},
|
},
|
||||||
'webhook.endpoint.handler' => function(ContainerInterface $container) : array {
|
'webhook.endpoint.handler' => function(ContainerInterface $container) : array {
|
||||||
$logger = $container->get('woocommerce.logger.woocommerce');
|
$logger = $container->get('woocommerce.logger.woocommerce');
|
||||||
|
$prefix = $container->get('api.prefix');
|
||||||
return [
|
return [
|
||||||
new CheckoutOrderCompleted($logger),
|
new CheckoutOrderCompleted($logger, $prefix),
|
||||||
new PaymentCaptureRefunded($logger),
|
new PaymentCaptureRefunded($logger, $prefix),
|
||||||
new PaymentCaptureReversed($logger),
|
new PaymentCaptureReversed($logger, $prefix),
|
||||||
new PaymentCaptureCompleted($logger),
|
new PaymentCaptureCompleted($logger, $prefix),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
@ -9,10 +9,12 @@ use Psr\Log\LoggerInterface;
|
||||||
class CheckoutOrderCompleted implements RequestHandler
|
class CheckoutOrderCompleted implements RequestHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use PrefixTrait;
|
||||||
private $logger;
|
private $logger;
|
||||||
public function __construct(LoggerInterface $logger)
|
public function __construct(LoggerInterface $logger, string $prefix)
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->prefix = $prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eventTypes(): array
|
public function eventTypes(): array
|
||||||
|
@ -32,7 +34,7 @@ class CheckoutOrderCompleted implements RequestHandler
|
||||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||||
{
|
{
|
||||||
$response = ['success' => false];
|
$response = ['success' => false];
|
||||||
$orderIds = array_filter(
|
$customIds = array_filter(
|
||||||
array_map(
|
array_map(
|
||||||
static function (array $purchaseUnit): string {
|
static function (array $purchaseUnit): string {
|
||||||
return isset($purchaseUnit['custom_id']) ? (string) $purchaseUnit['custom_id'] : '';
|
return isset($purchaseUnit['custom_id']) ? (string) $purchaseUnit['custom_id'] : '';
|
||||||
|
@ -45,7 +47,7 @@ class CheckoutOrderCompleted implements RequestHandler
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (empty($orderIds)) {
|
if (empty($customIds)) {
|
||||||
$message = sprintf(
|
$message = sprintf(
|
||||||
// translators: %s is the PayPal webhook Id.
|
// translators: %s is the PayPal webhook Id.
|
||||||
__('No order for webhook event %s was found.', 'woocommerce-paypal-commerce-gateway'),
|
__('No order for webhook event %s was found.', 'woocommerce-paypal-commerce-gateway'),
|
||||||
|
@ -62,6 +64,13 @@ class CheckoutOrderCompleted implements RequestHandler
|
||||||
return rest_ensure_response($response);
|
return rest_ensure_response($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$orderIds = array_map(
|
||||||
|
[
|
||||||
|
$this,
|
||||||
|
'sanitizeCustomId',
|
||||||
|
],
|
||||||
|
$customIds
|
||||||
|
);
|
||||||
$args = [
|
$args = [
|
||||||
'post__in' => $orderIds,
|
'post__in' => $orderIds,
|
||||||
'limit' => -1,
|
'limit' => -1,
|
||||||
|
|
|
@ -10,11 +10,12 @@ use Psr\Log\LoggerInterface;
|
||||||
class PaymentCaptureCompleted implements RequestHandler
|
class PaymentCaptureCompleted implements RequestHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use PrefixTrait;
|
||||||
private $logger;
|
private $logger;
|
||||||
|
public function __construct(LoggerInterface $logger, string $prefix)
|
||||||
public function __construct(LoggerInterface $logger)
|
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->prefix = $prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eventTypes(): array
|
public function eventTypes(): array
|
||||||
|
@ -31,7 +32,23 @@ class PaymentCaptureCompleted implements RequestHandler
|
||||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||||
{
|
{
|
||||||
$response = ['success' => false];
|
$response = ['success' => false];
|
||||||
$orderId = (int) $request['resource']['custom_id'];
|
$orderId = isset($request['resource']['custom_id']) ? $this->sanitizeCustomId($request['resource']['custom_id']) : 0;
|
||||||
|
if (! $orderId) {
|
||||||
|
$message = sprintf(
|
||||||
|
// translators: %s is the PayPal webhook Id.
|
||||||
|
__('No order for webhook event %s was found.', 'woocommerce-paypal-commerce-gateway'),
|
||||||
|
isset($request['id']) ? $request['id'] : ''
|
||||||
|
);
|
||||||
|
$this->logger->log(
|
||||||
|
'warning',
|
||||||
|
$message,
|
||||||
|
[
|
||||||
|
'request' => $request,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$response['message'] = $message;
|
||||||
|
return rest_ensure_response($response);
|
||||||
|
}
|
||||||
$wcOrder = wc_get_order($orderId);
|
$wcOrder = wc_get_order($orderId);
|
||||||
|
|
||||||
if (! is_a($wcOrder, \WC_Order::class)) {
|
if (! is_a($wcOrder, \WC_Order::class)) {
|
||||||
|
|
|
@ -9,10 +9,12 @@ use Psr\Log\LoggerInterface;
|
||||||
class PaymentCaptureRefunded implements RequestHandler
|
class PaymentCaptureRefunded implements RequestHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use PrefixTrait;
|
||||||
private $logger;
|
private $logger;
|
||||||
public function __construct(LoggerInterface $logger)
|
public function __construct(LoggerInterface $logger, string $prefix)
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->prefix = $prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eventTypes(): array
|
public function eventTypes(): array
|
||||||
|
@ -29,7 +31,7 @@ class PaymentCaptureRefunded implements RequestHandler
|
||||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||||
{
|
{
|
||||||
$response = ['success' => false];
|
$response = ['success' => false];
|
||||||
$orderId = isset($request['resource']['custom_id']) ? (int) $request['resource']['custom_id'] : 0;
|
$orderId = isset($request['resource']['custom_id']) ? $this->sanitizeCustomId($request['resource']['custom_id']) : 0;
|
||||||
if (! $orderId) {
|
if (! $orderId) {
|
||||||
$message = sprintf(
|
$message = sprintf(
|
||||||
// translators: %s is the PayPal webhook Id.
|
// translators: %s is the PayPal webhook Id.
|
||||||
|
|
|
@ -9,10 +9,12 @@ use Psr\Log\LoggerInterface;
|
||||||
class PaymentCaptureReversed implements RequestHandler
|
class PaymentCaptureReversed implements RequestHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use PrefixTrait;
|
||||||
private $logger;
|
private $logger;
|
||||||
public function __construct(LoggerInterface $logger)
|
public function __construct(LoggerInterface $logger, string $prefix)
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->prefix = $prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eventTypes(): array
|
public function eventTypes(): array
|
||||||
|
@ -33,7 +35,7 @@ class PaymentCaptureReversed implements RequestHandler
|
||||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||||
{
|
{
|
||||||
$response = ['success' => false];
|
$response = ['success' => false];
|
||||||
$orderId = isset($request['resource']['custom_id']) ? (int) $request['resource']['custom_id'] : 0;
|
$orderId = isset($request['resource']['custom_id']) ? $this->sanitizeCustomId($request['resource']['custom_id']) : 0;
|
||||||
if (! $orderId) {
|
if (! $orderId) {
|
||||||
$message = sprintf(
|
$message = sprintf(
|
||||||
// translators: %s is the PayPal webhook Id.
|
// translators: %s is the PayPal webhook Id.
|
||||||
|
|
16
modules.local/ppcp-webhooks/src/Handler/PrefixTrait.php
Normal file
16
modules.local/ppcp-webhooks/src/Handler/PrefixTrait.php
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||||
|
|
||||||
|
|
||||||
|
trait PrefixTrait
|
||||||
|
{
|
||||||
|
|
||||||
|
private $prefix = '';
|
||||||
|
|
||||||
|
private function sanitizeCustomId(string $customId) : int {
|
||||||
|
$orderId = str_replace($this->prefix, '', $customId);
|
||||||
|
return (int) $orderId;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue