Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:medusajs/medusa into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Nov 2, 2022
2 parents ee8a71c + c3f8297 commit 0b0d50b
Show file tree
Hide file tree
Showing 28 changed files with 2,000 additions and 1,313 deletions.
40 changes: 24 additions & 16 deletions docs-util/fixture-gen/src/services/test-pay.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,67 @@
import { AbstractPaymentService } from "@medusajs/medusa";
import { AbstractPaymentService } from "@medusajs/medusa"

class TestPayService extends AbstractPaymentService {
static identifier = "test-pay";
static identifier = "test-pay"

constructor(_) {
super(_);
super(_)
}

async getStatus(paymentData) {
return "authorized";
return "authorized"
}

async retrieveSavedMethods(customer) {
return Promise.resolve([]);
return Promise.resolve([])
}

async createPayment() {
return {};
return {}
}

async createPaymentNew() {
return {}
}

async retrievePayment(data) {
return {};
return {}
}

async getPaymentData(sessionData) {
return {};
return {}
}

async authorizePayment(sessionData, context = {}) {
return {};
return {}
}

async updatePaymentData(sessionData, update) {
return {};
return {}
}

async updatePayment(sessionData, cart) {
return {};
return {}
}

async updatePaymentNew(sessionData, paymentInput) {
return {}
}

async deletePayment(payment) {
return {};
return {}
}

async capturePayment(payment) {
return {};
return {}
}

async refundPayment(payment, amountToRefund) {
return {};
return {}
}

async cancelPayment(payment) {
return {};
return {}
}
}

export default TestPayService;
export default TestPayService
8 changes: 8 additions & 0 deletions integration-tests/api/src/services/test-pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class TestPayService extends AbstractPaymentService {
return data
}

async createPaymentNew(inputData) {
return inputData
}

async retrievePayment(data) {
return {}
}
Expand All @@ -59,6 +63,10 @@ class TestPayService extends AbstractPaymentService {
return {}
}

async updatePaymentNew(sessionData) {
return sessionData
}

async deletePayment(payment) {
return {}
}
Expand Down
9 changes: 9 additions & 0 deletions integration-tests/development/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ const bootstrapApp = async () => {
}

const app = express()
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", req.headers.origin)
res.header("Access-Control-Allow-Methods", "*")
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
)
next()
})

