Skip to content

Commit

Permalink
Merge pull request #54 from xendit/feature/subscription
Browse files Browse the repository at this point in the history
Feature/subscription
  • Loading branch information
candrasaputra authored Sep 21, 2020
2 parents 31604b1 + 0f01f6e commit b90fcb2
Show file tree
Hide file tree
Showing 39 changed files with 1,208 additions and 34 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ fabric.properties

*.zip

telepresence.log
telepresence.log

.DS_Store
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 2.2.0 (2020-09-15)

Features:

- Add Credit Card Subscription payment method

## 2.1.0 (2020-09-04)

Features:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ To activate this feature, you need to follow this additional steps:
- Done! The cron should already scheduling and running in the background.

## Supported Payment Method
Currently this plugins support collecting payment through Xendit from these payment channels:
- Credit and Debit Card
- Mandiri VA
- BNI VA
Expand All @@ -65,6 +64,9 @@ Since v1.3.0, online refund (full and partial) is supported for payment through
## Multishipping Support
Since v2.0.0, multishipping checkout is supported for all payment methods.

## Installment & Subscription
Since v2.2.0, merchant can setup installment & subscription payment methods via credit card.

## Unit Testing
To run unit test, run this command from you `MAGENTO_DIR`:

Expand Down
53 changes: 53 additions & 0 deletions Xendit/M2Invoice/Block/CustomView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace Xendit\M2Invoice\Block;

use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Xendit\M2Invoice\Helper\Data;

