Skip to content

Instantly share code, notes, and snippets.

@zackexplosion
Created January 31, 2020 09:44
Show Gist options
  • Save zackexplosion/6f3fcaae8eeebc3b3f26e24caf5e21e3 to your computer and use it in GitHub Desktop.
Save zackexplosion/6f3fcaae8eeebc3b3f26e24caf5e21e3 to your computer and use it in GitHub Desktop.
const moment = require('moment')
const ecpay_payment = require('ecpay-payment')
function getAddress (invoice) {
if (invoice.string_address) {
return invoice.string_address
}
var address = ''
try {
address = `${invoice.address.city}${invoice.address.district}${invoice.address.detail}`
} catch (err) {
// logger.error('get address error', err)
}
return address
}
class Payment {
constructor (params, user, order, base_url) {
this.invoice_params = {}
this.user = user
this.order = order
this.params = Object.assign(params, {
MerchantTradeNo: order.token,
MerchantTradeDate: moment().format('YYYY/MM/DD HH:mm:ss'),
TradeDesc: '小院購物',
TotalAmount: order.total_amount.toString(),
ReturnURL: `${base_url}/ON_PAYMENT_COMPLETE`,
ItemName: this.getItemName()
})
if (params.InvoiceMark === 'Y') {
// let invoice = order.invoice
// this.invoice_params = createInvoiceParams(params, invoice, user)
this.invoice_params = this.getInvoiceParams()
delete params.InvoiceItems
}
delete params.Items
}
getInvoiceParams () {
const { user, order, order: { invoice } } = this
var params = {
RelateNumber: this.params.MerchantTradeNo,
CustomerEmail: user.email,
CustomerIdentifier: '',
InvType: '07',
CarruerType: '', // 載具類別
CarruerNum: '', // 載具編號
CustomerID: '',
ClearanceMark: '2',
LoveCode: '',
InvoiceRemark: '', // 備註
DelayDay: '0',
CustomerAddr: getAddress(invoice),
CustomerPhone: user.mobile_phone || '',
// CustomerPhone: '1111111111',
Print: '0',
TaxType: this.params.TaxType
}
switch (order.invoice.type) {
case 'donated':
params = Object.assign(params, {
CustomerName: user.realname,
Donation: '1',
LoveCode: 'X13579'
})
break
case 'two_copies':
if (typeof user.realname !== 'string') {
throw `user realname error ${user.realname}`
}
params = Object.assign(params, {
CustomerName: user.realname,
Donation: '2'
})
break
case 'three_copies':
params = Object.assign(params, {
CustomerName: invoice.company_name,
CustomerIdentifier: invoice.company_no.toString(),
Donation: '2',
Print: '1'
})
break
}
var itemNames = []
var itemCounts = []
var itemWords = []
var itemPrices = []
var itemTaxTypes = []
order.details.forEach(function (item) {
itemNames.push(item.item_name)
itemCounts.push(item.qty)
itemWords.push('個')
itemPrices.push(item.amount)
itemTaxTypes.push(item.taxType || 1)
})
// 商品名稱
params.InvoiceItemName = itemNames.join('|')
// 商品數量
params.InvoiceItemCount = itemCounts.join('|')
// 商品單位
params.InvoiceItemWord = itemWords.join('|')
// 商品價格
params.InvoiceItemPrice = itemPrices.join('|')
// 商品課稅別
params.InvoiceItemTaxType = itemTaxTypes.join('|')
logger.debug('invoice params', params)
return params
}
getItemName () {
// var { params } = this
var orderItems = this.order.details.map(i => {
return {
name: i.item_name,
quantity: i.qty,
currency: '元',
price: i.amount
}
})
var items = []
// var alipayItemNames = []
// var alipayItemCounts = []
// var alipayItemPrices = []
orderItems.forEach(function (item) {
items.push(item.name + ' ' + item.price + ' ' + item.currency + ' x ' + item.quantity)
// if (params['ChoosePayment'] === 'Alipay') {
// alipayItemNames.push(item.name);
// alipayItemCounts.push(item.quantity);
// alipayItemPrices.push(item.price);
// }
})
return items.join('#').substr(0, 200)
}
checkout () {
const payment = new ecpay_payment()
const { params, invoice_params } = this
// logger.debug('!!!!!!! params !!!!', params)
switch (params.ChoosePayment) {
case 'Credit':
return payment.payment_client.aio_check_out_credit_onetime(params, invoice_params)
case 'WebATM':
return payment.payment_client.aio_check_out_webatm(params, invoice_params)
case 'ATM':
return payment.payment_client.aio_check_out_atm(
params,
'',
'2',
'',
invoice_params
)
default:
return payment.payment_client.aio_check_out_all(params, invoice_params)
}
}
}
module.exports = {
checkout: (params, user, order) => {
const base_url = process.env.PORTAL_PAYMENT_RETURN_URL || process.env.PORTAL_BASE_URL
const payment = new Payment(params, user, order, base_url)
return payment.checkout()
},
payment_client: new ecpay_payment().payment_client
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment