|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Xendit\M2Invoice\Model\Payment; |
| 4 | + |
| 5 | +use Magento\Framework\Api\ExtensionAttributesFactory; |
| 6 | +use Magento\Framework\Api\AttributeValueFactory; |
| 7 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 8 | +use Magento\Framework\Model\Context; |
| 9 | +use Magento\Framework\Phrase; |
| 10 | +use Magento\Framework\Registry; |
| 11 | +use Magento\Payment\Helper\Data; |
| 12 | +use Magento\Payment\Model\Method\Logger; |
| 13 | +use Xendit\M2Invoice\Helper\ApiRequest; |
| 14 | +use Xendit\M2Invoice\Helper\LogDNA; |
| 15 | +use Xendit\M2Invoice\Enum\LogDNALevel; |
| 16 | + |
| 17 | +class CCInstallment extends CCHosted |
| 18 | +{ |
| 19 | + const PLATFORM_NAME = 'MAGENTO2'; |
| 20 | + const PAYMENT_TYPE = 'CREDIT_CARD'; |
| 21 | + /** |
| 22 | + * Payment code |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $_code = 'cc_installment'; |
| 27 | + protected $_minAmount = 10000; |
| 28 | + protected $_maxAmount = 10000000; |
| 29 | + protected $_canRefund = true; |
| 30 | + protected $methodCode = 'CC_INSTALLMENT'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Override |
| 34 | + */ |
| 35 | + public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount) |
| 36 | + { |
| 37 | + $payment->setIsTransactionPending(true); |
| 38 | + |
| 39 | + $order = $payment->getOrder(); |
| 40 | + $quoteId = $order->getQuoteId(); |
| 41 | + $quote = $this->quoteRepository->get($quoteId); |
| 42 | + |
| 43 | + if ($quote->getIsMultiShipping()) { |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + $orderId = $order->getRealOrderId(); |
| 49 | + |
| 50 | + $billingAddress = $order->getBillingAddress(); |
| 51 | + $shippingAddress = $order->getShippingAddress(); |
| 52 | + |
| 53 | + $firstName = $billingAddress->getFirstname() ?: $shippingAddress->getFirstname(); |
| 54 | + $country = $billingAddress->getCountryId() ?: $shippingAddress->getCountryId(); |
| 55 | + $billingDetails = array( |
| 56 | + 'given_names' => ($firstName ?: 'N/A'), |
| 57 | + 'surname' => ($billingAddress->getLastname() ?: null), |
| 58 | + 'email' => ($billingAddress->getEmail() ?: null), |
| 59 | + 'phone_number' => ($billingAddress->getTelephone() ?: null), |
| 60 | + 'address' => array( |
| 61 | + 'country' => ($country ?: 'ID'), |
| 62 | + 'street_line_1' => ($billingAddress->getStreetLine(1) ?: null), |
| 63 | + 'street_line_2' => ($billingAddress->getStreetLine(2) ?: null), |
| 64 | + 'city' => ($billingAddress->getCity() ?: null), |
| 65 | + 'state' => ($billingAddress->getRegion() ?: null), |
| 66 | + 'postal_code' => ($billingAddress->getPostcode() ?: null) |
| 67 | + ) |
| 68 | + ); |
| 69 | + |
| 70 | + $rawAmount = ceil($order->getSubtotal() + $order->getShippingAmount()); |
| 71 | + |
| 72 | + $args = array( |
| 73 | + 'order_number' => $orderId, |
| 74 | + 'amount' => $amount, |
| 75 | + 'payment_type' => self::PAYMENT_TYPE, |
| 76 | + 'store_name' => $this->storeManager->getStore()->getName(), |
| 77 | + 'platform_name' => self::PLATFORM_NAME, |
| 78 | + 'is_installment' => "true", |
| 79 | + 'billing_details' => json_encode($billingDetails, JSON_FORCE_OBJECT) |
| 80 | + ); |
| 81 | + |
| 82 | + $promo = $this->calculatePromo($order, $rawAmount); |
| 83 | + |
| 84 | + if (!empty($promo)) { |
| 85 | + $args['promotions'] = json_encode($promo); |
| 86 | + $args['amount'] = $rawAmount; |
| 87 | + |
| 88 | + $invalidDiscountAmount = $order->getBaseDiscountAmount(); |
| 89 | + $order->setBaseDiscountAmount(0); |
| 90 | + $order->setBaseGrandTotal($order->getBaseGrandTotal() - $invalidDiscountAmount); |
| 91 | + |
| 92 | + $invalidDiscountAmount = $order->getDiscountAmount(); |
| 93 | + $order->setDiscountAmount(0); |
| 94 | + $order->setGrandTotal($order->getGrandTotal() - $invalidDiscountAmount); |
| 95 | + |
| 96 | + $order->setBaseTotalDue($order->getBaseGrandTotal()); |
| 97 | + $order->setTotalDue($order->getGrandTotal()); |
| 98 | + |
| 99 | + $payment->setBaseAmountOrdered($order->getBaseGrandTotal()); |
| 100 | + $payment->setAmountOrdered($order->getGrandTotal()); |
| 101 | + |
| 102 | + $payment->setAmountAuthorized($order->getGrandTotal()); |
| 103 | + $payment->setBaseAmountAuthorized($order->getBaseGrandTotal()); |
| 104 | + } |
| 105 | + |
| 106 | + $hostedPayment = $this->requestHostedPayment($args); |
| 107 | + |
| 108 | + if (isset($hostedPayment['error_code'])) { |
| 109 | + $message = isset($hostedPayment['message']) ? $hostedPayment['message'] : $hostedPayment['error_code']; |
| 110 | + $this->processFailedPayment($payment, $message); |
| 111 | + |
| 112 | + throw new \Magento\Framework\Exception\LocalizedException( |
| 113 | + new Phrase($message) |
| 114 | + ); |
| 115 | + } elseif (isset($hostedPayment['id'])) { |
| 116 | + $hostedPaymentId = $hostedPayment['id']; |
| 117 | + $hostedPaymentToken = $hostedPayment['hp_token']; |
| 118 | + |
| 119 | + $payment->setAdditionalInformation('xendit_hosted_payment_id', $hostedPaymentId); |
| 120 | + $payment->setAdditionalInformation('xendit_hosted_payment_token', $hostedPaymentToken); |
| 121 | + } else { |
| 122 | + $message = 'Error connecting to Xendit. Check your API key'; |
| 123 | + $this->processFailedPayment($payment, $message); |
| 124 | + |
| 125 | + throw new \Magento\Framework\Exception\LocalizedException( |
| 126 | + new Phrase($message) |
| 127 | + ); |
| 128 | + } |
| 129 | + } catch (\Exception $e) { |
| 130 | + $errorMsg = $e->getMessage(); |
| 131 | + throw new \Magento\Framework\Exception\LocalizedException( |
| 132 | + new Phrase($errorMsg) |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + return $this; |
| 137 | + } |
| 138 | +} |
0 commit comments