Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thankyou page for akulaku #17

Merged
merged 6 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_STORE
28 changes: 4 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,20 @@ Let your Drupal Commerce 2 store integrated with Midtrans payment gateway.
This is the official Midtrans extension for the Drupal Commerce 2 E-commerce platform.

### Version
2.0.1
2.1.0
(for Drupal v8.x and Drupal v9.x)

### Requirements
The following plugin is tested under following environment:

* PHP v5.6.x or greater
* MySQL version 5.0 or greater
* Drupal v8.x or greater
* [Drupal v8.x or greater](https://www.drupal.org/project/drupal)
* [Drupal Commerce 8.x-2.xx ](http://www.drupal.org/project/commerce)

#### Composer Installation
If you are using [Composer](https://getcomposer.org), you can install via composer CLI
By default, composer.json in drupal site only add drupal repository, because this plugin store in github, you need add this require line to your `composer.json` file:

```json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/midtrans/midtrans-drupal8"
}
]
```
run: `composer require midtrans/midtrans-drupal8` on your terminal.

or
```json
{
"require": {
"midtrans/midtrans-drupal8": "2.0.1"
}
}
```
run `composer update` on your terminal.
run: `composer require drupal/midtrans_commerce` on your terminal.

#### Manual Instalation
The manual installation method involves downloading our feature-rich plugin and uploading it to your webserver via your favourite FTP application.
Expand All @@ -64,7 +44,7 @@ The manual installation method involves downloading our feature-rich plugin and
#### Midtrans Map Configuration
1. Go to **Settings > Configuration**.
2. Insert ``http://[your web]/payment/notify/midtrans`` as your Payment Notification URL in your MAP.
3. Insert ``http://[your web]`` link as Finish/Unfinish/Error Redirect URL in your MAP configuration.
3. Insert ``http://[your web]/payment/finish/midtrans`` link as Finish/Unfinish/Error Redirect URL in your MAP configuration.

#### Advanced Usage
<details>
Expand Down
11 changes: 7 additions & 4 deletions commerce_midtrans.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ commerce_payment_midtrans.notify:
_controller: '\Drupal\commerce_midtrans\Controller\MidtransNotification::notifyPage'
requirements:
_access: 'TRUE'
# options:
# parameters:
# commerce_payment_gateway:
# type: entity:commerce_payment_gateway

commerce_payment_midtrans.thankyoupage:
path: '/payment/finish/midtrans'
defaults:
_controller: '\Drupal\commerce_midtrans\Controller\MidtransFinishUrl::finishPage'
requirements:
_access: 'TRUE'
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "midtrans/midtrans-drupal8",
"name": "drupal/midtrans_commerce",
"description": "The official Midtrans extension for the Drupal Commerce 2",
"homepage": "https://midtrans.com",
"version": "2.0.1",
"homepage": "http://drupal.org/project/midtrans_commerce",
"license": "GPL-2.0+",
"type": "drupal-module",
"license":"MIT",
"require": {
"midtrans/midtrans-php": "^2.4"
"midtrans/midtrans-php": "^2.4.2"
}
}
124 changes: 124 additions & 0 deletions src/Controller/MidtransFinishUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Drupal\commerce_midtrans\Controller;

use Drupal\commerce_payment\Entity\PaymentGatewayInterface;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* Provides the finish url for midtrans payment gateway.
*/
class MidtransFinishUrl extends ControllerBase {
/**
* Return finish url
*/
public function finishPage(Request $request) {
$get_payment = FALSE;
$params = \Drupal::request()->request->all();

// bca_klikpay will redirect to this page with query params ?id=transaction_id
if ($request->query->get('id')) {
$transaction_id = $request->query->get('id');
$get_payment = $this->getPaymentByTransactionId($transaction_id);
}
else if (isset($params['response'])) {
// akulaku, cimb_clicks, danamon_online, bri_epay will redirect to this page with POST method
$response = json_decode($params['response'], TRUE);
$transaction_id = $response['transaction_id'];
$get_payment = $this->getPaymentByTransactionId($transaction_id);
}
else if ($request->query->get('order_id')) {
// other payment methods use this for redirection
$transaction_id = $request->query->get('order_id');
$get_payment = $this->getPaymentByTransactionId($transaction_id, TRUE);
}

if (!$get_payment) {
$base_url = \Drupal::request()->getSchemeAndHttpHost().base_path();
$response = new RedirectResponse($base_url);
$response->send();
}

$config = $get_payment->getPaymentGateway()->getPluginConfiguration();
$result = $this->getStatus($config, $transaction_id);
if (!$result) {
$payment_path = 'checkout/'.$get_payment->getOrderId().'/payment/return';
$base_url = \Drupal::request()->getSchemeAndHttpHost().base_path().$payment_path;
$response = new RedirectResponse($base_url);
$response->send();
}

$message = $this->buildMessage($result);
return [
'#markup' => $message
];
}

protected function getPaymentByTransactionId($transaction_id, $use_order_id = FALSE) {
$entity = \Drupal::entityTypeManager();
$storage = $entity->getStorage('commerce_payment');

if ($use_order_id) {
$params = array('order_id' => $transaction_id);
}
else {
$params = array('remote_id' => $transaction_id);
}
$get_payments = $storage->loadByProperties($params);
return reset($get_payments);
}

protected function getStatus($config, $transaction_id) {
\Midtrans\Config::$serverKey = $config['server_key'];
\Midtrans\Config::$isProduction = ($config['mode'] == 'production') ? TRUE : FALSE;

try {
return \Midtrans\Transaction::status($transaction_id);
}
catch (\Exception $e) {
return FALSE;
}
}

protected function buildMessage($data) {
$message = '<p><strong>Thank you, here is the detail payment from Midtrans</strong><br />';
$message .= 'Order ID: '.$data->order_id.'<br>';
$message .= 'Transaction ID: '.$data->transaction_id.'<br>';
$message .= 'Transaction Status: '.ucwords($data->transaction_status).'<br>';
$message .= 'Payment Type: '.$this->detailPaymentType($data).'<br>';
$message .= 'You can view your order on your account page when logged in.';

return $message;
}

protected function detailPaymentType($data) {
$result = '-';
if ($data->payment_type == 'credit_card') {
$result = ucwords($data->card_type).' Card - Mask Card: '.$data->masked_card;
}
else if ($data->payment_type == 'qris') {
$issuer = isset($data->issuer) ? ' - Issuer: '.ucwords($data->issuer) : '';
$result = 'QRIS - Acquirer: '.ucwords($data->acquirer).$issuer;
} else if ($data->payment_type == 'cstore') {
$result = ucwords($data->store).' - Payment Code: '.$data->payment_code;
}
else if ($data->payment_type == 'echannel') {
$result = 'Mandiri Bill - Bill Number: '.$data->bill_key;
}
else if ($data->payment_type == 'bank_transfer') {
if (isset($data->permata_va_number)){
$result = 'Permata VA - '.$data->permata_va_number;
}
else {
$result = strtoupper($data->va_numbers[0]->bank).' VA - '.$data->va_numbers[0]->va_number;
}
}
else {
$result = ucwords($data->payment_type);
}

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Controller/MidtransNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MidtransNotification {
* The response.
*/
public function notifyPage(Request $request) {
$raw_notification = json_decode(file_get_contents('php://input'), true);
$raw_notification = json_decode(file_get_contents('php://input'), TRUE);
$order_id = $raw_notification['order_id'];

if (empty($order_id)) {
Expand Down
1 change: 1 addition & 0 deletions src/PluginForm/MidtransForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$payment_gateway_plugin = $create_payment->getPaymentGateway()->getPlugin();
$gateway_mode = $payment_gateway_plugin->getMode();
$configuration = $payment_gateway_plugin->getConfiguration();
$snap_token = FALSE;

if (version_compare(\Drupal::VERSION, "9.0.0", ">=")) {
$plugin_info = \Drupal::service('extension.list.module')->getExtensionInfo('commerce_midtrans');
Expand Down
1 change: 1 addition & 0 deletions src/PluginForm/MidtransInstallmentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$payment_gateway_plugin = $create_payment->getPaymentGateway()->getPlugin();
$gateway_mode = $payment_gateway_plugin->getMode();
$configuration = $payment_gateway_plugin->getConfiguration();
$snap_token = FALSE;

if (version_compare(\Drupal::VERSION, "9.0.0", ">=")) {
$plugin_info = \Drupal::service('extension.list.module')->getExtensionInfo('commerce_midtrans');
Expand Down
1 change: 1 addition & 0 deletions src/PluginForm/MidtransOfflineInstallmentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$payment_gateway_plugin = $create_payment->getPaymentGateway()->getPlugin();
$gateway_mode = $payment_gateway_plugin->getMode();
$configuration = $payment_gateway_plugin->getConfiguration();
$snap_token = FALSE;

if (version_compare(\Drupal::VERSION, "9.0.0", ">=")) {
$plugin_info = \Drupal::service('extension.list.module')->getExtensionInfo('commerce_midtrans');
Expand Down
1 change: 1 addition & 0 deletions src/PluginForm/MidtransPromoForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$payment_gateway_plugin = $create_payment->getPaymentGateway()->getPlugin();
$gateway_mode = $payment_gateway_plugin->getMode();
$configuration = $payment_gateway_plugin->getConfiguration();
$snap_token = FALSE;

if (version_compare(\Drupal::VERSION, "9.0.0", ">=")) {
$plugin_info = \Drupal::service('extension.list.module')->getExtensionInfo('commerce_midtrans');
Expand Down