class CustomView extends Template
{
private $dataHelper;

public function __construct(
Context $context,
Registry $registry,
Data $dataHelper,
array $data = []
) {
$this->registry = $registry;
$this->dataHelper = $dataHelper;

parent::__construct($context, $data);
}

/**
* Retrieve current order model instance
*
* @return \Magento\Sales\Model\Order
*/
public function getOrder()
{
return $this->registry->registry('current_order');
}

public function getPaymentMethod()
{
return $this->getOrder()->getPayment()->getMethodInstance()->getCode();
}

public function getSubscriptionConfig()
{
$data = array();
$data['card_subscription_interval'] = $this->dataHelper->getSubscriptionInterval();
$data['card_subscription_interval_count'] = $this->dataHelper->getSubscriptionIntervalCount();

return $data;
}

public function getInstallmentData()
{
return $this->getOrder()->getPayment()->getAdditionalInformation('xendit_installment');
}
}
6 changes: 2 additions & 4 deletions Xendit/M2Invoice/Controller/Checkout/CCCallback.m22.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class CCCallback extends ProcessHosted
public function execute()
{
try {
$post = $this->getRequest()->getContent();
$callbackToken = $this->getRequest()->getHeader('X-CALLBACK-TOKEN');
$decodedPost = json_decode($post, true);

$orderIds = explode('-', $this->getRequest()->getParam('order_ids'));

$shouldRedirect = false;
Expand Down Expand Up @@ -52,6 +48,8 @@ public function execute()
$order->setGrandTotal($order->getGrandTotal() + $order->getDiscountAmount());
$order->save();
}
$payment->setAdditionalInformation('token_id', $hostedPayment['token_id']);
$payment->setAdditionalInformation('xendit_installment', $hostedPayment['installment']);

$this->processSuccessfulTransaction(
$order,
Expand Down
6 changes: 2 additions & 4 deletions Xendit/M2Invoice/Controller/Checkout/CCCallback.m23.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class CCCallback extends ProcessHosted implements CsrfAwareActionInterface
public function execute()
{
try {
$post = $this->getRequest()->getContent();
$callbackToken = $this->getRequest()->getHeader('X-CALLBACK-TOKEN');
$decodedPost = json_decode($post, true);

$orderIds = explode('-', $this->getRequest()->getParam('order_ids'));

$shouldRedirect = false;
Expand Down Expand Up @@ -55,6 +51,8 @@ public function execute()
$order->setGrandTotal($order->getGrandTotal() + $order->getDiscountAmount());
$order->save();
}
$payment->setAdditionalInformation('token_id', $hostedPayment['token_id']);
$payment->setAdditionalInformation('xendit_installment', $hostedPayment['installment']);

$this->processSuccessfulTransaction(
$order,
Expand Down
29 changes: 24 additions & 5 deletions Xendit/M2Invoice/Controller/Checkout/CCMultishipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
use Magento\Framework\Controller\ResultFactory;
use Magento\Sales\Model\Order;
use Xendit\M2Invoice\Enum\LogDNALevel;
use Magento\Framework\UrlInterface;

class CCMultishipping extends AbstractAction
{
public function execute()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

try {
$rawOrderIds = $this->getRequest()->getParam('order_ids');
$method = $this->getRequest()->getParam('preferred_method');
Expand All @@ -19,6 +23,12 @@ public function execute()
$tokenId = '';
$orders = [];

if ($method === 'cc_subscription' && !$customerSession->isLoggedIn()) {
$message = 'You must logged in to use this payment method';
$this->getLogger()->info($message);
return $this->redirectToCart($message);
}

foreach ($orderIds as $key => $value) {
$order = $this->getOrderFactory()->create();
$order ->load($value);
Expand Down Expand Up @@ -76,7 +86,7 @@ public function execute()
return $this->processFailedPayment($orderIds, $charge['failure_reason']);
}
}
else if ($method === 'cchosted' || $method === 'cc_installment') {
else if ($method === 'cchosted' || $method === 'cc_installment' || $method === 'cc_subscription') {
$requestData = array(
'order_number' => $rawOrderIds,
'amount' => $transactionAmount,
Expand All @@ -88,10 +98,10 @@ public function execute()
'platform_callback_url' => $this->_url->getUrl('xendit/checkout/cccallback') . '?order_ids=' . $rawOrderIds
);

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

if ($method === 'cc_installment') {
$firstName = $billingAddress->getFirstname() ?: $shippingAddress->getFirstname();
$country = $billingAddress->getCountryId() ?: $shippingAddress->getCountryId();
$billingDetails = array(
Expand All @@ -111,6 +121,15 @@ public function execute()

$requestData['is_installment'] = "true";
$requestData['billing_details'] = json_encode($billingDetails, JSON_FORCE_OBJECT);
} else if ($method === 'cc_subscription') {
$requestData['payment_type'] = 'CREDIT_CARD_SUBSCRIPTION';
$requestData['is_subscription'] = "true";
$requestData['subscription_callback_url'] = $this->getDataHelper()->getXenditSubscriptionCallbackUrl(true);
$requestData['payer_email'] = $billingAddress->getEmail();
$requestData['subscription_option'] = json_encode(array(
'interval' => $this->getDataHelper()->getSubscriptionInterval(),
'interval_count' => $this->getDataHelper()->getSubscriptionIntervalCount()
), JSON_FORCE_OBJECT);
}

$hostedPayment = $this->requestHostedPayment($requestData);
Expand All @@ -126,7 +145,7 @@ public function execute()
$this->addCCHostedData($orders, $hostedPayment);

// redirect to hosted payment page
$redirect = "https://tpi-ui.xendit.co/hosted-payments/$hostedPaymentId?hp_token=$hostedPaymentToken";
$redirect = $this->getDataHelper()->getUiUrl() . "/hosted-payments/$hostedPaymentId?hp_token=$hostedPaymentToken";
$resultRedirect = $this->getRedirectFactory()->create();
$resultRedirect->setUrl($redirect);

Expand Down
2 changes: 2 additions & 0 deletions Xendit/M2Invoice/Controller/Checkout/ProcessHosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function execute()
$order->setGrandTotal($order->getGrandTotal() + $order->getDiscountAmount());
$order->save();
}
$payment->setAdditionalInformation('token_id', $hostedPayment['token_id']);
$payment->setAdditionalInformation('xendit_installment', $hostedPayment['installment']);

return $this->processSuccessfulTransaction(
$order,
Expand Down
Loading

0 comments on commit b90fcb2

Please sign in to comment.