Adds update payment + minor fixes
This commit is contained in:
@@ -13,7 +13,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
switch (event.success) {
|
switch (event.success) {
|
||||||
case "false":
|
case "false":
|
||||||
res.status(400).send(event.reason)
|
res.status(400).send("[accepted]")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
res.status(200).send("[accepted]")
|
res.status(200).send("[accepted]")
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { Validator, MedusaError } from "medusa-core-utils"
|
|||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const schema = Validator.object().keys({
|
const schema = Validator.object().keys({
|
||||||
cart_id: Validator.string().required(),
|
cart_id: Validator.string().required(),
|
||||||
payment_method: Validator.object().required(),
|
|
||||||
provider_id: Validator.string().required(),
|
provider_id: Validator.string().required(),
|
||||||
|
payment_data: Validator.object().required(),
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = schema.validate(req.body)
|
const { value, error } = schema.validate(req.body)
|
||||||
@@ -20,7 +20,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
const { data } = await paymentProvider.authorizePayment(
|
const { data } = await paymentProvider.authorizePayment(
|
||||||
cart,
|
cart,
|
||||||
value.payment_method
|
value.payment_data.paymentMethod
|
||||||
)
|
)
|
||||||
|
|
||||||
const transactionReference = data.pspReference
|
const transactionReference = data.pspReference
|
||||||
@@ -46,6 +46,11 @@ export default async (req, res) => {
|
|||||||
newPaymentSession
|
newPaymentSession
|
||||||
)
|
)
|
||||||
|
|
||||||
|
await cartService.setPaymentMethod(cart._id, {
|
||||||
|
provider_id: value.provider_id,
|
||||||
|
data,
|
||||||
|
})
|
||||||
|
|
||||||
res.status(200).json({ data })
|
res.status(200).json({ data })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import { Validator, MedusaError } from "medusa-core-utils"
|
|||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const schema = Validator.object().keys({
|
const schema = Validator.object().keys({
|
||||||
payment_provider: Validator.string().required(),
|
payload: Validator.string().required(),
|
||||||
payload: Validator.object().required(),
|
payment_data: Validator.string().required(),
|
||||||
payment_data: Validator.object().required(),
|
provider_id: Validator.string().required(),
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = schema.validate(req.body)
|
const { value, error } = schema.validate(req.body)
|
||||||
@@ -13,17 +13,15 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const adyenService = req.scope.resolve("adyenService")
|
const adyen = req.scope.resolve("adyenService")
|
||||||
const paymentProviderService = req.scope.resolve(
|
const paymentProvider = req.scope.resolve(`pp_${value.provider_id}`)
|
||||||
`${value.payment_provider}AdyenProviderService`
|
|
||||||
)
|
|
||||||
|
|
||||||
const adyenResultCode = await adyenService.checkPaymentResult(
|
const { data } = await adyen.checkPaymentResult(
|
||||||
value.payment_data,
|
value.payment_data,
|
||||||
value.payload
|
value.payload
|
||||||
)
|
)
|
||||||
|
|
||||||
const status = paymentProviderService.getStatus(adyenResultCode)
|
const status = await paymentProvider.getStatus(data)
|
||||||
|
|
||||||
res.status(200).json({ status })
|
res.status(200).json({ status })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -33,6 +33,18 @@ export default (app, rootDirectory) => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
route.post(
|
route.post(
|
||||||
|
"/update",
|
||||||
|
bodyParser.json(),
|
||||||
|
middlewares.wrap(require("./update-payment").default)
|
||||||
|
)
|
||||||
|
|
||||||
|
route.post(
|
||||||
|
"/payment-status",
|
||||||
|
bodyParser.json(),
|
||||||
|
middlewares.wrap(require("./check-payment-status").default)
|
||||||
|
)
|
||||||
|
|
||||||
|
route.get(
|
||||||
"/payment-status",
|
"/payment-status",
|
||||||
bodyParser.json(),
|
bodyParser.json(),
|
||||||
middlewares.wrap(require("./check-payment-status").default)
|
middlewares.wrap(require("./check-payment-status").default)
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ export default async (req, res) => {
|
|||||||
(ps) => ps.provider_id.split("Adyen")[0]
|
(ps) => ps.provider_id.split("Adyen")[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (allowedMethods.length === 0) {
|
||||||
|
res.status(200).json({ paymentMethods: {} })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const { data } = await adyenService.retrievePaymentMethods(
|
const { data } = await adyenService.retrievePaymentMethods(
|
||||||
cart,
|
cart,
|
||||||
allowedMethods,
|
allowedMethods,
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
|
|
||||||
|
export default async (req, res) => {
|
||||||
|
const schema = Validator.object().keys({
|
||||||
|
cart_id: Validator.string().required(),
|
||||||
|
provider_id: Validator.string().required(),
|
||||||
|
payment_data: Validator.object().required(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const { value, error } = schema.validate(req.body)
|
||||||
|
if (error) {
|
||||||
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const cartService = req.scope.resolve("cartService")
|
||||||
|
const paymentProvider = req.scope.resolve(`pp_${value.provider_id}`)
|
||||||
|
|
||||||
|
const cart = await cartService.retrieve(value.cart_id)
|
||||||
|
|
||||||
|
const { data } = await paymentProvider.updatePayment(
|
||||||
|
value.payment_data.paymentData,
|
||||||
|
value.payment_data.details
|
||||||
|
)
|
||||||
|
|
||||||
|
await cartService.updatePaymentSession(cart._id, value.provider_id, data)
|
||||||
|
|
||||||
|
res.status(200).json({ data })
|
||||||
|
} catch (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ class AdyenService extends BaseService {
|
|||||||
this.options_ = options
|
this.options_ = options
|
||||||
|
|
||||||
this.adyenCheckoutApi = axios.create({
|
this.adyenCheckoutApi = axios.create({
|
||||||
baseURL: "https://checkout-test.adyen.com/v52",
|
baseURL: "https://checkout-test.adyen.com/v53",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"x-API-key": this.options_.api_key,
|
"x-API-key": this.options_.api_key,
|
||||||
@@ -24,7 +24,7 @@ class AdyenService extends BaseService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.adyenPaymentApi = axios.create({
|
this.adyenPaymentApi = axios.create({
|
||||||
baseURL: "https://pal-test.adyen.com/pal/servlet/Payment/v52",
|
baseURL: "https://pal-test.adyen.com/pal/servlet/Payment/v53",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"x-API-key": this.options_.api_key,
|
"x-API-key": this.options_.api_key,
|
||||||
@@ -96,6 +96,14 @@ class AdyenService extends BaseService {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updatePayment(paymentData, details) {
|
||||||
|
const request = {
|
||||||
|
paymentData,
|
||||||
|
details,
|
||||||
|
}
|
||||||
|
return this.adyenCheckoutApi.post("/payments/details", request)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates and authorizes an Ayden payment
|
* Creates and authorizes an Ayden payment
|
||||||
* @returns {Object} payment data result
|
* @returns {Object} payment data result
|
||||||
@@ -146,11 +154,29 @@ class AdyenService extends BaseService {
|
|||||||
const { pspReference, amount } = data
|
const { pspReference, amount } = data
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return this.adyenPaymentApi.post("/capture", {
|
const captured = this.adyenPaymentApi.post("/capture", {
|
||||||
originalReference: pspReference,
|
originalReference: pspReference,
|
||||||
modificationAmount: amount,
|
modificationAmount: amount,
|
||||||
merchantAccount: this.options_.merchant_account,
|
merchantAccount: this.options_.merchant_account,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (
|
||||||
|
captured.data.pspReference &&
|
||||||
|
captured.data.response !== "[capture-received]"
|
||||||
|
) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.INVALID_ARGUMENT,
|
||||||
|
"Could not process capture"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// await this.orderService_.setMetadata(
|
||||||
|
// orderId,
|
||||||
|
// "adyen_capture_reference",
|
||||||
|
// captureData.data.pspReference
|
||||||
|
// )
|
||||||
|
|
||||||
|
return captured
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
@@ -603,19 +603,7 @@ class OrderService extends BaseService {
|
|||||||
provider_id
|
provider_id
|
||||||
)
|
)
|
||||||
|
|
||||||
const captureData = await paymentProvider.capturePayment(data)
|
await paymentProvider.capturePayment(data)
|
||||||
|
|
||||||
// If Adyen is used as payment provider, we need to check the
|
|
||||||
// validity of the capture request
|
|
||||||
if (
|
|
||||||
captureData.data.pspReference &&
|
|
||||||
captureData.data.response !== "[capture-received]"
|
|
||||||
) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.INVALID_ARGUMENT,
|
|
||||||
"Could not process capture"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.orderModel_
|
return this.orderModel_
|
||||||
.updateOne(
|
.updateOne(
|
||||||
|
|||||||
@@ -30,14 +30,6 @@ class OrderSubscriber {
|
|||||||
await this.customerService_.addAddress(order.customer_id, address)
|
await this.customerService_.addAddress(order.customer_id, address)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.eventBus_.subscribe("order.placed", async order => {
|
|
||||||
await this.cartService_.delete(order.cart_id).catch(err => {
|
|
||||||
if (err.type !== "not_found") {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
this.eventBus_.subscribe("order.placed", this.handleDiscounts)
|
this.eventBus_.subscribe("order.placed", this.handleDiscounts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user