const dir = path.resolve(
path.join(__dirname, "../../packages/medusa/src/loaders")
Expand Down
8 changes: 8 additions & 0 deletions packages/medusa-payment-adyen/src/services/adyen.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class AdyenService extends BaseService {
return { cart_id: cart.id }
}

async createPaymentNew(paymentInput) {
return { resource_id: paymentInput.resource_id }
}

/**
* Retrieves Adyen payment. This is not supported by adyen, so we simply
* return the current payment method data
Expand Down Expand Up @@ -322,6 +326,10 @@ class AdyenService extends BaseService {
return paymentData
}

async updatePaymentNew(paymentData, details) {
return paymentData
}

/**
* Additional details
* @param {object} paymentData - payment data
Expand Down
20 changes: 15 additions & 5 deletions packages/medusa-payment-klarna/src/api/routes/hooks/push.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { MedusaError } from "medusa-core-utils"

export default async (req, res) => {
const { klarna_order_id } = req.query

function isPaymentCollection(id) {
return id && id.startsWith("paycol")
}

try {
const orderService = req.scope.resolve("orderService")
const klarnaProviderService = req.scope.resolve("pp_klarna")
Expand All @@ -11,10 +13,18 @@ export default async (req, res) => {
klarna_order_id
)

const cartId = klarnaOrder.merchant_data
const order = await orderService.retrieveByCartId(cartId)
const resourceId = klarnaOrder.merchant_data

if (isPaymentCollection(resourceId)) {
await klarnaProviderService.acknowledgeOrder(klarnaOrder.order_id)
} else {
const order = await orderService.retrieveByCartId(resourceId)

await klarnaProviderService.acknowledgeOrder(klarnaOrder.order_id, order.id)
await klarnaProviderService.acknowledgeOrder(
klarnaOrder.order_id,
order.id
)
}

res.sendStatus(200)
} catch (error) {
Expand Down
127 changes: 119 additions & 8 deletions packages/medusa-payment-klarna/src/services/klarna-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,84 @@ class KlarnaProviderService extends PaymentService {
return order
}

validateKlarnaOrderUrls(property) {
const required = ["terms", "checkout", "confirmation"]

const isMissing = required.some((prop) => !this.options_[property]?.[prop])

if (isMissing) {
throw new Error(
`options.${property} is required to create a Klarna Order.\n` +
`medusa-config.js file has to contain ${property} { ${required.join(
", "
)}}`
)
}
}

replaceStringWithPropertyValue(string, obj) {
const keys = Object.keys(obj)
for (const key of keys) {
if (string.includes(`{${key}}`)) {
string = string.replace(`{${key}}`, obj[key])
}
}
return string
}

async paymentInputToKlarnaOrder(paymentInput) {
if (paymentInput.cart) {
this.validateKlarnaOrderUrls("merchant_urls")
return this.cartToKlarnaOrder(paymentInput.cart)
}

this.validateKlarnaOrderUrls("payment_collection_urls")

let order = {
// Custom id is stored, such that we can use it for hooks
merchant_data: paymentInput.resource_id,
locale: "en-US",
}

const { currency_code, amount } = paymentInput

order.order_lines = [
{
name: "Payment Collection",
quantity: 1,
unit_price: amount,
tax_rate: 0,
total_amount: amount,
total_tax_amount: 0,
},
]

// Defaults to Sweden
order.purchase_country = "SE"

order.order_amount = amount
order.order_tax_amount = 0
order.purchase_currency = currency_code.toUpperCase()

order.merchant_urls = {
terms: this.replaceStringWithPropertyValue(
this.options_.payment_collection_urls.terms,
paymentInput
),
checkout: this.replaceStringWithPropertyValue(
this.options_.payment_collection_urls.checkout,
paymentInput
),
confirmation: this.replaceStringWithPropertyValue(
this.options_.payment_collection_urls.confirmation,
paymentInput
),
push: `${this.backendUrl_}/klarna/push?klarna_order_id={checkout.order.id}`,
}

return order
}

/**
* Status for Klarna order.
* @param {Object} paymentData - payment method data from cart
Expand All @@ -251,7 +329,7 @@ class KlarnaProviderService extends PaymentService {
}

/**
* Creates Stripe PaymentIntent.
* Creates Klarna 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
Expand All @@ -271,6 +349,21 @@ class KlarnaProviderService extends PaymentService {
}
}

async createPaymentNew(paymentInput) {
try {
const order = await this.paymentInputToKlarnaOrder(paymentInput)

const klarnaPayment = await this.klarna_
.post(this.klarnaOrderUrl_, order)
.then(({ data }) => data)

return klarnaPayment
} catch (error) {
this.logger_.error(error)
throw error
}
}

/**
* Retrieves Klarna Order.
* @param {string} cart - the cart to retrieve order for
Expand Down Expand Up @@ -338,18 +431,20 @@ class KlarnaProviderService extends PaymentService {
* @param {string} klarnaOrderId - id of the order to acknowledge
* @returns {string} id of acknowledged order
*/
async acknowledgeOrder(klarnaOrderId, orderId) {
async acknowledgeOrder(klarnaOrderId, orderId = null) {
try {
await this.klarna_.post(
`${this.klarnaOrderManagementUrl_}/${klarnaOrderId}/acknowledge`
)

await this.klarna_.patch(
`${this.klarnaOrderManagementUrl_}/${klarnaOrderId}/merchant-references`,
{
merchant_reference1: orderId,
}
)
if (orderId !== null) {
await this.klarna_.patch(
`${this.klarnaOrderManagementUrl_}/${klarnaOrderId}/merchant-references`,
{
merchant_reference1: orderId,
}
)
}

return klarnaOrderId
} catch (error) {
Expand Down Expand Up @@ -408,6 +503,22 @@ class KlarnaProviderService extends PaymentService {
return paymentData
}

async updatePaymentNew(paymentData, paymentInput) {
if (paymentInput.amount !== paymentData.order_amount) {
const order = await this.paymentInputToKlarnaOrder(paymentInput)
return this.klarna_
.post(`${this.klarnaOrderUrl_}/${paymentData.order_id}`, order)
.then(({ data }) => data)
.catch(async (_) => {
return this.klarna_
.post(this.klarnaOrderUrl_, order)
.then(({ data }) => data)
})
}

return paymentData
}

/**
* Captures Klarna order.
* @param {Object} paymentData - payment method data from cart
Expand Down
8 changes: 8 additions & 0 deletions packages/medusa-payment-manual/src/services/manual-payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class ManualPaymentService extends PaymentService {
return { status: "pending" }
}

async createPaymentNew() {
return { status: "pending" }
}

/**
* Retrieves payment
* @param {object} data - the data of the payment to retrieve
Expand All @@ -52,6 +56,10 @@ class ManualPaymentService extends PaymentService {
return sessionData.data
}

async updatePaymentNew(sessionData) {
return sessionData.data
}

/**
.
* @param {object} sessionData - payment session data.
Expand Down
Loading

0 comments on commit 0b0d50b

Please sign in to comment.