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

Nourma Rafita Sari #39

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
dist
node_modules
18 changes: 18 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}

const express = require("express");
const app = express();
const router = require("./routes");
const cors = require("cors");

app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(express.json());

app.use(router);

module.exports = app;
8 changes: 8 additions & 0 deletions bin/www.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const app = require('../app')
const port = process.env.PORT || 3001;


app.listen(port, () => {
console.log(`listening on port ${port}`);
});

23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "postgres",
"password": "nurma123",
"database": "abeille",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
37 changes: 37 additions & 0 deletions controllers/Http/Midtrans/ApiRequestor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'
const axios = require("axios")
class ApiRequestor {
static get(url, server_key, data_hash) {
return this.remoteCall(url, server_key, data_hash, false);
}
static post(url, server_key, data_hash) {
return this.remoteCall(url, server_key, data_hash, true);
}
static remoteCall(url, server_key, data_hash, post = true){
const headers = {
"Content-Type" : "application/json",
Accept : "application/json",
Authorization:
"Basic " + Buffer.from(server_key + ":").toString("base64")
}
let body = JSON.stringify(data_hash)
let result
if (post){
result = axios.post(url, body, {
headers: headers
}).then((res) => {
return res.data
}).catch(e => console.log(e))
}else {
result = axios.get(url, {
headers: headers
}).then((res) => {
return res.data
}).catch(e => console.log(e))
}

return result
}
}

module.exports = ApiRequestor;
16 changes: 16 additions & 0 deletions controllers/Http/Midtrans/Config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'
const SANDBOX_BASE_URL = "https://api.sandbox.midtrans.com/v2";
const PRODUCTION_BASE_URL = "https://api.midtrans.com/v2";

class Config {
static serverKey = "SB-Mid-server-BKdejlopT9IIrgic6roqsBNv";
static isProduction = false;
static is3ds = false
static isSanitized = false

static getBaseUrl(){
return Config.isProduction ? PRODUCTION_BASE_URL : SANDBOX_BASE_URL;
}
}

module.exports = Config
16 changes: 16 additions & 0 deletions controllers/Http/Midtrans/CoreApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict"
const ApiRequestor = require('./ApiRequestor')
const Config = require('./Config')
class CoreApi {
static charge(payloads){
let result = ApiRequestor.post(
Config.getBaseUrl() + "/charge",
Config.serverKey,
payloads,
)

return result
}
}

module.exports = CoreApi
100 changes: 100 additions & 0 deletions controllers/Http/Payments/BankTransfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict'
class BankTransfer {
constructor(items = [], customer) {
this.items = items;
this.customer = customer;
}

baseBody() {
let gross_amount = 0;
let order_id = new Date().getTime();

let items = this.items;
let customer = this.customer;

items.forEach(function (item) {
gross_amount += item.price * item.quantity;
});

let body = {
payment_type: "bank_transfer",
transaction_details: {
gross_amount,
order_id: order_id,
},
customer_details: {
email: customer.email,
first_name: customer.first_name,
last_name: customer.last_name,
phone: customer.phone,
},
item_details: this.items,
};

return body;
}

bca() {
let base = this.baseBody();
let mybody = {
payment_type: base.payment_type,
transaction_details: base.transaction_details,
customer_details: base.customer_details,
item_details: base.item_details,
bank_transfer: {
bank: "BCA",
va_number: "12345678901",
free_text: {
inquiry: [
{
id: "text indonesia",
en: "text english",
},
],
payment: [
{
id: "pembayaran produk",
en: "product payment",
},
],
},
},
};

return mybody;
}

bni() {
let base = this.baseBody();
let mybody = {
payment_type: base.payment_type,
transaction_details: base.transaction_details,
customer_details: base.customer_details,
item_details: base.item_details,
bank_transfer: {
bank: "bni",
va_number: "12345678",
},
};

return mybody;
}

permata() {
let base = this.baseBody();
let mybody = {
payment_type: base.payment_type,
transaction_details: base.transaction_details,
customer_details: base.customer_details,
item_details: base.item_details,
bank_transfer: {
bank: "permata",
va_number: "1234567890",
},
};

return mybody;
}
}

module.exports = BankTransfer;
42 changes: 42 additions & 0 deletions controllers/Http/Payments/IndexController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict"

const CoreApi = require("../Midtrans/CoreApi")
const BankTransfer = require("./BankTransfer");
class IndexController{
static async bankTransfer(req, res){
try {
let data;
console.log(req, "<<<< req");
let body = req.body;
let customers = {
email: "classicimplements@gmail.com",
first_name: "CLASSIC",
last_name: "IMPLEMENTS",
phone: "089179179111",
};

let bankTransfer = await new BankTransfer(body.items, customers);
console.log(bankTransfer, "<<< bank");
switch (body.channel) {
case "BCA":
data = bankTransfer.bca();
break;
case "BNI":
data = bankTransfer.bni();
break;
case "PERMATA":
data = bankTransfer.permata();
break;
}
// return data
console.log(data, "<<<< data");
await CoreApi.charge(data);
console.log(await CoreApi.charge(data), "<<<< coreApi");;
res.status(201).json(data);
} catch (err) {
console.log(err);
}
}
}

module.exports = IndexController
Loading