Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Feb 15, 2023
1 parent 6e2a71b commit 5aef163
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/medusa-payment-stripe-processor/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "medusa-payment-stripe-processor",
"name": "@medusajs/medusa-payment-stripe-processor",
"version": "0.0.1",
"description": "Stripe Payment provider for Meduas Commerce",
"main": "dist/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ abstract class StripeBase extends AbstractPaymentProcessor {
return options
}

async getPaymentStatus(paymentId: string): Promise<PaymentSessionStatus> {
const paymentIntent = await this.stripe_.paymentIntents.retrieve(paymentId)
async getPaymentStatus(
paymentSessionData: Record<string, unknown>
): Promise<PaymentSessionStatus> {
const id = paymentSessionData.id as string
const paymentIntent = await this.stripe_.paymentIntents.retrieve(id)

switch (paymentIntent.status) {
case "requires_payment_method":
Expand Down Expand Up @@ -148,20 +151,28 @@ abstract class StripeBase extends AbstractPaymentProcessor {
}

async authorizePayment(
context: PaymentProcessorContext
): Promise<PaymentProcessorError | void> {
const id = context.paymentSessionData.id as string
await this.getPaymentStatus(id)
paymentSessionData: Record<string, unknown>,
context: Record<string, unknown>
): Promise<
| PaymentProcessorError
| {
status: PaymentSessionStatus
data: PaymentProcessorSessionResponse["session_data"]
}
> {
const status = await this.getPaymentStatus(paymentSessionData)
return { data: paymentSessionData, status }
}

async cancelPayment(
paymentId: string
paymentSessionData: Record<string, unknown>
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse["session_data"]
> {
try {
const id = paymentSessionData.id as string
return (await this.stripe_.paymentIntents.cancel(
paymentId
id
)) as unknown as PaymentProcessorSessionResponse["session_data"]
} catch (error) {
if (error.payment_intent.status === ErrorIntentStatus.CANCELED) {
Expand All @@ -180,9 +191,9 @@ abstract class StripeBase extends AbstractPaymentProcessor {
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse["session_data"]
> {
const { id } = context.paymentSessionData
const id = context.paymentSessionData.id as string
try {
const intent = await this.stripe_.paymentIntents.capture(id as string)
const intent = await this.stripe_.paymentIntents.capture(id)
return intent as unknown as PaymentProcessorSessionResponse["session_data"]
} catch (error) {
if (error.code === ErrorCodes.PAYMENT_INTENT_UNEXPECTED_STATE) {
Expand All @@ -199,24 +210,24 @@ abstract class StripeBase extends AbstractPaymentProcessor {
}

async deletePayment(
paymentId: string
paymentSessionData: Record<string, unknown>
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse["session_data"]
> {
return await this.cancelPayment(paymentId)
return await this.cancelPayment(paymentSessionData)
}

async refundPayment(
context: PaymentProcessorContext
paymentSessionData: Record<string, unknown>,
refundAmount: number
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse["session_data"]
> {
const { amount } = context
const { id } = context.paymentSessionData
const id = paymentSessionData.id as string

try {
await this.stripe_.refunds.create({
amount: Math.round(amount),
amount: Math.round(refundAmount),
payment_intent: id as string,
})
} catch (e) {
Expand All @@ -226,16 +237,17 @@ abstract class StripeBase extends AbstractPaymentProcessor {
)
}

return context.paymentSessionData
return paymentSessionData
}

async retrievePayment(
paymentId: string
paymentSessionData: Record<string, unknown>
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse["session_data"]
> {
try {
const intent = await this.stripe_.paymentIntents.retrieve(paymentId)
const id = paymentSessionData.id as string
const intent = await this.stripe_.paymentIntents.retrieve(id)
return intent as unknown as PaymentProcessorSessionResponse["session_data"]
} catch (e) {
return this.buildError("An error occurred in retrievePayment", e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import StripeBase from "../helpers/stripe-base"
import { PaymentIntentOptions } from "../types";
import StripeBase from "../core/stripe-base"
import { PaymentIntentOptions } from "../types"

class BancontactProviderService extends StripeBase {
static identifier = "stripe-bancontact"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import StripeBase from "../helpers/stripe-base"
import { PaymentIntentOptions } from "../types";
import StripeBase from "../core/stripe-base"
import { PaymentIntentOptions } from "../types"

class BlikProviderService extends StripeBase {
static identifier = "stripe-blik"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import StripeBase from "../helpers/stripe-base"
import { PaymentIntentOptions } from "../types";
import StripeBase from "../core/stripe-base"
import { PaymentIntentOptions } from "../types"

class GiropayProviderService extends StripeBase {
static identifier = "stripe-giropay"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import StripeBase from "../helpers/stripe-base"
import { PaymentIntentOptions } from "../types";
import StripeBase from "../core/stripe-base"
import { PaymentIntentOptions } from "../types"

class IdealProviderService extends StripeBase {
static identifier = "stripe-ideal"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import StripeBase from "../helpers/stripe-base";
import { PaymentIntentOptions } from "../types";
import StripeBase from "../core/stripe-base"
import { PaymentIntentOptions } from "../types"

class StripeProviderService extends StripeBase {
static identifier = "stripe"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import StripeBase from "../helpers/stripe-base"
import { PaymentIntentOptions } from "../types";
import StripeBase from "../core/stripe-base"
import { PaymentIntentOptions } from "../types"

class Przelewy24ProviderService extends StripeBase {
static identifier = "stripe-przelewy24"
Expand Down
7 changes: 5 additions & 2 deletions packages/medusa/src/interfaces/payment-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ export interface PaymentProcessor {
/**
* Refund an existing session
* @param paymentSessionData
* @param refundAmount
*/
refundPayment(
paymentSessionData: Record<string, unknown>
paymentSessionData: Record<string, unknown>,
refundAmount: number
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse["session_data"]
>
Expand Down Expand Up @@ -185,7 +187,8 @@ export abstract class AbstractPaymentProcessor implements PaymentProcessor {
): Promise<PaymentSessionStatus>

abstract refundPayment(
paymentSessionData: Record<string, unknown>
paymentSessionData: Record<string, unknown>,
refundAmount: number
): Promise<
PaymentProcessorError | PaymentProcessorSessionResponse["session_data"]
>
Expand Down
11 changes: 7 additions & 4 deletions packages/medusa/src/services/payment-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,11 @@ export default class PaymentProviderService extends TransactionBaseService {
const provider = this.retrieveProvider(paymentToRefund.provider_id)

if (provider instanceof AbstractPaymentProcessor) {
const res = await provider.refundPayment(paymentToRefund.data)
if ("error" in res) {
const res = await provider.refundPayment(
paymentToRefund.data,
refundAmount
)
if (isPaymentProcessorError(res)) {
this.throwFromPaymentProcessorError(res as PaymentProcessorError)
} else {
// Use else to avoid casting the object and infer the type instead
Expand Down Expand Up @@ -777,8 +780,8 @@ export default class PaymentProviderService extends TransactionBaseService {
const provider = this.retrieveProvider(payment.provider_id)

if (provider instanceof AbstractPaymentProcessor) {
const res = await provider.refundPayment(payment.data)
if ("error" in res) {
const res = await provider.refundPayment(payment.data, amount)
if (isPaymentProcessorError(res)) {
this.throwFromPaymentProcessorError(res as PaymentProcessorError)
} else {
// Use else to avoid casting the object and infer the type instead
Expand Down
34 changes: 17 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4845,6 +4845,23 @@ __metadata:
languageName: unknown
linkType: soft

"@medusajs/medusa-payment-stripe-processor@workspace:packages/medusa-payment-stripe-processor":
version: 0.0.0-use.local
resolution: "@medusajs/medusa-payment-stripe-processor@workspace:packages/medusa-payment-stripe-processor"
dependencies:
"@medusajs/medusa": ^1.7.7
"@types/stripe": ^8.0.417
body-parser: ^1.19.0
cross-env: ^5.2.1
express: ^4.17.1
jest: ^25.5.4
medusa-core-utils: ^1.1.38
stripe: ^11.10.0
peerDependencies:
"@medusajs/medusa": ^1.7.7
languageName: unknown
linkType: soft

"@medusajs/medusa@*, @medusajs/medusa@^1.7.6, @medusajs/medusa@workspace:packages/medusa":
version: 0.0.0-use.local
resolution: "@medusajs/medusa@workspace:packages/medusa"
Expand Down Expand Up @@ -25003,23 +25020,6 @@ __metadata:
languageName: unknown
linkType: soft

"medusa-payment-stripe-processor@workspace:packages/medusa-payment-stripe-processor":
version: 0.0.0-use.local
resolution: "medusa-payment-stripe-processor@workspace:packages/medusa-payment-stripe-processor"
dependencies:
"@medusajs/medusa": ^1.7.7
"@types/stripe": ^8.0.417
body-parser: ^1.19.0
cross-env: ^5.2.1
express: ^4.17.1
jest: ^25.5.4
medusa-core-utils: ^1.1.38
stripe: ^11.10.0
peerDependencies:
"@medusajs/medusa": ^1.7.7
languageName: unknown
linkType: soft

"medusa-payment-stripe@workspace:packages/medusa-payment-stripe":
version: 0.0.0-use.local
resolution: "medusa-payment-stripe@workspace:packages/medusa-payment-stripe"
Expand Down

0 comments on commit 5aef163

Please sign in to comment.