Skip to content

Commit

Permalink
Merge pull request Sylius#9437 from JakobTolkemit/payment_authorized_…
Browse files Browse the repository at this point in the history
…state_transition

[Payment] Support for authorized state
  • Loading branch information
pamil committed Aug 1, 2018
2 parents c991b2c + 9467893 commit 4428b33
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 7 deletions.
@@ -0,0 +1,4 @@
<span class="ui yellow{% if attached is defined and attached == true %} top attached{% endif %} label">
<i class="check icon"></i>
{{ value|trans }}
</span>
@@ -0,0 +1,4 @@
<span class="ui yellow{% if attached is defined and attached == true %} top attached{% endif %} label">
<i class="check icon"></i>
{{ value|trans }}
</span>
Expand Up @@ -7,6 +7,8 @@ winzou_state_machine:
states:
cart: ~
awaiting_payment: ~
partially_authorized: ~
authorized: ~
partially_paid: ~
cancelled: ~
paid: ~
Expand All @@ -16,14 +18,20 @@ winzou_state_machine:
request_payment:
from: [cart]
to: awaiting_payment
partially_authorize:
from: [awaiting_payment, partially_authorized]
to: partially_authorized
authorize:
from: [awaiting_payment, partially_authorized]
to: authorized
partially_pay:
from: [awaiting_payment, partially_paid]
from: [awaiting_payment, partially_paid, partially_authorized]
to: partially_paid
cancel:
from: [awaiting_payment]
from: [awaiting_payment, authorized, partially_authorized]
to: cancelled
pay:
from: [awaiting_payment, partially_paid]
from: [awaiting_payment, partially_paid, authorized]
to: paid
partially_refund:
from: [paid, partially_paid, partially_refunded]
Expand Down
Expand Up @@ -8,6 +8,7 @@ winzou_state_machine:
cart: ~
new: ~
processing: ~
authorized: ~
completed: ~
failed: ~
cancelled: ~
Expand All @@ -19,14 +20,17 @@ winzou_state_machine:
process:
from: [new]
to: processing
complete:
authorize:
from: [new, processing]
to: authorized
complete:
from: [new, processing, authorized]
to: completed
fail:
from: [new, processing]
to: failed
cancel:
from: [new, processing]
from: [new, processing, authorized]
to: cancelled
refund:
from: [completed]
Expand All @@ -38,6 +42,6 @@ winzou_state_machine:
do: ["@sylius.order_processing.order_payment_processor.after_checkout", "process"]
args: ["object.getOrder()"]
sylius_resolve_state:
on: ["complete", "refund"]
on: ["complete", "refund", "authorize"]
do: ["@sylius.state_resolver.order_payment", "resolve"]
args: ["object.getOrder()"]
Expand Up @@ -3,6 +3,7 @@

sylius:
payment:
authorized: 'Payment has been authorized.'
cancelled: 'Payment has been cancelled.'
completed: 'Payment has been completed.'
failed: 'Payment has failed.'
Expand Down
Expand Up @@ -68,6 +68,8 @@ sylius:
assortment: 'Assortment'
attributes: 'Attributes'
author: 'Author'
authorized: 'Authorized'
authorized_payment: 'Authorized payment'
availability: 'Availability'
available_for_channels: 'Available for channels'
available_on: 'Available on'
Expand Down Expand Up @@ -582,6 +584,7 @@ sylius:
pages: 'Pages'
paid: 'Paid'
parent: 'Parent'
partially_authorized: 'Partially authorized'
partially_paid: 'Partially paid'
partially_shipped: 'Partially shipped'
password: 'Password'
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Component/Core/OrderPaymentStates.php
Expand Up @@ -17,6 +17,8 @@ final class OrderPaymentStates
{
public const STATE_CART = 'cart';
public const STATE_AWAITING_PAYMENT = 'awaiting_payment';
public const STATE_PARTIALLY_AUTHORIZED = 'partially_authorized';
public const STATE_AUTHORIZED = 'authorized';
public const STATE_PARTIALLY_PAID = 'partially_paid';
public const STATE_CANCELLED = 'cancelled';
public const STATE_PAID = 'paid';
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Component/Core/OrderPaymentTransitions.php
Expand Up @@ -18,6 +18,8 @@ final class OrderPaymentTransitions
public const GRAPH = 'sylius_order_payment';

public const TRANSITION_REQUEST_PAYMENT = 'request_payment';
public const TRANSITION_PARTIALLY_AUTHORIZE = 'partially_authorize';
public const TRANSITION_AUTHORIZE = 'authorize';
public const TRANSITION_PARTIALLY_PAY = 'partially_pay';
public const TRANSITION_CANCEL = 'cancel';
public const TRANSITION_PAY = 'pay';
Expand Down
Expand Up @@ -105,6 +105,21 @@ private function getTargetTransition(OrderInterface $order): ?string
return OrderPaymentTransitions::TRANSITION_PARTIALLY_PAY;
}

