BitBagCommerce /
SyliusQuadPayPlugin
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file has been created by developers from BitBag. |
||
| 5 | * Feel free to contact us once you face any issues or want to start |
||
| 6 | * another great project. |
||
| 7 | * You can find more information about us on https://bitbag.shop and write us |
||
| 8 | * an email on [email protected]. |
||
| 9 | */ |
||
| 10 | |||
| 11 | declare(strict_types=1); |
||
| 12 | |||
| 13 | namespace BitBag\SyliusQuadPayPlugin\Action; |
||
| 14 | |||
| 15 | use Payum\Core\Action\ActionInterface; |
||
| 16 | use Payum\Core\Exception\RequestNotSupportedException; |
||
| 17 | use Payum\Core\GatewayAwareInterface; |
||
| 18 | use Payum\Core\GatewayAwareTrait; |
||
| 19 | use Payum\Core\Request\Convert; |
||
| 20 | use Payum\Core\Request\GetCurrency; |
||
| 21 | use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface; |
||
| 22 | use Sylius\Component\Core\Model\OrderInterface; |
||
| 23 | use Sylius\Component\Core\Model\OrderItemInterface; |
||
| 24 | use Sylius\Component\Core\Model\PaymentInterface; |
||
| 25 | |||
| 26 | final class ConvertPaymentAction implements ActionInterface, GatewayAwareInterface |
||
| 27 | { |
||
| 28 | use GatewayAwareTrait; |
||
| 29 | |||
| 30 | /** @var PaymentDescriptionProviderInterface */ |
||
| 31 | private $paymentDescriptionProvider; |
||
| 32 | |||
| 33 | public function __construct(PaymentDescriptionProviderInterface $paymentDescriptionProvider) |
||
| 34 | { |
||
| 35 | $this->paymentDescriptionProvider = $paymentDescriptionProvider; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | * |
||
| 41 | * @param Convert $request |
||
| 42 | */ |
||
| 43 | public function execute($request): void |
||
| 44 | { |
||
| 45 | RequestNotSupportedException::assertSupports($this, $request); |
||
| 46 | |||
| 47 | /** @var PaymentInterface $payment */ |
||
| 48 | $payment = $request->getSource(); |
||
| 49 | |||
| 50 | /** @var OrderInterface $order */ |
||
| 51 | $order = $payment->getOrder(); |
||
| 52 | |||
| 53 | $this->gateway->execute($currency = new GetCurrency($payment->getCurrencyCode())); |
||
| 54 | |||
| 55 | $details = [ |
||
| 56 | 'amount' => $this->amountFormat($payment->getAmount(), $currency), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 57 | 'consumer' => $this->getConsumerData($order), |
||
| 58 | 'billing' => $this->getBillingData($order), |
||
| 59 | 'shipping' => $this->getShippingData($order), |
||
| 60 | 'description' => $this->paymentDescriptionProvider->getPaymentDescription($payment), |
||
| 61 | 'items' => $this->getItemsData($order, $currency), |
||
| 62 | 'merchantReference' => $order->getNumber(), |
||
| 63 | 'taxAmount' => $this->amountFormat($order->getTaxTotal(), $currency), |
||
| 64 | 'shippingAmount' => $this->amountFormat($order->getShippingTotal(), $currency), |
||
| 65 | ]; |
||
| 66 | |||
| 67 | $request->setResult($details); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function supports($request): bool |
||
| 71 | { |
||
| 72 | return |
||
| 73 | $request instanceof Convert && |
||
| 74 | $request->getSource() instanceof PaymentInterface && |
||
| 75 | $request->getTo() === 'array' |
||
| 76 | ; |
||
| 77 | } |
||
| 78 | |||
| 79 | private function getConsumerData(OrderInterface $order): array |
||
| 80 | { |
||
| 81 | $customer = $order->getCustomer(); |
||
| 82 | $shippingAddress = $order->getShippingAddress(); |
||
| 83 | |||
| 84 | return [ |
||
| 85 | 'phoneNumber' => $shippingAddress->getPhoneNumber(), |
||
| 86 | 'givenNames' => $shippingAddress->getFirstName(), |
||
| 87 | 'surname' => $shippingAddress->getLastName(), |
||
| 88 | 'email' => $customer->getEmail(), |
||
| 89 | ]; |
||
| 90 | } |
||
| 91 | |||
| 92 | private function getBillingData(OrderInterface $order): array |
||
| 93 | { |
||
| 94 | $billingAddress = $order->getBillingAddress(); |
||
| 95 | |||
| 96 | return [ |
||
| 97 | 'addressLine1' => $billingAddress->getStreet(), |
||
| 98 | 'addressLine2' => '', |
||
| 99 | 'city' => $billingAddress->getCity(), |
||
| 100 | 'postcode' => $billingAddress->getPostcode() ?? '', |
||
| 101 | 'state' => $billingAddress->getProvinceCode() ?? '', |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | |||
| 105 | private function getShippingData(OrderInterface $order): array |
||
| 106 | { |
||
| 107 | $shippingAddress = $order->getShippingAddress(); |
||
| 108 | |||
| 109 | return [ |
||
| 110 | 'addressLine1' => $shippingAddress->getStreet(), |
||
| 111 | 'addressLine2' => '', |
||
| 112 | 'city' => $shippingAddress->getCity(), |
||
| 113 | 'postcode' => $shippingAddress->getPostcode() ?? '', |
||
| 114 | 'state' => $shippingAddress->getProvinceCode() ?? '', |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | |||
| 118 | private function getItemsData(OrderInterface $order, $currency): array |
||
| 119 | { |
||
| 120 | $items = []; |
||
| 121 | |||
| 122 | /** @var OrderItemInterface $orderItem */ |
||
| 123 | foreach ($order->getItems() as $orderItem) { |
||
| 124 | $product = $orderItem->getProduct(); |
||
| 125 | |||
| 126 | $items[] = [ |
||
| 127 | 'description' => $product->getShortDescription(), |
||
| 128 | 'name' => $product->getName(), |
||
| 129 | 'sku' => $product->getCode(), |
||
| 130 | 'quantity' => $orderItem->getQuantity(), |
||
| 131 | 'price' => $this->amountFormat($orderItem->getUnitPrice(), $currency), |
||
| 132 | ]; |
||
| 133 | } |
||
| 134 | |||
| 135 | return $items; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function amountFormat(int $amount, $currency): string |
||
| 139 | { |
||
| 140 | $divisor = 10 ** $currency->exp; |
||
| 141 | |||
| 142 | return number_format(abs($amount / $divisor), 2, '.', ''); |
||
| 143 | } |
||
| 144 | } |
||
| 145 |