PaymentFactory.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Domain\Factory\Payment; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Payment\PaymentGateway; |
| 12 | use AmeliaBooking\Domain\Entity\Payment\Payment; |
| 13 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 14 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 15 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 16 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 17 | use AmeliaBooking\Domain\ValueObjects\Number\Float\Price; |
| 18 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 19 | use AmeliaBooking\Domain\ValueObjects\String\PaymentStatus; |
| 20 | use AmeliaBooking\Domain\ValueObjects\String\Name; |
| 21 | use AmeliaBooking\Domain\ValueObjects\String\PaymentData; |
| 22 | use AmeliaBooking\Domain\ValueObjects\String\Url; |
| 23 | use AmeliaBooking\Infrastructure\WP\HelperService\HelperService; |
| 24 | use AmeliaBooking\Infrastructure\WP\Integrations\WooCommerce\WooCommerceService; |
| 25 | |
| 26 | /** |
| 27 | * Class PaymentFactory |
| 28 | * |
| 29 | * @package AmeliaBooking\Domain\Factory\Payment |
| 30 | */ |
| 31 | class PaymentFactory |
| 32 | { |
| 33 | /** |
| 34 | * @param $data |
| 35 | * |
| 36 | * @return Payment |
| 37 | * @throws InvalidArgumentException |
| 38 | */ |
| 39 | public static function create($data) |
| 40 | { |
| 41 | if (isset($data['data']) && !is_string($data['data'])) { |
| 42 | $data['data'] = json_encode($data['data'], true); |
| 43 | } |
| 44 | |
| 45 | $payment = new Payment( |
| 46 | new Price($data['amount']), |
| 47 | new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['dateTime'])), |
| 48 | new PaymentStatus($data['status']), |
| 49 | new PaymentGateway(new Name($data['gateway'])), |
| 50 | new PaymentData(isset($data['data']) ? $data['data'] : '') |
| 51 | ); |
| 52 | |
| 53 | if (!empty($data['customerBookingId'])) { |
| 54 | $payment->setCustomerBookingId(new Id($data['customerBookingId'])); |
| 55 | } |
| 56 | |
| 57 | if (isset($data['id'])) { |
| 58 | $payment->setId(new Id($data['id'])); |
| 59 | } |
| 60 | |
| 61 | if (!empty($data['gatewayTitle'])) { |
| 62 | $payment->setGatewayTitle(new Name($data['gatewayTitle'])); |
| 63 | } |
| 64 | |
| 65 | if (!empty($data['packageCustomerId'])) { |
| 66 | $payment->setPackageCustomerId(new Id($data['packageCustomerId'])); |
| 67 | } |
| 68 | |
| 69 | if (!empty($data['parentId'])) { |
| 70 | $payment->setParentId(new Id($data['parentId'])); |
| 71 | } |
| 72 | |
| 73 | if (!empty($data['invoiceNumber'])) { |
| 74 | $payment->setInvoiceNumber(new Id($data['invoiceNumber'])); |
| 75 | } |
| 76 | |
| 77 | if (!empty($data['entity'])) { |
| 78 | $payment->setEntity(new Name($data['entity'])); |
| 79 | } |
| 80 | |
| 81 | if (!empty($data['actionsCompleted'])) { |
| 82 | $payment->setActionsCompleted(new BooleanValueObject($data['actionsCompleted'])); |
| 83 | } |
| 84 | |
| 85 | if (!empty($data['triggeredActions'])) { |
| 86 | $payment->setTriggeredActions(new BooleanValueObject($data['triggeredActions'])); |
| 87 | } |
| 88 | |
| 89 | if (!empty($data['created'])) { |
| 90 | $payment->setCreated(new DateTimeValue(DateTimeService::getCustomDateTimeObjectFromUtc($data['created']))); |
| 91 | } |
| 92 | |
| 93 | if (!empty($data['wcOrderId']) && WooCommerceService::isEnabled()) { |
| 94 | $payment->setWcOrderId(new Id($data['wcOrderId'])); |
| 95 | |
| 96 | if (!empty($data['wcOrderItemId'])) { |
| 97 | $payment->setWcOrderItemId(new Id($data['wcOrderItemId'])); |
| 98 | } |
| 99 | |
| 100 | if ($wcOrderUrl = HelperService::getWooCommerceOrderUrl($data['wcOrderId'])) { |
| 101 | $payment->setWcOrderUrl(new Url($wcOrderUrl)); |
| 102 | } |
| 103 | |
| 104 | if ($wcOrderItemValues = HelperService::getWooCommerceOrderItemAmountValues($data['wcOrderId'])) { |
| 105 | $key = !empty($data['wcOrderItemId']) && !empty($wcOrderItemValues[$data['wcOrderItemId']]) ? |
| 106 | $data['wcOrderItemId'] : array_keys($wcOrderItemValues)[0]; |
| 107 | |
| 108 | if (!empty($wcOrderItemValues[$key]['coupon'])) { |
| 109 | $payment->setWcItemCouponValue( |
| 110 | new Price($wcOrderItemValues[$key]['coupon'] < 0 ? 0 : $wcOrderItemValues[$key]['coupon']) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | if (!empty($wcOrderItemValues[$key]['tax'])) { |
| 115 | $payment->setWcItemTaxValue(new Price($wcOrderItemValues[$key]['tax'])); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!empty($data['transactionId'])) { |
| 121 | $payment->setTransactionId($data['transactionId']); |
| 122 | } |
| 123 | |
| 124 | if (!empty($data['transfers'])) { |
| 125 | $payment->setTransfers(new Json($data['transfers'])); |
| 126 | } |
| 127 | |
| 128 | return $payment; |
| 129 | } |
| 130 | } |
| 131 |