Skip to content

Commit

Permalink
Update payment transaction (#44)
Browse files Browse the repository at this point in the history
* feat: introduce new order api method

* feat: update payment transaction after sending payments

* feat: refactored the api controllers

* feat: added option for overriding controllers

* chore: removed unused import

* feat: provided the option for extending controllers

* feat: call next() on error

* fix: typo in webhook handler

* feat: remove payment instrument from basket on error (#45)

* chore: provide more examples about overriding endpoints

* feat: fail order in case of payment unsuccessful

* chore: extend example

* chore: clean up unused functions

* chore: clean up unused functions

* feat: restructure
  • Loading branch information
amihajlovski authored Nov 27, 2023
1 parent ebf5d3f commit e00f96b
Show file tree
Hide file tree
Showing 26 changed files with 492 additions and 25,595 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {OrderApiClient} from './orderApi'
import {NotificationRequestItem} from '@adyen/api-library/lib/src/typings/notification/notificationRequestItem'
import {ORDER} from '../utils/constants.mjs'
import {ORDER} from '../../utils/constants.mjs'
import Logger from './logger'

const messages = {
AUTH_ERROR: 'Access Denied!',
AUTH_SUCCESS: '[accepted]',
DEFAULT_ERROR: 'Technical error!'
}

async function authorizationWebhookHandler(req, res, next) {
try {
const notification = res.locals.notification
Expand Down Expand Up @@ -38,6 +44,7 @@ async function authorizationWebhookHandler(req, res, next) {
await orderApi.updateOrderStatus(orderNo, ORDER.ORDER_STATUS_FAILED)
}

res.locals.response = messages.AUTH_SUCCESS
return next()
} catch (err) {
return next(err)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
async function getEnvironment(req, res) {
res.json({
async function getEnvironment(req, res, next) {
res.locals.response = {
ADYEN_CLIENT_KEY: process.env.ADYEN_CLIENT_KEY,
ADYEN_ENVIRONMENT: process.env.ADYEN_ENVIRONMENT
})
}
next()
}

export default getEnvironment
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {APPLICATION_VERSION} from '../utils/constants.mjs'
import {APPLICATION_VERSION} from '../../utils/constants.mjs'

const ADYEN_PREFIX = 'ADYEN'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Logger from './logger'

export class OrderApiClient {
tokenUrl =
'https://account.demandware.com/dwsso/oauth2/access_token?grant_type=client_credentials'
Expand Down Expand Up @@ -36,7 +38,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 +49,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 +62,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 +75,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 +88,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 @@ -97,4 +99,21 @@ export class OrderApiClient {
}
return response
}

async updateOrderPaymentTransaction(orderNo, paymentInstrumentId, pspReference) {
const response = await this.base(
'PATCH',
`${orderNo}/payment-instruments/${paymentInstrumentId}/transaction`,
{
body: JSON.stringify({
c_externalReferenceCode: pspReference
})
}
)
if (!response.ok) {
const error = await response.text()
Logger.error(`Payment transaction update failed ${JSON.stringify(error)}`)
}
return response
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {getCurrencyValueForApi} from '../utils/parsers.mjs'
import {BLOCKED_PAYMENT_METHODS} from '../utils/constants.mjs'
import {getCurrencyValueForApi} from '../../utils/parsers.mjs'
import {BLOCKED_PAYMENT_METHODS} from '../../utils/constants.mjs'
import {ShopperCustomers} from 'commerce-sdk-isomorphic'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import AdyenCheckoutConfig from './checkout-config'
import Logger from './logger'
import {createErrorResponse} from '../utils/createErrorResponse.mjs'
import {v4 as uuidv4} from 'uuid'

async function getPaymentMethods(req, res) {
async function getPaymentMethods(req, res, next) {
Logger.info('getPaymentMethods', 'start')
const checkout = AdyenCheckoutConfig.getInstance()

Expand All @@ -24,9 +23,7 @@ async function getPaymentMethods(req, res) {
}
})

const {
locale: {id: shopperLocale}
} = req.body
const {locale: shopperLocale} = req.query
const countryCode = shopperLocale?.slice(-2)

const paymentMethodsRequest = {
Expand All @@ -50,12 +47,11 @@ async function getPaymentMethods(req, res) {
})

Logger.info('getPaymentMethods', 'success')
res.json(response)
res.locals.response = response
next()
} catch (err) {
Logger.error('getPaymentMethods', err.message)
res.status(err.statusCode || 500).json(
createErrorResponse(err.statusCode || 500, err.message)
)
next(err)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {createCheckoutResponse} from '../utils/createCheckoutResponse.mjs'
import {createCheckoutResponse} from '../../utils/createCheckoutResponse.mjs'
import AdyenCheckoutConfig from './checkout-config'
import Logger from './logger'
import {createErrorResponse} from '../utils/createErrorResponse.mjs'
import {v4 as uuidv4} from 'uuid'

const errorMessages = {
PAYMENTS_DETAILS_NOT_SUCCESSFUL: 'payments details call not successful'
}

async function sendPaymentDetails(req, res) {
async function sendPaymentDetails(req, res, next) {
Logger.info('sendPaymentDetails', 'start')
const checkout = AdyenCheckoutConfig.getInstance()
try {
Expand All @@ -21,12 +20,11 @@ async function sendPaymentDetails(req, res) {
if (checkoutResponse.isFinal && !checkoutResponse.isSuccessful) {
throw new Error(errorMessages.PAYMENTS_DETAILS_NOT_SUCCESSFUL)
}
res.json(checkoutResponse)
res.locals.response = checkoutResponse
next()
} catch (err) {
Logger.error('sendPaymentDetails', err.message)
res.status(err.statusCode || 500).json(
createErrorResponse(err.statusCode || 500, err.message)
)
next(err)
}
}

Expand Down
Loading

0 comments on commit e00f96b

Please sign in to comment.