Skip to content

Commit

Permalink
Merge pull request #40 from xendit/TPI-2541/admin-settings
Browse files Browse the repository at this point in the history
Add new CC Installment payment method & admin setting
  • Loading branch information
hakiemaul authored Sep 8, 2020
2 parents b2eafce + 4884bac commit 5077843
Show file tree
Hide file tree
Showing 25 changed files with 346 additions and 77 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 2.1.0 (2020-09-04)

Features:

- Add Credit Card Installment payment method

## 2.0.1 (2020-06-11)

Bugfix:
Expand Down
31 changes: 28 additions & 3 deletions Xendit/M2Invoice/Controller/Checkout/CCMultishipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function execute()
return $this->processFailedPayment($orderIds, $charge['failure_reason']);
}
}
else if ($method === 'cchosted') {
$requestData = [
else if ($method === 'cchosted' || $method === 'cc_installment') {
$requestData = array(
'order_number' => $rawOrderIds,
'amount' => $transactionAmount,
'payment_type' => 'CREDIT_CARD',
Expand All @@ -86,7 +86,32 @@ public function execute()
'success_redirect_url' => $this->getDataHelper()->getSuccessUrl(true),
'failure_redirect_url' => $this->getDataHelper()->getFailureUrl($rawOrderIds, true),
'platform_callback_url' => $this->_url->getUrl('xendit/checkout/cccallback') . '?order_ids=' . $rawOrderIds
];
);

if ($method === 'cc_installment') {
$billingAddress = $orders[0]->getBillingAddress();
$shippingAddress = $orders[0]->getShippingAddress();

$firstName = $billingAddress->getFirstname() ?: $shippingAddress->getFirstname();
$country = $billingAddress->getCountryId() ?: $shippingAddress->getCountryId();
$billingDetails = array(
'given_names' => ($firstName ?: 'N/A'),
'surname' => ($billingAddress->getLastname() ?: null),
'email' => ($billingAddress->getEmail() ?: null),
'phone_number' => ($billingAddress->getTelephone() ?: null),
'address' => array(
'country' => ($country ?: 'ID'),
'street_line_1' => ($billingAddress->getStreetLine(1) ?: null),
'street_line_2' => ($billingAddress->getStreetLine(2) ?: null),
'city' => ($billingAddress->getCity() ?: null),
'state' => ($billingAddress->getRegion() ?: null),
'postal_code' => ($billingAddress->getPostcode() ?: null)
)
);

$requestData['is_installment'] = "true";
$requestData['billing_details'] = json_encode($billingDetails, JSON_FORCE_OBJECT);
}

$hostedPayment = $this->requestHostedPayment($requestData);

Expand Down
2 changes: 1 addition & 1 deletion Xendit/M2Invoice/Controller/Payment/OverviewPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function execute()
$params = implode("-", $ids);
$baseUrl = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl();

if ($xenditPaymentMethod === 'cc' || $xenditPaymentMethod === 'cchosted') {
if ($xenditPaymentMethod === 'cc' || $xenditPaymentMethod === 'cchosted' || $xenditPaymentMethod === 'cc_installment') {
$redirect = $baseUrl . '/xendit/checkout/ccmultishipping?order_ids=' . $params . '&preferred_method=' . $xenditPaymentMethod;
} else {
$redirect = $baseUrl . '/xendit/checkout/invoicemultishipping?order_ids=' . $params.'&preferred_method='.$xenditPaymentMethod.'&billing_email='.$billingEmail;
Expand Down
2 changes: 1 addition & 1 deletion Xendit/M2Invoice/Cron/CancelOrderCC.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function execute()
if (strtotime($creationTime) < strtotime('-1 day')) {
$order->setState(Order::STATE_CANCELED)
->setStatus(Order::STATE_CANCELED)
->addStatusHistoryComment("Xendit payment cancelled due to stuck on payment review for 30 days");
->addStatusHistoryComment("Xendit payment cancelled due to stuck on payment review for 24 hours");

$bulkCancelData[] = array(
'id' => $hosted3DSId,
Expand Down
3 changes: 2 additions & 1 deletion Xendit/M2Invoice/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ public function mapSalesRuleType($type)

public function xenditPaymentMethod( $payment ){

//methode name => frontend routing
//method name => frontend routing
$listPayment = [
"cc" => "cc",
"cchosted" => "cchosted",
"cc_installment" => "cc_installment",
"bcava" => "bca",
"bniva" => "bni",
"briva" => "bri",
Expand Down
1 change: 1 addition & 0 deletions Xendit/M2Invoice/Model/Adminhtml/Source/ChosenMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function toOptionArray($isMultiselect = false)
['value' => 'mandiriva', 'label' => __('Bank Transfer Mandiri')],
['value' => 'permatava', 'label' => __('Bank Transfer Permata')],
['value' => 'cc', 'label' => __('Credit Card')],
['value' => 'cc_installment', 'label' => __('Credit Card Installment')],
['value' => 'ovo', 'label' => __('OVO')],
];

Expand Down
2 changes: 1 addition & 1 deletion Xendit/M2Invoice/Model/Payment/AbstractInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)

$cardPaymentType = $this->dataHelper->getCardPaymentType();

if ($cardPaymentType === 'popup' && $this->methodCode === 'CCHOSTED') {
if (($cardPaymentType === 'popup' && $this->methodCode === 'CCHOSTED') || $this->methodCode === 'CC_INSTALLMENT') {
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions Xendit/M2Invoice/Model/Payment/CCHosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private function requestRefund($chargeId, $requestData)
return $refund;
}

private function requestHostedPayment($requestData)
public function requestHostedPayment($requestData)
{
$hostedPaymentUrl = $this->dataHelper->getCheckoutUrl() . "/payment/xendit/hosted-payments";
$hostedPaymentMethod = \Zend\Http\Request::METHOD_POST;
Expand All @@ -170,7 +170,7 @@ private function requestHostedPayment($requestData)
return $hostedPayment;
}

private function processFailedPayment($payment, $message)
public function processFailedPayment($payment, $message)
{
$payment->setAdditionalInformation('xendit_failure_reason', $message);
}
Expand Down Expand Up @@ -198,7 +198,7 @@ private function handleRefundResult($payment, $refund, $canRefundMore)
);
}

private function calculatePromo($order, $rawAmount)
public function calculatePromo($order, $rawAmount)
{
$promo = [];
$ruleIds = $order->getAppliedRuleIds();
Expand Down
138 changes: 138 additions & 0 deletions Xendit/M2Invoice/Model/Payment/CCInstallment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Xendit\M2Invoice\Model\Payment;

use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Model\Context;
use Magento\Framework\Phrase;
use Magento\Framework\Registry;
use Magento\Payment\Helper\Data;
use Magento\Payment\Model\Method\Logger;
use Xendit\M2Invoice\Helper\ApiRequest;
use Xendit\M2Invoice\Helper\LogDNA;
use Xendit\M2Invoice\Enum\LogDNALevel;

class CCInstallment extends CCHosted
{
const PLATFORM_NAME = 'MAGENTO2';
const PAYMENT_TYPE = 'CREDIT_CARD';
/**
* Payment code
*
* @var string
*/
protected $_code = 'cc_installment';
protected $_minAmount = 10000;
protected $_maxAmount = 10000000;
protected $_canRefund = true;
protected $methodCode = 'CC_INSTALLMENT';

/**
* Override
*/
public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
$payment->setIsTransactionPending(true);

$order = $payment->getOrder();
$quoteId = $order->getQuoteId();
$quote = $this->quoteRepository->get($quoteId);

if ($quote->getIsMultiShipping()) {
return $this;
}

try {
$orderId = $order->getRealOrderId();

$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();

$firstName = $billingAddress->getFirstname() ?: $shippingAddress->getFirstname();
$country = $billingAddress->getCountryId() ?: $shippingAddress->getCountryId();
$billingDetails = array(
'given_names' => ($firstName ?: 'N/A'),
'surname' => ($billingAddress->getLastname() ?: null),
'email' => ($billingAddress->getEmail() ?: null),
'phone_number' => ($billingAddress->getTelephone() ?: null),
'address' => array(
'country' => ($country ?: 'ID'),
'street_line_1' => ($billingAddress->getStreetLine(1) ?: null),
'street_line_2' => ($billingAddress->getStreetLine(2) ?: null),
'city' => ($billingAddress->getCity() ?: null),
'state' => ($billingAddress->getRegion() ?: null),
'postal_code' => ($billingAddress->getPostcode() ?: null)
)
);

$rawAmount = ceil($order->getSubtotal() + $order->getShippingAmount());

$args = array(
'order_number' => $orderId,
'amount' => $amount,
'payment_type' => self::PAYMENT_TYPE,
'store_name' => $this->storeManager->getStore()->getName(),
'platform_name' => self::PLATFORM_NAME,
'is_installment' => "true",
'billing_details' => json_encode($billingDetails, JSON_FORCE_OBJECT)
);

$promo = $this->calculatePromo($order, $rawAmount);

if (!empty($promo)) {
$args['promotions'] = json_encode($promo);
$args['amount'] = $rawAmount;

$invalidDiscountAmount = $order->getBaseDiscountAmount();
$order->setBaseDiscountAmount(0);
$order->setBaseGrandTotal($order->getBaseGrandTotal() - $invalidDiscountAmount);

$invalidDiscountAmount = $order->getDiscountAmount();
$order->setDiscountAmount(0);
$order->setGrandTotal($order->getGrandTotal() - $invalidDiscountAmount);

$order->setBaseTotalDue($order->getBaseGrandTotal());
$order->setTotalDue($order->getGrandTotal());

$payment->setBaseAmountOrdered($order->getBaseGrandTotal());
$payment->setAmountOrdered($order->getGrandTotal());

$payment->setAmountAuthorized($order->getGrandTotal());
$payment->setBaseAmountAuthorized($order->getBaseGrandTotal());
}

$hostedPayment = $this->requestHostedPayment($args);

if (isset($hostedPayment['error_code'])) {
$message = isset($hostedPayment['message']) ? $hostedPayment['message'] : $hostedPayment['error_code'];
$this->processFailedPayment($payment, $message);

throw new \Magento\Framework\Exception\LocalizedException(
new Phrase($message)
);
} elseif (isset($hostedPayment['id'])) {
$hostedPaymentId = $hostedPayment['id'];
$hostedPaymentToken = $hostedPayment['hp_token'];

$payment->setAdditionalInformation('xendit_hosted_payment_id', $hostedPaymentId);
$payment->setAdditionalInformation('xendit_hosted_payment_token', $hostedPaymentToken);
} else {
$message = 'Error connecting to Xendit. Check your API key';
$this->processFailedPayment($payment, $message);

throw new \Magento\Framework\Exception\LocalizedException(
new Phrase($message)
);
}
} catch (\Exception $e) {
$errorMsg = $e->getMessage();
throw new \Magento\Framework\Exception\LocalizedException(
new Phrase($errorMsg)
);
}

return $this;
}
}
7 changes: 5 additions & 2 deletions Xendit/M2Invoice/Model/Ui/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ public function __construct(

public function getConfig()
{
$defaultCCInstallmentDesc = "Bayar pesanan dengan cicilan kartu kredit anda melalui Xendit.\r\nBank yang tersedia: BCA, BRI";

$config = [
'payment' => [
Config::CODE => [
'xendit_env' => $this->m2Invoice->getConfigData('xendit_env'),
'test_prefix' => $this->m2Invoice->getConfigData('checkout_test_prefix'),
'test_content' => $this->m2Invoice->getConfigData('checkout_test_content'),
'public_api_key' => $this->m2Invoice->getPublicApiKey(),
'availableTypes' => ['cc' => $this->ccConfig->getCcAvailableTypes()],
'available_types' => ['cc' => $this->ccConfig->getCcAvailableTypes()],
'months' => ['cc' => $this->ccConfig->getCcMonths()],
'years' => ['cc' => $this->ccConfig->getCcYears()],
'hasVerification' => $this->ccConfig->hasVerification()
'has_verification' => $this->ccConfig->hasVerification(),
'card_installment_description' => ($this->m2Invoice->getConfigData('card_installment_description') ?: $defaultCCInstallmentDesc)
]
]
];
Expand Down
2 changes: 1 addition & 1 deletion Xendit/M2Invoice/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "xendit/m2invoice",
"description": "Xendit Payment Gateway Module",
"type": "magento2-module",
"version": "2.0.1",
"version": "2.1.0",
"license": [
"GPL-3.0"
],
Expand Down
21 changes: 14 additions & 7 deletions Xendit/M2Invoice/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
dynamicShow();
shouldEnableSpecificMethod();
specificSelection.prop('size', 6);
jQuery("textarea[id*='m2invoice_card_installment_description']").attr("placeholder", "Bayar pesanan dengan cicilan kartu kredit anda melalui Xendit.\nBank yang tersedia: BCA, BRI");
jQuery("textarea[id*='m2invoice_card_installment_description']").val("Bayar pesanan dengan cicilan kartu kredit anda melalui Xendit.\nBank yang tersedia: BCA, BRI");
});
</script>
]]></comment>
Expand All @@ -113,38 +115,38 @@
<label>Payment Mode</label>
<source_model>Xendit\M2Invoice\Model\Adminhtml\Source\EnvRadioBtn</source_model>
<comment>
Choose live to have live transaction
Choose live to perform real transaction
</comment>
<attribute type="shared">1</attribute>
</field>
<field id="test_public_key" translate="label" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Test - Public Key</label>
<comment>
Public API key that provided by Xendit Dashboard
Public API key provided by Xendit Dashboard
</comment>
<attribute type="shared">1</attribute>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="test_private_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Test - Private Key</label>
<comment>
Secret API key that provided by Xendit Dashboard
Secret API key provided by Xendit Dashboard
</comment>
<attribute type="shared">1</attribute>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="public_key" translate="label" type="obscure" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Public Key</label>
<comment>
Public API key that provided by Xendit Dashboard
Public API key provided by Xendit Dashboard
</comment>
<attribute type="shared">1</attribute>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="private_key" translate="label" type="obscure" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Private Key</label>
<comment>
Secret API key that provided by Xendit Dashboard
Secret API key provided by Xendit Dashboard
</comment>
<attribute type="shared">1</attribute>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
Expand All @@ -156,7 +158,7 @@
</comment>
<source_model>Xendit\M2Invoice\Model\Adminhtml\Source\AllowedMethod</source_model>
</field>
<field id="chosen_methods" translate="label" type="multiselect" size="6" sortOrder="66" showInDefault="1" showInWebsite="1" showInStore="0">
<field id="chosen_methods" translate="label" type="multiselect" sortOrder="66" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Specific Payment Method</label>
<comment>
Use all payment method that is provided by Xendit
Expand All @@ -167,11 +169,16 @@
<field id="card_payment_type" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Card Payment Type</label>
<comment>
Xendit provide card form to increase your order payment
Choose popup if you don't want to hold sensitive card data on your site
</comment>
<source_model>Xendit\M2Invoice\Model\Adminhtml\Source\CardPaymentType</source_model>
<attribute type="shared">1</attribute>
</field>
<field id="card_installment_description" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Credit Card Installment Description</label>
<comment>Customize your installment payment description on checkout page</comment>
</field>

<group id="card_promo_bca" translate="label" showInDefault="1" showInWebsite="1" sortOrder="100">
<label>Credit Card Promotion - BCA</label>
<field id="card_promo_bca_active" translate="label comment" sortOrder="101" type="select" showInDefault="1" showInWebsite="1" showInStore="0">
Expand Down
Loading

0 comments on commit 5077843

Please sign in to comment.