$authorizedPaymentTotal = 0;
$authorizedPayments = $this->getPaymentsWithState($order, PaymentInterface::STATE_AUTHORIZED);

foreach ($authorizedPayments as $payment) {
$authorizedPaymentTotal += $payment->getAmount();
}

if (0 < $authorizedPayments->count() && $authorizedPaymentTotal >= $order->getTotal()) {
return OrderPaymentTransitions::TRANSITION_AUTHORIZE;
}

if ($authorizedPaymentTotal < $order->getTotal() && 0 < $authorizedPaymentTotal) {
return OrderPaymentTransitions::TRANSITION_PARTIALLY_AUTHORIZE;
}

return null;
}

Expand Down
Expand Up @@ -144,7 +144,7 @@ function it_marks_an_order_as_paid_if_fully_paid_even_if_previous_payment_was_fa
$this->resolve($order);
}

function it_marks_an_order_as_partially_refunded_if_one_of_the_payment_is_completed(
function it_marks_an_order_as_partially_refunded_if_one_of_the_payment_is_refunded(
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
OrderInterface $order,
Expand Down Expand Up @@ -218,4 +218,54 @@ function it_marks_an_order_as_partially_paid_if_one_of_the_payment_is_processing

$this->resolve($order);
}

function it_marks_an_order_as_authorized_if_all_its_payments_are_authorized(
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
OrderInterface $order,
PaymentInterface $firstPayment,
PaymentInterface $secondPayment
): void {
$firstPayment->getAmount()->willReturn(6000);
$firstPayment->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);
$secondPayment->getAmount()->willReturn(4000);
$secondPayment->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$order
->getPayments()
->willReturn(new ArrayCollection([$firstPayment->getWrappedObject(), $secondPayment->getWrappedObject()]))
;
$order->getTotal()->willReturn(10000);

$stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->can(OrderPaymentTransitions::TRANSITION_AUTHORIZE)->willReturn(true);
$stateMachine->apply(OrderPaymentTransitions::TRANSITION_AUTHORIZE)->shouldBeCalled();

$this->resolve($order);
}

function it_marks_an_order_as_partially_authorized_if_one_of_the_payments_is_processing_and_one_of_the_payments_is_authorized(
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
OrderInterface $order,
PaymentInterface $firstPayment,
PaymentInterface $secondPayment
): void {
$firstPayment->getAmount()->willReturn(6000);
$firstPayment->getState()->willReturn(PaymentInterface::STATE_PROCESSING);
$secondPayment->getAmount()->willReturn(4000);
$secondPayment->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$order
->getPayments()
->willReturn(new ArrayCollection([$firstPayment->getWrappedObject(), $secondPayment->getWrappedObject()]))
;
$order->getTotal()->willReturn(10000);

$stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->can(OrderPaymentTransitions::TRANSITION_PARTIALLY_AUTHORIZE)->willReturn(true);
$stateMachine->apply(OrderPaymentTransitions::TRANSITION_PARTIALLY_AUTHORIZE)->shouldBeCalled();

$this->resolve($order);
}
}

0 comments on commit 4428b33

Please sign in to comment.