-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🚧 add work in progress refunds
- Loading branch information
Showing
11 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fragment AvataxOrderMetadata on Order { | ||
avataxEntityCode: metafield(key: "avataxEntityCode") | ||
avataxTaxCalculationDate: metafield(key: "avataxTaxCalculationDate") | ||
avataxDocumentCode: metafield(key: "avataxDocumentCode") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fragment Money on Money { | ||
currency | ||
amount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
apps/taxes/src/modules/avatax/order-refunded/avatax-order-refunded-adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { RefundType } from "avatax/lib/enums/RefundType"; | ||
import { z } from "zod"; | ||
import { Logger, createLogger } from "../../../lib/logger"; | ||
import { OrderRefundedPayload } from "../../../pages/api/webhooks/order-refunded"; | ||
import { WebhookAdapter } from "../../taxes/tax-webhook-adapter"; | ||
import { AvataxClient, RefundTransactionParams } from "../avatax-client"; | ||
import { AvataxConfig, defaultAvataxConfig } from "../avatax-connection-schema"; | ||
import { taxProviderUtils } from "../../taxes/tax-provider-utils"; | ||
|
||
class AvataxOrderRefundedPayloadTransformer { | ||
private logger: Logger; | ||
|
||
constructor() { | ||
this.logger = createLogger({ name: "AvataxOrderRefundedPayloadTransformer" }); | ||
} | ||
|
||
transform(payload: OrderRefundedPayload, avataxConfig: AvataxConfig): RefundTransactionParams { | ||
this.logger.debug( | ||
{ payload }, | ||
"Transforming the Saleor payload for refunding order with AvaTax...", | ||
); | ||
|
||
const isFull = true; | ||
|
||
const transactionCode = z | ||
.string() | ||
.min(1, "Unable to refund transaction. Avatax id not found in order metadata") | ||
.parse(payload.order?.avataxId); | ||
|
||
const baseParams: Pick<RefundTransactionParams, "transactionCode" | "companyCode"> = { | ||
transactionCode, | ||
companyCode: avataxConfig.companyCode ?? defaultAvataxConfig.companyCode, | ||
}; | ||
|
||
if (!isFull) { | ||
return { | ||
...baseParams, | ||
model: { | ||
refundType: RefundType.Partial, | ||
refundDate: new Date(), | ||
refundLines: payload.order?.lines?.map((line) => | ||
// todo: replace with some other code | ||
taxProviderUtils.resolveStringOrThrow(line.productSku), | ||
), | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
...baseParams, | ||
model: { | ||
refundType: RefundType.Full, | ||
refundDate: new Date(), | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export class AvataxOrderRefundedAdapter implements WebhookAdapter<OrderRefundedPayload, void> { | ||
private logger: Logger; | ||
|
||
constructor(private readonly config: AvataxConfig) { | ||
this.logger = createLogger({ name: "AvataxOrderRefundedAdapter" }); | ||
} | ||
|
||
async send(payload: OrderRefundedPayload) { | ||
this.logger.debug( | ||
{ payload }, | ||
"Transforming the Saleor payload for refunding order with AvaTax...", | ||
); | ||
|
||
if (!this.config.isAutocommit) { | ||
throw new Error( | ||
"Unable to refund transaction. AvaTax can only refund commited transactions.", | ||
); | ||
} | ||
|
||
const client = new AvataxClient(this.config); | ||
const payloadTransformer = new AvataxOrderRefundedPayloadTransformer(); | ||
const target = payloadTransformer.transform(payload, this.config); | ||
|
||
const response = await client.refundTransaction(target); | ||
|
||
this.logger.debug( | ||
{ response }, | ||
`Succesfully refunded the transaction of id: ${target.transactionCode}`, | ||
); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
apps/taxes/src/modules/avatax/order-refunded/avatax-refunds-resolver.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { PaymentFragment, TransactionKind } from "../../../../generated/graphql"; | ||
import { AvataxRefundsResolver } from "./avatax-refunds-resolver"; | ||
import { expect, describe, it } from "vitest"; | ||
|
||
describe("AvataxRefundsResolver", () => { | ||
it("returns transaction amounts for refunds", () => { | ||
const resolver = new AvataxRefundsResolver(); | ||
const mockPayments: PaymentFragment[] = [ | ||
{ | ||
transactions: [ | ||
{ | ||
kind: TransactionKind.Refund, | ||
amount: { | ||
amount: 20.0, | ||
currency: "USD", | ||
}, | ||
}, | ||
{ | ||
kind: TransactionKind.Capture, | ||
amount: { | ||
amount: 20.0, | ||
currency: "USD", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
transactions: [ | ||
{ | ||
kind: TransactionKind.Refund, | ||
amount: { | ||
amount: 35.0, | ||
currency: "USD", | ||
}, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const refunds = resolver.resolve(mockPayments); | ||
|
||
expect(refunds).toEqual([ | ||
{ | ||
amount: 20.0, | ||
currency: "USD", | ||
}, | ||
{ | ||
amount: 35.0, | ||
currency: "USD", | ||
}, | ||
]); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
apps/taxes/src/modules/avatax/order-refunded/avatax-refunds-resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { PaymentFragment, TransactionKind } from "../../../../generated/graphql"; | ||
|
||
export class AvataxRefundsResolver { | ||
resolve(payments: PaymentFragment[]) { | ||
return payments | ||
.flatMap( | ||
(payment) => payment.transactions?.filter((t) => t.kind === TransactionKind.Refund) ?? [], | ||
) | ||
.map((t) => t.amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters