mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
refactor AuthorizedPaymentsProcessor
This commit is contained in:
parent
4f5b6950e0
commit
4b14a7f193
3 changed files with 51 additions and 44 deletions
|
@ -92,18 +92,24 @@ class WcGateway extends WcGatewayBase
|
|||
|
||||
public function captureAuthorizedPayment(\WC_Order $wcOrder): bool
|
||||
{
|
||||
$result = $this->authorizedPayments->process($wcOrder);
|
||||
$isProcessed = $this->authorizedPayments->process($wcOrder);
|
||||
$this->renderAuthorizationMessageForStatus($this->authorizedPayments->lastStatus());
|
||||
|
||||
if ($result === AuthorizedPaymentsProcessor::INACCESSIBLE) {
|
||||
$this->notice->displayMessage(AuthorizeOrderActionNotice::NO_INFO);
|
||||
return false;
|
||||
}
|
||||
if ($result === AuthorizedPaymentsProcessor::NOT_FOUND) {
|
||||
$this->notice->displayMessage(AuthorizeOrderActionNotice::NOT_FOUND);
|
||||
return false;
|
||||
if ($isProcessed) {
|
||||
$wcOrder->add_order_note(
|
||||
__(
|
||||
'Payment successfully captured.',
|
||||
'woocommerce-paypal-gateway'
|
||||
)
|
||||
);
|
||||
|
||||
$wcOrder->set_status('processing');
|
||||
$wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true');
|
||||
$wcOrder->save();
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($result === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) {
|
||||
if ($this->authorizedPayments->lastStatus() === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) {
|
||||
if ($wcOrder->get_status() === 'on-hold') {
|
||||
$wcOrder->add_order_note(
|
||||
__(
|
||||
|
@ -116,28 +122,19 @@ class WcGateway extends WcGatewayBase
|
|||
|
||||
$wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true');
|
||||
$wcOrder->save();
|
||||
$this->notice->displayMessage(AuthorizeOrderActionNotice::ALREADY_CAPTURED);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private function renderAuthorizationMessageForStatus(string $status) {
|
||||
|
||||
if ($result !== AuthorizedPaymentsProcessor::SUCCESSFUL) {
|
||||
$this->notice->displayMessage(AuthorizeOrderActionNotice::FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
$wcOrder->add_order_note(
|
||||
__(
|
||||
'Payment successfully captured.',
|
||||
'woocommerce-paypal-gateway'
|
||||
)
|
||||
);
|
||||
|
||||
$wcOrder->set_status('processing');
|
||||
$wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true');
|
||||
$wcOrder->save();
|
||||
|
||||
$this->notice->displayMessage(AuthorizeOrderActionNotice::SUCCESS);
|
||||
return true;
|
||||
$messageMapping = [
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ class AuthorizedPaymentsProcessor
|
|||
public const NOT_FOUND = 'NOT_FOUND';
|
||||
private $orderEndpoint;
|
||||
private $paymentsEndpoint;
|
||||
private $lastStatus = '';
|
||||
|
||||
public function __construct(
|
||||
OrderEndpoint $orderEndpoint,
|
||||
|
@ -31,30 +32,39 @@ class AuthorizedPaymentsProcessor
|
|||
$this->paymentsEndpoint = $paymentsEndpoint;
|
||||
}
|
||||
|
||||
public function process(\WC_Order $wcOrder): string
|
||||
public function process(\WC_Order $wcOrder): bool
|
||||
{
|
||||
try {
|
||||
$order = $this->payPalOrderFromWcOrder($wcOrder);
|
||||
} catch (Exception $exception) {
|
||||
if ($exception->getCode() === 404) {
|
||||
return self::NOT_FOUND;
|
||||
$this->lastStatus = self::NOT_FOUND;
|
||||
return false;
|
||||
}
|
||||
return self::INACCESSIBLE;
|
||||
$this->lastStatus = self::INACCESSIBLE;
|
||||
return false;
|
||||
}
|
||||
|
||||
$authorizations = $this->allAuthorizations($order);
|
||||
|
||||
if (!$this->areAuthorizationToCapture(...$authorizations)) {
|
||||
return self::ALREADY_CAPTURED;
|
||||
$this->lastStatus = self::ALREADY_CAPTURED;
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->captureAuthorizations(...$authorizations);
|
||||
} catch (Exception $exception) {
|
||||
return self::FAILED;
|
||||
$this->lastStatus = self::FAILED;
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::SUCCESSFUL;
|
||||
$this->lastStatus = self::SUCCESSFUL;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function lastStatus() : string {
|
||||
return $this->lastStatus;
|
||||
}
|
||||
|
||||
private function payPalOrderFromWcOrder(\WC_Order $wcOrder): Order
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue