-
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 createOrAdjustTransaction refund flow
- Loading branch information
Showing
15 changed files
with
140 additions
and
184 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,22 @@ | ||
fragment OrderLine on OrderLine { | ||
id | ||
productSku | ||
productName | ||
quantity | ||
taxClass { | ||
id | ||
} | ||
unitPrice { | ||
net { | ||
amount | ||
} | ||
} | ||
totalPrice { | ||
net { | ||
amount | ||
} | ||
tax { | ||
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
9 changes: 5 additions & 4 deletions
9
apps/taxes/src/modules/avatax/order-confirmed/avatax-address-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 |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { AddressesModel } from "avatax/lib/models/AddressesModel"; | ||
import { AddressFragment } from "../../../../generated/graphql"; | ||
import { taxProviderUtils } from "../../taxes/tax-provider-utils"; | ||
import { avataxAddressFactory } from "../address-factory"; | ||
import { AvataxConfig } from "../avatax-connection-schema"; | ||
import { CreateTransactionModel } from "avatax/lib/models/CreateTransactionModel"; | ||
|
||
export class AvataxAddressResolver { | ||
resolve({ | ||
from, | ||
to, | ||
}: { | ||
from: AvataxConfig["address"]; | ||
to: AddressFragment; | ||
}): CreateTransactionModel["addresses"] { | ||
to: AddressFragment | undefined | null; | ||
}): AddressesModel { | ||
return { | ||
shipFrom: avataxAddressFactory.fromChannelAddress(from), | ||
shipTo: avataxAddressFactory.fromSaleorAddress(to), | ||
shipTo: avataxAddressFactory.fromSaleorAddress(taxProviderUtils.resolveOptionalOrThrow(to)), | ||
}; | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
apps/taxes/src/modules/avatax/order-refunded/avatax-order-refunded-lines-transformer.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,18 @@ | ||
import { OrderRefundedPayload } from "../../../pages/api/webhooks/order-refunded"; | ||
import { RefundTransactionParams } from "../avatax-client"; | ||
|
||
export class AvataxOrderRefundedLinesTransformer { | ||
transform(payload: OrderRefundedPayload): RefundTransactionParams["lines"] { | ||
const refundTransactions = | ||
payload.order?.transactions.filter((t) => t.refundedAmount.amount > 0) ?? []; | ||
|
||
if (!refundTransactions.length) { | ||
throw new Error("Cannot refund order without any refund transactions"); | ||
} | ||
|
||
return refundTransactions.map((t) => ({ | ||
amount: -t.refundedAmount.amount, | ||
taxIncluded: true, | ||
})); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
apps/taxes/src/modules/avatax/order-refunded/avatax-order-refunded-payload-transformer.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,47 @@ | ||
import { Logger, createLogger } from "../../../lib/logger"; | ||
import { OrderRefundedPayload } from "../../../pages/api/webhooks/order-refunded"; | ||
import { taxProviderUtils } from "../../taxes/tax-provider-utils"; | ||
import { RefundTransactionParams } from "../avatax-client"; | ||
import { AvataxConfig, defaultAvataxConfig } from "../avatax-connection-schema"; | ||
import { AvataxDocumentCodeResolver } from "../avatax-document-code-resolver"; | ||
import { AvataxAddressResolver } from "../order-confirmed/avatax-address-resolver"; | ||
import { AvataxOrderRefundedLinesTransformer } from "./avatax-order-refunded-lines-transformer"; | ||
|
||
export 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 addressResolver = new AvataxAddressResolver(); | ||
const linesTransformer = new AvataxOrderRefundedLinesTransformer(); | ||
const documentCodeResolver = new AvataxDocumentCodeResolver(); | ||
|
||
const addresses = addressResolver.resolve({ | ||
from: avataxConfig.address, | ||
to: payload.order?.shippingAddress, | ||
}); | ||
const lines = linesTransformer.transform(payload); | ||
const customerCode = taxProviderUtils.resolveStringOrThrow(payload.order?.user?.id); | ||
const code = documentCodeResolver.resolve({ | ||
avataxDocumentCode: payload.order?.avataxDocumentCode, | ||
orderId: payload.order?.id, | ||
}); | ||
|
||
return { | ||
code, | ||
lines, | ||
customerCode, | ||
addresses, | ||
date: new Date(), | ||
companyCode: avataxConfig.companyCode ?? defaultAvataxConfig.companyCode, | ||
}; | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
apps/taxes/src/modules/avatax/order-refunded/avatax-refunds-resolver.test.ts
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
apps/taxes/src/modules/avatax/order-refunded/avatax-refunds-resolver.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.