Skip to content

Commit

Permalink
chore: set list payment config
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgnreis authored Mar 20, 2024
1 parent 44f32ec commit a569214
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 40 deletions.
41 changes: 41 additions & 0 deletions functions/assets/onload-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; (function () {
const token = window._pagarmeKey
const storeId = window.storefront && window.storefront.settings && window.storefront.settings.store_id
window._appmaxHash = async function (cardClient) {
return new Promise(async function (resolve, reject) {
const card = {
number: cardClient.number,
name: cardClient.name,
month: cardClient.month,
year: cardClient.year,
cvv: cardClient.cvc
}

const resp = await fetch(
'https://homolog.sandboxappmax.com.br/api/v3/payment/credit-card',
{
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
'access-token': 'token',
card
})
}
)

try {
const data = await resp.json()
if (data.id) {
resolve(data.id)
}
throw new Error('Credencial inválida')
} catch (err) {
console.log('credencial inválida')
// console.error(err)
reject(err)
}
})
}
}())
175 changes: 135 additions & 40 deletions functions/routes/ecom/modules/list-payments.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,144 @@
exports.post = ({ appSdk }, req, res) => {
/**
* Requests coming from Modules API have two object properties on body: `params` and `application`.
* `application` is a copy of your app installed by the merchant,
* including the properties `data` and `hidden_data` with admin settings configured values.
* JSON Schema reference for the List Payments module objects:
* `params`: https://apx-mods.e-com.plus/api/v1/list_payments/schema.json?store_id=100
* `response`: https://apx-mods.e-com.plus/api/v1/list_payments/response_schema.json?store_id=100
*
* Examples in published apps:
* https://github.com/ecomplus/app-pagarme/blob/master/functions/routes/ecom/modules/list-payments.js
* https://github.com/ecomplus/app-custom-payment/blob/master/functions/routes/ecom/modules/list-payments.js
*/
const { baseUri } = require('../../../__env')
const path = require('path')
const fs = require('fs')
const addInstallments = require('../../../lib/payments/add-installments')

exports.post = ({ appSdk }, req, res) => {
// https://apx-mods.e-com.plus/api/v1/list_payments/schema.json?store_id=100
const { params, application } = req.body
const { storeId } = req
// setup basic required response object
const amount = params.amount || {}
const initialTotalAmount = amount.total

const config = Object.assign({}, application.data, application.hidden_data)
if (!config.token || !config.public_key) {
return res.status(409).send({
error: 'NO_APPMAX_KEYS',
message: 'Chave de API e/ou criptografia não configurada (lojista deve configurar o aplicativo)'
})
}

// https://apx-mods.e-com.plus/api/v1/list_payments/response_schema.json?store_id=100
const response = {
payment_gateways: []
}
// merge all app options configured by merchant
const appData = Object.assign({}, application.data, application.hidden_data)

/* DO THE STUFF HERE TO FILL RESPONSE OBJECT WITH PAYMENT GATEWAYS */

/**
* Sample snippets:
// add new payment method option
response.payment_gateways.push({
intermediator: {
code: 'paupay',
link: 'https://www.palpay.com.br',
name: 'paupay'
},
payment_url: 'https://www.palpay.com.br/',
type: 'payment',
payment_method: {
code: 'banking_billet',
name: 'Boleto Bancário'
},
label: 'Boleto Bancário',
expiration_date: appData.expiration_date || 14
})

*/
const { discount } = config
if (discount && discount.value > 0) {
if (discount.apply_at !== 'freight') {
// default discount option
const { value } = discount
response.discount_option = {
label: config.discount_option_label,
value
}
// specify the discount type and min amount is optional
;['type', 'min_amount'].forEach(prop => {
if (discount[prop]) {
response.discount_option[prop] = discount[prop]
}
})
}

if (amount.total) {
// check amount value to apply discount
if (amount.total < discount.min_amount) {
discount.value = 0
} else {
delete discount.min_amount

// fix local amount object
const maxDiscount = amount[discount.apply_at || 'subtotal']
let discountValue
if (discount.type === 'percentage') {
discountValue = maxDiscount * discount.value / 100
} else {
discountValue = discount.value
if (discountValue > maxDiscount) {
discountValue = maxDiscount
}
}
if (discountValue > 0) {
amount.discount = (amount.discount || 0) + discountValue
amount.total -= discountValue
if (amount.total < 0) {
amount.total = 0
}
}
}
}
}

// setup payment gateway objects
const intermediator = {
name: 'Appmax',
link: 'https://appmax.com.br/',
code: 'appmax'
}
;['credit_card', 'banking_billet', 'account_deposit'].forEach(paymentMethod => {
const methodConfig = config[paymentMethod] || {}
const isPix = paymentMethod === 'account_deposit'
if (!methodConfig.disable && (!isPix || methodConfig.enable)) {
const isCreditCard = paymentMethod === 'credit_card'
let label = methodConfig.label
if (!label) {
if (isCreditCard) {
label = 'Cartão de crédito'
} else {
label = !isPix ? 'Boleto bancário' : 'Pix'
}
}
const isDiscountInOneParcel = discount[paymentMethod] === '1 parcela'
if (isCreditCard && (typeof discount[paymentMethod] === 'string')) {
discount[paymentMethod] = isDiscountInOneParcel || discount[paymentMethod] === 'Todas as parcelas' || false
}
const gateway = {
label,
icon: methodConfig.icon,
text: methodConfig.text,
payment_method: {
code: paymentMethod,
name: `${label} - ${intermediator.name}`
},
intermediator
}

if (methodConfig.discount) {
gateway.discount = methodConfig.discount
} else if (
discount &&
(discount[paymentMethod] === true || (!isCreditCard && discount[paymentMethod] !== false))
) {
gateway.discount = discount
if (response.discount_option && !response.discount_option.label) {
response.discount_option.label = label
}
}

if (isCreditCard) {
if (!gateway.icon) {
gateway.icon = `${baseUri}/credit-card.png`
}
// https://github.com/pagarme/pagarme-js
gateway.js_client = {
script_uri: 'https://assets.pagar.me/pagarme-js/4.8/pagarme.min.js',
onload_expression: `window._pagarmeKey="${config.pagarme_encryption_key}";` +
fs.readFileSync(path.join(__dirname, '../../../public/onload-expression.min.js'), 'utf8'),
cc_hash: {
function: '_pagarmeHash',
is_promise: true
}
}
const { installments } = config
if (installments) {
const installmentsTotal = gateway.discount ? amount.total : initialTotalAmount
// list all installment options and default one
addInstallments(installmentsTotal, installments, gateway, response, initialTotalAmount, isDiscountInOneParcel)
}
}

response.payment_gateways.push(gateway)
}
})

res.send(response)
}

0 comments on commit a569214

Please sign in to comment.