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