Skip to content

Commit

Permalink
feat: update payment transaction after sending payments
Browse files Browse the repository at this point in the history
  • Loading branch information
amihajlovski committed Nov 21, 2023
1 parent 1bb6dc3 commit ab06e1a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 110 deletions.
21 changes: 7 additions & 14 deletions adyen/controllers/orderApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class OrderApiClient {
}

async getOrder(orderNo) {
const response = await this.base('get', orderNo)
const response = await this.base('GET', orderNo)
if (!response.ok) {
const error = await response.text()
throw new Error(`${response.status} ${response.statusText}`, {
Expand All @@ -47,7 +47,7 @@ export class OrderApiClient {
}

async updateOrderStatus(orderNo, status) {
const response = await this.base('put', `${orderNo}/status`, {
const response = await this.base('PUT', `${orderNo}/status`, {
body: JSON.stringify({status: status})
})
if (!response.ok) {
Expand All @@ -60,7 +60,7 @@ export class OrderApiClient {
}

async updateOrderPaymentStatus(orderNo, status) {
const response = await this.base('put', `${orderNo}/payment-status`, {
const response = await this.base('PUT', `${orderNo}/payment-status`, {
body: JSON.stringify({status: status})
})
if (!response.ok) {
Expand All @@ -73,7 +73,7 @@ export class OrderApiClient {
}

async updateOrderExportStatus(orderNo, status) {
const response = await this.base('put', `${orderNo}/export-status`, {
const response = await this.base('PUT', `${orderNo}/export-status`, {
body: JSON.stringify({status: status})
})
if (!response.ok) {
Expand All @@ -86,7 +86,7 @@ export class OrderApiClient {
}

async updateOrderConfirmationStatus(orderNo, status) {
const response = await this.base('put', `${orderNo}/confirmation-status`, {
const response = await this.base('PUT', `${orderNo}/confirmation-status`, {
body: JSON.stringify({status: status})
})
if (!response.ok) {
Expand All @@ -99,21 +99,14 @@ export class OrderApiClient {
}

async updateOrderPaymentTransaction(orderNo, paymentInstrumentId, pspReference) {
const response = await this.base(
'patch',
return await this.base(
'PATCH',
`${orderNo}/payment-instruments/${paymentInstrumentId}/transaction`,
{
body: JSON.stringify({
c_externalReferenceCode: pspReference
})
}
)
if (!response.ok) {
const error = await response.text()
throw new Error(`${response.status} ${response.statusText}`, {
cause: error
})
}
return response
}
}
191 changes: 95 additions & 96 deletions adyen/controllers/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {formatAddressInAdyenFormat} from '../utils/formatAddress.mjs'
import {getCurrencyValueForApi} from '../utils/parsers.mjs'
import {
APPLICATION_VERSION,
ORDER,
PAYMENT_METHODS,
RECURRING_PROCESSING_MODEL,
SHOPPER_INTERACTIONS
Expand Down Expand Up @@ -61,16 +62,96 @@ const filterStateData = (stateData) => {
return filteredStateData
}

async function sendPayments(req, res) {
function getShopperName(order) {
const [firstName, lastName] = order.customerName.split(' ')
return {
firstName,
lastName
}
}

function getApplicationInfo() {
return {
merchantApplication: {
name: 'adyen-salesforce-commerce-cloud',
version: APPLICATION_VERSION
},
externalPlatform: {
name: 'SalesforceCommerceCloud',
version: 'PWA',
integrator: process.env.SYSTEM_INTEGRATOR_NAME
}
}
}

function isOpenInvoiceMethod(paymentMethod) {
return (
paymentMethod.indexOf('afterpay') > -1 ||
paymentMethod.indexOf('klarna') > -1 ||
paymentMethod.indexOf('ratepay') > -1 ||
paymentMethod.indexOf('facilypay') > -1 ||
paymentMethod === 'zip' ||
paymentMethod === 'affirm' ||
paymentMethod === 'clearpay'
)
}

function getLineItems(order) {
const productLineItems = order?.productItems?.length
? order?.productItems?.map((productItem) => {
return {
id: productItem.itemId,
quantity: productItem.quantity,
description: productItem.itemText,
amountExcludingTax: getCurrencyValueForApi(productItem.basePrice, order.currency),
taxAmount: getCurrencyValueForApi(productItem.tax, order.currency),
taxCategory: 'None',
taxPercentage: productItem.taxRate
}
})
: []
const shippingLineItems = order?.shippingItems?.length
? order?.shippingItems?.map((shippingItem) => {
return {
id: shippingItem.itemId,
quantity: 1,
description: shippingItem.itemText,
amountExcludingTax: getCurrencyValueForApi(
shippingItem.basePrice,
order.currency
),
taxAmount: getCurrencyValueForApi(shippingItem.tax, order.currency),
taxCategory: 'None',
taxPercentage: shippingItem.taxRate
}
})
: []
const priceAdjustmentLineItems = order?.priceAdjustments?.length
? order.priceAdjustments.map((priceAdjustment) => {
return {
id: priceAdjustment.priceAdjustmentId,
quantity: priceAdjustment.quantity,
description: priceAdjustment.itemText,
amountExcludingTax: getCurrencyValueForApi(
priceAdjustment.basePrice,
order.currency
),
taxAmount: getCurrencyValueForApi(priceAdjustment.tax, order.currency),
taxCategory: 'None',
taxPercentage: priceAdjustment.taxRate
}
})
: []
return [...productLineItems, ...shippingLineItems, ...priceAdjustmentLineItems]
}

async function sendPayments(req, res, next) {
Logger.info('sendPayments', 'start')
if (!validateRequestParams(req)) {
throw new Error(errorMessages.INVALID_PARAMS)
}

const checkout = AdyenCheckoutConfig.getInstance()
let basket
let order
let paymentInstrument

try {
const {app: appConfig} = getConfig()
Expand All @@ -80,7 +161,7 @@ async function sendPayments(req, res) {
headers: {authorization: req.headers.authorization}
})

basket = await shopperBaskets.getBasket({
const basket = await shopperBaskets.getBasket({
parameters: {
basketId: req.headers.basketid
}
Expand All @@ -92,7 +173,7 @@ async function sendPayments(req, res) {

if (!basket?.paymentInstruments || !basket?.paymentInstruments?.length) {
Logger.info('sendPayments', 'addPaymentInstrumentToBasket')
paymentInstrument = await shopperBaskets.addPaymentInstrumentToBasket({
await shopperBaskets.addPaymentInstrumentToBasket({
body: {
amount: basket.orderTotal,
paymentMethodId: PAYMENT_METHODS.ADYEN_COMPONENT,
Expand All @@ -111,7 +192,7 @@ async function sendPayments(req, res) {
headers: {authorization: req.headers.authorization}
})

order = await shopperOrders.createOrder({
const order = await shopperOrders.createOrder({
body: {
basketId: req.headers.basketid
}
Expand Down Expand Up @@ -164,19 +245,20 @@ async function sendPayments(req, res) {
})
Logger.info('sendPayments', `resultCode ${response.resultCode}`)

// const orderApi = new OrderApiClient()
// await orderApi.updateOrderPaymentTransaction(
// order.orderNo,
// order.paymentInstruments[0].paymentInstrumentId,
// response.pspReference
// )
const orderApi = new OrderApiClient()
await orderApi.updateOrderPaymentTransaction(
order.orderNo,
order.paymentInstruments[0].paymentInstrumentId,
response.pspReference
)

const checkoutResponse = createCheckoutResponse(response)
if (checkoutResponse.isFinal && !checkoutResponse.isSuccessful) {
throw new Error(errorMessages.PAYMENT_NOT_SUCCESSFUL)
}

res.json(checkoutResponse)
return next()
} catch (err) {
Logger.error('sendPayments', err.message)
res.status(err.statusCode || 500).json(
Expand All @@ -185,87 +267,4 @@ async function sendPayments(req, res) {
}
}

function getShopperName(order) {
const [firstName, lastName] = order.customerName.split(' ')
return {
firstName,
lastName
}
}

function getApplicationInfo() {
return {
merchantApplication: {
name: 'adyen-salesforce-commerce-cloud',
version: APPLICATION_VERSION
},
externalPlatform: {
name: 'SalesforceCommerceCloud',
version: 'PWA',
integrator: process.env.SYSTEM_INTEGRATOR_NAME
}
}
}

function isOpenInvoiceMethod(paymentMethod) {
return (
paymentMethod.indexOf('afterpay') > -1 ||
paymentMethod.indexOf('klarna') > -1 ||
paymentMethod.indexOf('ratepay') > -1 ||
paymentMethod.indexOf('facilypay') > -1 ||
paymentMethod === 'zip' ||
paymentMethod === 'affirm' ||
paymentMethod === 'clearpay'
)
}

function getLineItems(order) {
const productLineItems = order?.productItems?.length
? order?.productItems?.map((productItem) => {
return {
id: productItem.itemId,
quantity: productItem.quantity,
description: productItem.itemText,
amountExcludingTax: getCurrencyValueForApi(productItem.basePrice, order.currency),
taxAmount: getCurrencyValueForApi(productItem.tax, order.currency),
taxCategory: 'None',
taxPercentage: productItem.taxRate
}
})
: []
const shippingLineItems = order?.shippingItems?.length
? order?.shippingItems?.map((shippingItem) => {
return {
id: shippingItem.itemId,
quantity: 1,
description: shippingItem.itemText,
amountExcludingTax: getCurrencyValueForApi(
shippingItem.basePrice,
order.currency
),
taxAmount: getCurrencyValueForApi(shippingItem.tax, order.currency),
taxCategory: 'None',
taxPercentage: shippingItem.taxRate
}
})
: []
const priceAdjustmentLineItems = order?.priceAdjustments?.length
? order.priceAdjustments.map((priceAdjustment) => {
return {
id: priceAdjustment.priceAdjustmentId,
quantity: priceAdjustment.quantity,
description: priceAdjustment.itemText,
amountExcludingTax: getCurrencyValueForApi(
priceAdjustment.basePrice,
order.currency
),
taxAmount: getCurrencyValueForApi(priceAdjustment.tax, order.currency),
taxCategory: 'None',
taxPercentage: priceAdjustment.taxRate
}
})
: []
return [...productLineItems, ...shippingLineItems, ...priceAdjustmentLineItems]
}

export default sendPayments

0 comments on commit ab06e1a

Please sign in to comment.