Adds klarna payment provider (#79)
This commit is contained in:
1 parent
448dc9ed8f
commit
5e83a7d38a
25 files changed
+6809
No files matched your search
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"@babel/plugin-proposal-class-properties",
|
||||||
|
"@babel/plugin-transform-instanceof",
|
||||||
|
"@babel/plugin-transform-classes"
|
||||||
|
],
|
||||||
|
"presets": ["@babel/preset-env"],
|
||||||
|
"env": {
|
||||||
|
"test": {
|
||||||
|
"plugins": ["@babel/plugin-transform-runtime"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["prettier"],
|
||||||
|
"extends": ["prettier"],
|
||||||
|
"rules": {
|
||||||
|
"prettier/prettier": "error",
|
||||||
|
"semi": "error",
|
||||||
|
"no-unused-expressions": "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/lib
|
||||||
|
node_modules
|
||||||
|
.DS_store
|
||||||
|
.env*
|
||||||
|
/*.js
|
||||||
|
!index.js
|
||||||
|
!jest.config.js
|
||||||
|
|
||||||
|
dist
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/lib
|
||||||
|
node_modules
|
||||||
|
.DS_store
|
||||||
|
.env*
|
||||||
|
/*.js
|
||||||
|
!index.js
|
||||||
|
yarn.lock
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"endOfLine": "lf",
|
||||||
|
"semi": false,
|
||||||
|
"singleQuote": false,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"trailingComma": "es5"
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// noop
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = {
|
||||||
|
testEnvironment: "node",
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
{
|
||||||
|
"name": "medusa-payment-klarna",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Klarna Payment provider for Medusa Commerce",
|
||||||
|
"main": "index.js",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/medusajs/medusa",
|
||||||
|
"directory": "packages/medusa-payment-klarna"
|
||||||
|
},
|
||||||
|
"author": "Oliver Juhl",
|
||||||
|
"license": "AGPL-3.0-or-later",
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/cli": "^7.7.5",
|
||||||
|
"@babel/core": "^7.7.5",
|
||||||
|
"@babel/node": "^7.7.4",
|
||||||
|
"@babel/plugin-proposal-class-properties": "^7.7.4",
|
||||||
|
"@babel/plugin-transform-instanceof": "^7.8.3",
|
||||||
|
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||||
|
"@babel/preset-env": "^7.7.5",
|
||||||
|
"@babel/register": "^7.7.4",
|
||||||
|
"@babel/runtime": "^7.9.6",
|
||||||
|
"client-sessions": "^0.8.0",
|
||||||
|
"cross-env": "^5.2.1",
|
||||||
|
"eslint": "^6.8.0",
|
||||||
|
"jest": "^25.5.2"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "babel src -d dist",
|
||||||
|
"prepare": "cross-env NODE_ENV=production npm run build",
|
||||||
|
"watch": "babel -w src --out-dir . --ignore **/__tests__",
|
||||||
|
"test": "jest"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/plugin-transform-classes": "^7.9.5",
|
||||||
|
"axios": "^0.19.2",
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"medusa-core-utils": "^0.3.0",
|
||||||
|
"medusa-interfaces": "^0.3.0",
|
||||||
|
"medusa-test-utils": "^0.3.0"
|
||||||
|
},
|
||||||
|
"gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc"
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
const mockAxios = jest.genMockFromModule("axios")
|
||||||
|
|
||||||
|
mockAxios.create = jest.fn(() => mockAxios)
|
||||||
|
|
||||||
|
export default mockAxios
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import { IdMap } from "medusa-test-utils"
|
||||||
|
|
||||||
|
export const carts = {
|
||||||
|
frCart: {
|
||||||
|
_id: IdMap.getId("fr-cart"),
|
||||||
|
email: "lebron@james.com",
|
||||||
|
title: "test",
|
||||||
|
region_id: IdMap.getId("region-france"),
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
_id: IdMap.getId("line"),
|
||||||
|
title: "merge line",
|
||||||
|
description: "This is a new line",
|
||||||
|
thumbnail: "test-img-yeah.com/thumb",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
unit_price: 8,
|
||||||
|
variant: {
|
||||||
|
_id: IdMap.getId("eur-8-us-10"),
|
||||||
|
},
|
||||||
|
product: {
|
||||||
|
_id: IdMap.getId("product"),
|
||||||
|
},
|
||||||
|
quantity: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
unit_price: 10,
|
||||||
|
variant: {
|
||||||
|
_id: IdMap.getId("eur-10-us-12"),
|
||||||
|
},
|
||||||
|
product: {
|
||||||
|
_id: IdMap.getId("product"),
|
||||||
|
},
|
||||||
|
quantity: 1,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
quantity: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: IdMap.getId("existingLine"),
|
||||||
|
title: "merge line",
|
||||||
|
description: "This is a new line",
|
||||||
|
thumbnail: "test-img-yeah.com/thumb",
|
||||||
|
content: {
|
||||||
|
unit_price: 10,
|
||||||
|
variant: {
|
||||||
|
_id: IdMap.getId("eur-10-us-12"),
|
||||||
|
},
|
||||||
|
product: {
|
||||||
|
_id: IdMap.getId("product"),
|
||||||
|
},
|
||||||
|
quantity: 1,
|
||||||
|
},
|
||||||
|
quantity: 10,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
shipping_methods: [
|
||||||
|
{
|
||||||
|
_id: IdMap.getId("freeShipping"),
|
||||||
|
profile_id: "default_profile",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
shipping_options: [
|
||||||
|
{
|
||||||
|
_id: IdMap.getId("freeShipping"),
|
||||||
|
profile_id: "default_profile",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
payment_sessions: [
|
||||||
|
{
|
||||||
|
provider_id: "stripe",
|
||||||
|
data: {
|
||||||
|
id: "pi_123456789",
|
||||||
|
customer: IdMap.getId("not-lebron"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
payment_method: {
|
||||||
|
provider_id: "stripe",
|
||||||
|
data: {
|
||||||
|
id: "pi_123456789",
|
||||||
|
customer: IdMap.getId("not-lebron"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
shipping_address: {},
|
||||||
|
billing_address: {},
|
||||||
|
discounts: [
|
||||||
|
{
|
||||||
|
code: "MEDUSA_FREE",
|
||||||
|
discount_rule: {
|
||||||
|
type: "percent",
|
||||||
|
value: 20,
|
||||||
|
allocation: "item",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
customer_id: IdMap.getId("lebron"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CartServiceMock = {
|
||||||
|
retrieve: jest.fn().mockImplementation((cartId) => {
|
||||||
|
if (cartId === IdMap.getId("fr-cart")) {
|
||||||
|
return Promise.resolve(carts.frCart)
|
||||||
|
}
|
||||||
|
return Promise.resolve(undefined)
|
||||||
|
}),
|
||||||
|
updatePaymentSession: jest
|
||||||
|
.fn()
|
||||||
|
.mockImplementation((cartId, stripe, paymentIntent) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
const mock = jest.fn().mockImplementation(() => {
|
||||||
|
return CartServiceMock
|
||||||
|
})
|
||||||
|
|
||||||
|
export default mock
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { IdMap } from "medusa-test-utils"
|
||||||
|
|
||||||
|
export const RegionServiceMock = {
|
||||||
|
retrieve: jest.fn().mockImplementation((regionId) => {
|
||||||
|
return Promise.resolve({
|
||||||
|
_id: IdMap.getId("testRegion"),
|
||||||
|
name: "Test Region",
|
||||||
|
countries: ["DK", "US", "DE"],
|
||||||
|
tax_rate: 0.25,
|
||||||
|
payment_providers: ["default_provider", "unregistered"],
|
||||||
|
fulfillment_providers: ["test_shipper"],
|
||||||
|
currency_code: "usd",
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
const mock = jest.fn().mockImplementation(() => {
|
||||||
|
return RegionServiceMock
|
||||||
|
})
|
||||||
|
|
||||||
|
export default mock
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
export const TotalsServiceMock = {
|
||||||
|
getTotal: jest.fn(),
|
||||||
|
getTaxTotal: jest.fn(),
|
||||||
|
getAllocationItemDiscounts: jest.fn(),
|
||||||
|
}
|
||||||
|
|
||||||
|
const mock = jest.fn().mockImplementation(() => {
|
||||||
|
return TotalsServiceMock
|
||||||
|
})
|
||||||
|
|
||||||
|
export default mock
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { Router } from "express"
|
||||||
|
import hooks from "./routes/hooks"
|
||||||
|
|
||||||
|
export default (container) => {
|
||||||
|
const app = Router()
|
||||||
|
|
||||||
|
hooks(app)
|
||||||
|
|
||||||
|
return app
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export default (fn) => (...args) => fn(...args).catch(args[2])
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { default as wrap } from "./await-middleware"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
wrap,
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
export default async (req, res) => {
|
||||||
|
// In Medusa, we store the cart id in merchant_data
|
||||||
|
const { shipping_address, merchant_data } = req.body
|
||||||
|
|
||||||
|
try {
|
||||||
|
const cartService = req.resolve("cartService")
|
||||||
|
const klarnaProviderService = req.resolve("pp_klarna")
|
||||||
|
|
||||||
|
const cart = await cartService.retrieve(merchant_data)
|
||||||
|
|
||||||
|
if (shipping_address) {
|
||||||
|
const updatedAddress = {
|
||||||
|
email: shipping_address.email,
|
||||||
|
street_address: shipping_address.address_1,
|
||||||
|
street_address2: shipping_address.address_2,
|
||||||
|
postal_code: shipping_address.postal_code,
|
||||||
|
city: shipping_address.city,
|
||||||
|
country: shipping_address.country_code,
|
||||||
|
}
|
||||||
|
|
||||||
|
await cartService.update(cart._id, {
|
||||||
|
email: shipping_address.email,
|
||||||
|
shipping_address: updatedAddress,
|
||||||
|
billing_address: updatedAddress,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Fetch and return updated Klarna order
|
||||||
|
const updatedCart = await cartService.retrieve(cart._id)
|
||||||
|
const order = klarnaProviderService.cartToKlarnaOrder(updatedCart)
|
||||||
|
res.json(order)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
res.sendStatus(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { Router } from "express"
|
||||||
|
import middlewares from "../../middlewares"
|
||||||
|
|
||||||
|
const route = Router()
|
||||||
|
|
||||||
|
export default (app) => {
|
||||||
|
app.use("/hooks", route)
|
||||||
|
|
||||||
|
route.post("/shipping", middlewares.wrap(require("./shippping").default))
|
||||||
|
route.post("/address", middlewares.wrap(require("./address").default))
|
||||||
|
route.post("/push", middlewares.wrap(require("./push").default))
|
||||||
|
|
||||||
|
return app
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
export default async (req, res) => {
|
||||||
|
const { klarna_order_id } = req.query
|
||||||
|
|
||||||
|
try {
|
||||||
|
const orderService = req.resolve("orderService")
|
||||||
|
const klarnaProviderService = req.resolve("pp_klarna")
|
||||||
|
|
||||||
|
const klarnaOrder = await klarnaProviderService.retrieveCompletedOrder(
|
||||||
|
klarna_order_id
|
||||||
|
)
|
||||||
|
|
||||||
|
const cartId = klarnaOrder.merchant_data
|
||||||
|
const order = await orderService.list({ cart_id: cartId })[0]
|
||||||
|
|
||||||
|
await klarnaProviderService.acknowledgeOrder(klarnaOrder.id, order._id)
|
||||||
|
res.sendStatus(200)
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
export default async (req, res) => {
|
||||||
|
// In Medusa, we store the cart id in merchant_data
|
||||||
|
const { merchant_data, selected_shipping_option } = req.body
|
||||||
|
|
||||||
|
try {
|
||||||
|
const cartService = req.resolve("cartService")
|
||||||
|
const klarnaProviderService = req.resolve("pp_klarna")
|
||||||
|
|
||||||
|
const cart = await cartService.retrieve(merchant_data)
|
||||||
|
const updatedMethod = cart.shipping_options.find(
|
||||||
|
(so) => so._id === selected_shipping_option.id
|
||||||
|
)
|
||||||
|
|
||||||
|
if (updatedMethod) {
|
||||||
|
await cartService.update(cart._id, {
|
||||||
|
shipping_methods: [updatedMethod],
|
||||||
|
})
|
||||||
|
|
||||||
|
// Fetch and return updated Klarna order
|
||||||
|
const updatedCart = await cartService.retrieve(cart._id)
|
||||||
|
const order = klarnaProviderService.cartToKlarnaOrder(updatedCart)
|
||||||
|
res.json(order)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
res.sendStatus(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { IdMap } from "medusa-test-utils"
|
||||||
|
|
||||||
|
export const KlarnaProviderServiceMock = {
|
||||||
|
retrievePayment: jest.fn().mockImplementation((cart) => {
|
||||||
|
if (cart._id === IdMap.getId("fr-cart")) {
|
||||||
|
return Promise.resolve({
|
||||||
|
order_id: "123456789",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return Promise.resolve(undefined)
|
||||||
|
}),
|
||||||
|
cancelPayment: jest.fn().mockImplementation((cart) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
}),
|
||||||
|
updatePayment: jest.fn().mockImplementation((cart) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
}),
|
||||||
|
capturePayment: jest.fn().mockImplementation((cart) => {
|
||||||
|
if (cart._id === IdMap.getId("fr-cart")) {
|
||||||
|
return Promise.resolve({
|
||||||
|
id: "123456789",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return Promise.resolve(undefined)
|
||||||
|
}),
|
||||||
|
createPayment: jest.fn().mockImplementation((cart) => {
|
||||||
|
if (cart._id === IdMap.getId("fr-cart")) {
|
||||||
|
return Promise.resolve({
|
||||||
|
id: "123456789",
|
||||||
|
order_amount: 100,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return Promise.resolve(undefined)
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
const mock = jest.fn().mockImplementation(() => {
|
||||||
|
return KlarnaProviderServiceMock
|
||||||
|
})
|
||||||
|
|
||||||
|
export default mock
|
||||||
@@ -0,0 +1,327 @@
|
|||||||
|
import KlarnaProviderService from "../klarna-provider"
|
||||||
|
import mockAxios from "../../__mocks__/axios"
|
||||||
|
import { carts } from "../../__mocks__/cart"
|
||||||
|
import { TotalsServiceMock } from "../../__mocks__/totals"
|
||||||
|
import { RegionServiceMock } from "../../__mocks__/region"
|
||||||
|
|
||||||
|
describe("KlarnaProviderService", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockAxios.mockClear()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("createPayment", () => {
|
||||||
|
let result
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
regionService: RegionServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
merchant_urls: {
|
||||||
|
terms: "terms",
|
||||||
|
checkout: "checkout",
|
||||||
|
confirmation: "confirmation",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("creates Klarna order", async () => {
|
||||||
|
mockAxios.post.mockImplementation(() => {
|
||||||
|
return Promise.resolve({
|
||||||
|
order_id: "123456789",
|
||||||
|
order_amount: 100,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.createPayment(carts.frCart)
|
||||||
|
expect(result).toEqual({
|
||||||
|
order_id: "123456789",
|
||||||
|
order_amount: 100,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("retrievePayment", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns Klarna order", async () => {
|
||||||
|
mockAxios.get.mockImplementation((data) => {
|
||||||
|
return Promise.resolve({
|
||||||
|
order_id: "123456789",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.retrievePayment({
|
||||||
|
payment_method: {
|
||||||
|
data: {
|
||||||
|
id: "123456789",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
order_id: "123456789",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("retrieveCompletedOrder", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns completed Klarna order", async () => {
|
||||||
|
mockAxios.get.mockImplementation((data) => {
|
||||||
|
return Promise.resolve({
|
||||||
|
order_id: "123456789",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.retrieveCompletedOrder({
|
||||||
|
payment_method: {
|
||||||
|
data: {
|
||||||
|
id: "123456789",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
order_id: "123456789",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("updatePayment", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns updated Klarna order", async () => {
|
||||||
|
mockAxios.post.mockImplementation((data) => {
|
||||||
|
return Promise.resolve({
|
||||||
|
order_id: "123456789",
|
||||||
|
order_amount: 1000,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.updatePayment(
|
||||||
|
{
|
||||||
|
payment_method: {
|
||||||
|
data: {
|
||||||
|
id: "123456789",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
order_amount: 1000,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
order_id: "123456789",
|
||||||
|
order_amount: 1000,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("cancelPayment", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns order id", async () => {
|
||||||
|
mockAxios.post.mockImplementation((data) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.cancelPayment({ id: "123456789" })
|
||||||
|
|
||||||
|
expect(result).toEqual("123456789")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("acknowledgeOrder", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns order id", async () => {
|
||||||
|
mockAxios.post.mockImplementation((data) => {
|
||||||
|
return Promise.resolve({})
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.acknowledgeOrder("123456789")
|
||||||
|
|
||||||
|
expect(result).toEqual("123456789")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("addOrderToKlarnaOrder", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns order id", async () => {
|
||||||
|
mockAxios.post.mockImplementation((data) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.addOrderToKlarnaOrder(
|
||||||
|
"123456789",
|
||||||
|
"order123456789"
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result).toEqual("123456789")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("capturePayment", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns order id", async () => {
|
||||||
|
mockAxios.get.mockImplementation((data) => {
|
||||||
|
return Promise.resolve({
|
||||||
|
order: { order_amount: 1000 },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
mockAxios.post.mockImplementation((data) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.capturePayment({
|
||||||
|
id: "123456789",
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result).toEqual("123456789")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("refundPayment", () => {
|
||||||
|
let result
|
||||||
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const klarnaProviderService = new KlarnaProviderService(
|
||||||
|
{
|
||||||
|
totalsService: TotalsServiceMock,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "medusajs/tests",
|
||||||
|
user: "lebronjames",
|
||||||
|
password: "123456789",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it("returns order id", async () => {
|
||||||
|
mockAxios.post.mockImplementation((data) => {
|
||||||
|
return Promise.resolve()
|
||||||
|
})
|
||||||
|
|
||||||
|
result = await klarnaProviderService.capturePayment(
|
||||||
|
{
|
||||||
|
id: "123456789",
|
||||||
|
},
|
||||||
|
1000
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result).toEqual("123456789")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,351 @@
|
|||||||
|
import _ from "lodash"
|
||||||
|
import axios from "axios"
|
||||||
|
import { PaymentService } from "medusa-interfaces"
|
||||||
|
|
||||||
|
class KlarnaProviderService extends PaymentService {
|
||||||
|
static identifier = "klarna"
|
||||||
|
|
||||||
|
constructor({ totalsService, regionService }, options) {
|
||||||
|
super()
|
||||||
|
|
||||||
|
this.options_ = options
|
||||||
|
|
||||||
|
this.klarna_ = axios.create({
|
||||||
|
baseURL: options.url,
|
||||||
|
auth: {
|
||||||
|
username: options.user,
|
||||||
|
password: options.password,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
this.klarnaOrderUrl_ = "/checkout/v3/orders"
|
||||||
|
|
||||||
|
this.klarnaOrderManagementUrl_ = "/ordermanagement/v1/orders"
|
||||||
|
|
||||||
|
this.backendUrl_ = process.env.BACKEND_URL || ""
|
||||||
|
|
||||||
|
this.totalsService_ = totalsService
|
||||||
|
|
||||||
|
this.regionService_ = regionService
|
||||||
|
}
|
||||||
|
|
||||||
|
async lineItemsToOrderLines_(cart, taxRate) {
|
||||||
|
let order_lines = []
|
||||||
|
// Find the discount, that is not free shipping
|
||||||
|
const discount = cart.discounts.find(
|
||||||
|
({ discount_rule }) => discount_rule.type !== "free_shipping"
|
||||||
|
)
|
||||||
|
// If the discount has an item specific allocation method,
|
||||||
|
// we need to fetch the discount for each item
|
||||||
|
const itemDiscounts = await this.totalsService_.getAllocationItemDiscounts(
|
||||||
|
discount,
|
||||||
|
cart
|
||||||
|
)
|
||||||
|
|
||||||
|
cart.items.forEach((item) => {
|
||||||
|
// For bundles, we create an order line for each item in the bundle
|
||||||
|
if (Array.isArray(item.content)) {
|
||||||
|
item.content.forEach((c) => {
|
||||||
|
const total_amount = c.unit_price * c.quantity * (taxRate + 1)
|
||||||
|
const total_tax_amount = total_amount * taxRate
|
||||||
|
|
||||||
|
order_lines.push({
|
||||||
|
unit_price: c.unit_price,
|
||||||
|
// Medusa does not allow discount on bundles
|
||||||
|
total_discount_amount: 0,
|
||||||
|
quantity: c.quantity,
|
||||||
|
tax_rate: taxRate,
|
||||||
|
total_amount,
|
||||||
|
total_tax_amount,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
const total_amount =
|
||||||
|
item.content.unit_price * item.content.quantity * (taxRate + 1)
|
||||||
|
const total_tax_amount = total_amount * taxRate
|
||||||
|
|
||||||
|
// Find the discount for current item and default to 0
|
||||||
|
const itemDiscount =
|
||||||
|
(itemDiscounts &&
|
||||||
|
itemDiscounts.find((el) => el.lineItem._id === item._id)) ||
|
||||||
|
0
|
||||||
|
|
||||||
|
// Withdraw discount from the total item amount
|
||||||
|
const total_discount_amount =
|
||||||
|
total_amount - itemDiscount * (taxRate + 1)
|
||||||
|
|
||||||
|
order_lines.push({
|
||||||
|
unit_price: item.content.unit_price,
|
||||||
|
quantity: item.content.quantity,
|
||||||
|
total_discount_amount,
|
||||||
|
tax_rate: taxRate,
|
||||||
|
total_amount,
|
||||||
|
total_tax_amount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async cartToKlarnaOrder(cart) {
|
||||||
|
let order = {
|
||||||
|
// Cart id is stored, such that we can use it for hooks
|
||||||
|
merchant_data: cart._id,
|
||||||
|
// TODO: Investigate if other locales are needed
|
||||||
|
locale: "en-US",
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tax_rate, currency_code } = await this.regionService_.retrieve(
|
||||||
|
cart.region_id
|
||||||
|
)
|
||||||
|
|
||||||
|
order.order_lines = this.lineItemsToOrderLines_(cart, tax_rate)
|
||||||
|
|
||||||
|
if (cart.billing_address) {
|
||||||
|
order.billing_address = {
|
||||||
|
email: cart.email,
|
||||||
|
street_address: cart.billing_address.address_1,
|
||||||
|
street_address2: cart.billing_address.address_2,
|
||||||
|
postal_code: cart.billing_address.postal_code,
|
||||||
|
city: cart.billing_address.city,
|
||||||
|
country: cart.billing_address.country_code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Check if country matches ISO
|
||||||
|
if (cart.billing_address.country) {
|
||||||
|
order.purchase_country = cart.billing_address.country
|
||||||
|
} else {
|
||||||
|
// Defaults to Sweden
|
||||||
|
order.purchase_country = "SE"
|
||||||
|
}
|
||||||
|
|
||||||
|
order.order_amount = this.totalsService_.getTotal(cart)
|
||||||
|
order.order_tax_amount = this.totalsService_.getTaxTotal(cart)
|
||||||
|
// TODO: Check if currency matches ISO
|
||||||
|
order.purchase_currency = currency_code
|
||||||
|
|
||||||
|
order.merchant_urls = {
|
||||||
|
terms: this.options_.merchant_urls.terms,
|
||||||
|
checkout: this.options_.merchant_urls.checkout,
|
||||||
|
confirmation: this.options_.merchant_urls.confirmation,
|
||||||
|
push: `${this.backendUrl_}/klarna/hooks/push`,
|
||||||
|
shipping_option_update: `${this.backendUrl_}/klarna/hooks/shipping`,
|
||||||
|
address_update: `${this.backendUrl_}/klarna/hooks/address`,
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the cart does not have shipping methods yet, preselect one from
|
||||||
|
// shipping_options and set the selected shipping method
|
||||||
|
if (!cart.shipping_methods) {
|
||||||
|
const shipping_method = cart.shipping_options[0]
|
||||||
|
order.selected_shipping_option = {
|
||||||
|
id: shipping_method._id,
|
||||||
|
// TODO: Add shipping method name
|
||||||
|
name: shipping_method.provider_id,
|
||||||
|
price: shipping_method.price * (1 + tax_rate) * 100,
|
||||||
|
tax_amount: shipping_method.price * tax_rate * 100,
|
||||||
|
// Medusa tax rate of e.g. 0.25 (25%) needs to be 2500 in Klarna
|
||||||
|
tax_rate: tax_rate * 10000,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the cart does have shipping methods, set the selected shipping method
|
||||||
|
if (cart.shipping_methods) {
|
||||||
|
const shipping_method = cart.shipping_methods[0]
|
||||||
|
order.selected_shipping_option = {
|
||||||
|
id: shipping_method._id,
|
||||||
|
name: shipping_method.provider_id,
|
||||||
|
price: shipping_method.price * (1 + tax_rate) * 100,
|
||||||
|
tax_amount: shipping_method.price * tax_rate * 100,
|
||||||
|
tax_rate: tax_rate * 10000,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
order.shipping_options = cart.shipping_options.map((so) => ({
|
||||||
|
id: so._id,
|
||||||
|
name: so.provider_id,
|
||||||
|
price: so.price * (1 + tax_rate) * 100,
|
||||||
|
tax_amount: so.price * tax_rate * 100,
|
||||||
|
tax_rate: tax_rate * 10000,
|
||||||
|
preselected:
|
||||||
|
cart.shipping_methods[0] &&
|
||||||
|
`${cart.shipping_methods[0].provider_id}` === `${so.provider_id}`,
|
||||||
|
}))
|
||||||
|
|
||||||
|
return order
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Status for Klarna order.
|
||||||
|
* @param {Object} paymentData - payment method data from cart
|
||||||
|
* @returns {string} the status of the Klarna order
|
||||||
|
*/
|
||||||
|
async getStatus(paymentData) {
|
||||||
|
try {
|
||||||
|
const { id } = paymentData
|
||||||
|
const order = await this.klarna_.get(`${this.klarnaOrderUrl_}/${id}`)
|
||||||
|
// TODO: Klarna docs does not provide a list of statues, so we need to
|
||||||
|
// play around our selves to figure it out
|
||||||
|
let status = "initial"
|
||||||
|
return status
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates Stripe PaymentIntent.
|
||||||
|
* @param {string} cart - the cart to create a payment for
|
||||||
|
* @param {number} amount - the amount to create a payment for
|
||||||
|
* @returns {string} id of payment intent
|
||||||
|
*/
|
||||||
|
async createPayment(cart) {
|
||||||
|
try {
|
||||||
|
const order = await this.cartToKlarnaOrder(cart)
|
||||||
|
return this.klarna_.post(this.klarnaOrderUrl__, order)
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves Klarna Order.
|
||||||
|
* @param {string} cart - the cart to retrieve order for
|
||||||
|
* @returns {Object} Klarna order
|
||||||
|
*/
|
||||||
|
async retrievePayment(cart) {
|
||||||
|
try {
|
||||||
|
const { data } = cart.payment_method
|
||||||
|
return this.klarna_.get(`${this.klarnaOrderUrl_}/${data.id}`)
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves completed Klarna Order.
|
||||||
|
* @param {string} klarnaOrderId - id of the order to retrieve
|
||||||
|
* @returns {Object} Klarna order
|
||||||
|
*/
|
||||||
|
async retrieveCompletedOrder(klarnaOrderId) {
|
||||||
|
try {
|
||||||
|
return this.klarna_.get(
|
||||||
|
`${this.klarnaOrderManagementUrl_}/${klarnaOrderId}`
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Acknowledges a Klarna order as part of the order completion process
|
||||||
|
* @param {string} klarnaOrderId - id of the order to acknowledge
|
||||||
|
* @returns {string} id of acknowledged order
|
||||||
|
*/
|
||||||
|
async acknowledgeOrder(klarnaOrderId) {
|
||||||
|
try {
|
||||||
|
await this.klarna_.post(
|
||||||
|
`${this.klarnaOrderManagementUrl_}/${klarnaOrderId}/acknowledge`,
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
return klarnaOrderId
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the id of the Medusa order to the Klarna Order to create a relation
|
||||||
|
* @param {string} klarnaOrderId - id of the klarna order
|
||||||
|
* @param {string} orderId - id of the Medusa order
|
||||||
|
* @returns {string} id of updated order
|
||||||
|
*/
|
||||||
|
async addOrderToKlarnaOrder(klarnaOrderId, orderId) {
|
||||||
|
try {
|
||||||
|
await this.klarna_.post(
|
||||||
|
`${this.klarnaOrderManagementUrl_}/${klarnaOrderId}/merchant-references`,
|
||||||
|
{
|
||||||
|
merchant_reference1: orderId,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return klarnaOrderId
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates Klarna order.
|
||||||
|
* @param {string} order - the order to update
|
||||||
|
* @param {Object} data - the update object
|
||||||
|
* @returns {Object} updated order
|
||||||
|
*/
|
||||||
|
async updatePayment(order, update) {
|
||||||
|
try {
|
||||||
|
const { data } = order.payment_method
|
||||||
|
return this.klarna_.post(`${this.klarnaOrderUrl_}/${data.id}`, update)
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Captures Klarna order.
|
||||||
|
* @param {Object} paymentData - payment method data from cart
|
||||||
|
* @returns {string} id of captured order
|
||||||
|
*/
|
||||||
|
async capturePayment(paymentData) {
|
||||||
|
try {
|
||||||
|
const { id } = paymentData
|
||||||
|
const orderData = await this.klarna_.get(`${this.klarnaOrderUrl_}/${id}`)
|
||||||
|
const { order_amount } = orderData.order
|
||||||
|
|
||||||
|
await this.klarna_.post(
|
||||||
|
`${this.klarnaOrderManagementUrl_}/${id}/captures`,
|
||||||
|
{
|
||||||
|
captured_amount: order_amount,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return id
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refunds payment for Klarna Order.
|
||||||
|
* @param {Object} paymentData - payment method data from cart
|
||||||
|
* @returns {string} id of refunded order
|
||||||
|
*/
|
||||||
|
async refundPayment(paymentData, amount) {
|
||||||
|
try {
|
||||||
|
const { id } = paymentData
|
||||||
|
await this.klarna_.post(
|
||||||
|
`${this.klarnaOrderManagementUrl_}/${id}/refunds`,
|
||||||
|
{
|
||||||
|
refunded_amount: amount,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return id
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels payment for Klarna Order.
|
||||||
|
* @param {Object} paymentData - payment method data from cart
|
||||||
|
* @returns {string} id of cancelled order
|
||||||
|
*/
|
||||||
|
async cancelPayment(paymentData) {
|
||||||
|
try {
|
||||||
|
const { id } = paymentData
|
||||||
|
await this.klarna_.post(`${this.klarnaOrderUrl_}/${id}/cancel`)
|
||||||
|
return id
|
||||||
|
} catch (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default KlarnaProviderService
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -18,6 +18,7 @@ class OrderModel extends BaseModel {
|
|||||||
// awaiting, captured, refunded
|
// awaiting, captured, refunded
|
||||||
payment_status: { type: String, default: "awaiting" },
|
payment_status: { type: String, default: "awaiting" },
|
||||||
email: { type: String, required: true },
|
email: { type: String, required: true },
|
||||||
|
cart_id: { type: String },
|
||||||
billing_address: { type: AddressSchema, required: true },
|
billing_address: { type: AddressSchema, required: true },
|
||||||
shipping_address: { type: AddressSchema, required: true },
|
shipping_address: { type: AddressSchema, required: true },
|
||||||
items: { type: [LineItemSchema], required: true },
|
items: { type: [LineItemSchema], required: true },
|
||||||
|
|||||||
@@ -123,6 +123,14 @@ class OrderService extends BaseService {
|
|||||||
return updatedMethods
|
return updatedMethods
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Object} selector - the query object for find
|
||||||
|
* @return {Promise} the result of the find operation
|
||||||
|
*/
|
||||||
|
list(selector) {
|
||||||
|
return this.orderModel_.find(selector)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an order by id.
|
* Gets an order by id.
|
||||||
* @param {string} orderId - id of order to retrieve
|
* @param {string} orderId - id of order to retrieve
|
||||||
|
|||||||
Reference in new issue
Block a user