feat(medusa): manage payment sessions from payment collection (#2402)
* feat: manage payment sessions from payment collection
This commit is contained in:
1 parent
a014e3623c
commit
c3f8297599
28 files changed
+2002
-1315
No files matched your search
@@ -21,21 +21,11 @@ export default async (req, res) => {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const body = req.body
|
||||
const authId = body.resource.id
|
||||
const auth = await paypalService.retrieveAuthorization(authId)
|
||||
|
||||
const order = await paypalService.retrieveOrderFromAuth(auth)
|
||||
|
||||
const purchaseUnit = order.purchase_units[0]
|
||||
const cartId = purchaseUnit.custom_id
|
||||
|
||||
if (!cartId) {
|
||||
res.sendStatus(200)
|
||||
return
|
||||
}
|
||||
function isPaymentCollection(id) {
|
||||
return id && id.startsWith("paycol")
|
||||
}
|
||||
|
||||
async function autorizeCart(req, cartId) {
|
||||
const manager = req.scope.resolve("manager")
|
||||
const cartService = req.scope.resolve("cartService")
|
||||
const swapService = req.scope.resolve("swapService")
|
||||
@@ -78,6 +68,42 @@ export default async (req, res) => {
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function autorizePaymentCollection(req, payColId) {
|
||||
const manager = req.scope.resolve("manager")
|
||||
const paymentCollectionService = req.scope.resolve(
|
||||
"paymentCollectonService"
|
||||
)
|
||||
|
||||
await manager.transaction(async (m) => {
|
||||
const payCol = await paymentCollectionService
|
||||
.withTransaction(m)
|
||||
.retrieve(payColId)
|
||||
})
|
||||
// TODO: complete authorization
|
||||
}
|
||||
|
||||
try {
|
||||
const body = req.body
|
||||
const authId = body.resource.id
|
||||
const auth = await paypalService.retrieveAuthorization(authId)
|
||||
|
||||
const order = await paypalService.retrieveOrderFromAuth(auth)
|
||||
|
||||
const purchaseUnit = order.purchase_units[0]
|
||||
const customId = purchaseUnit.custom_id
|
||||
|
||||
if (!customId) {
|
||||
res.sendStatus(200)
|
||||
return
|
||||
}
|
||||
|
||||
if (isPaymentCollection(customId)) {
|
||||
await autorizePaymentCollection(req, customId)
|
||||
} else {
|
||||
await autorizeCart(req, customId)
|
||||
}
|
||||
|
||||
res.sendStatus(200)
|
||||
} catch (err) {
|
||||
|
||||
@@ -118,6 +118,34 @@ class PayPalProviderService extends PaymentService {
|
||||
return { id: res.result.id }
|
||||
}
|
||||
|
||||
async createPaymentNew(paymentInput) {
|
||||
const { resource_id, currency_code, amount } = paymentInput
|
||||
|
||||
const request = new PayPal.orders.OrdersCreateRequest()
|
||||
request.requestBody({
|
||||
intent: "AUTHORIZE",
|
||||
application_context: {
|
||||
shipping_preference: "NO_SHIPPING",
|
||||
},
|
||||
purchase_units: [
|
||||
{
|
||||
custom_id: resource_id,
|
||||
amount: {
|
||||
currency_code: currency_code.toUpperCase(),
|
||||
value: roundToTwo(
|
||||
humanizeAmount(amount, currency_code),
|
||||
currency_code
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const res = await this.paypal_.execute(request)
|
||||
|
||||
return { id: res.result.id }
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a PayPal order.
|
||||
* @param {object} data - the data stored with the payment
|
||||
@@ -216,6 +244,35 @@ class PayPalProviderService extends PaymentService {
|
||||
}
|
||||
}
|
||||
|
||||
async updatePaymentNew(sessionData, paymentInput) {
|
||||
try {
|
||||
const { currency_code, amount } = paymentInput
|
||||
|
||||
const request = new PayPal.orders.OrdersPatchRequest(sessionData.id)
|
||||
request.requestBody([
|
||||
{
|
||||
op: "replace",
|
||||
path: "/purchase_units/@reference_id=='default'",
|
||||
value: {
|
||||
amount: {
|
||||
currency_code: currency_code.toUpperCase(),
|
||||
value: roundToTwo(
|
||||
humanizeAmount(amount, currency_code),
|
||||
currency_code
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
await this.paypal_.execute(request)
|
||||
|
||||
return sessionData
|
||||
} catch (error) {
|
||||
return this.createPaymentNew(paymentInput)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Not suported
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user