From ccfc5f666dc22e1b0d13b7552fe3dcef75e23772 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 15 Nov 2022 17:27:00 -0300 Subject: [PATCH] feat(medusa-js): Add payment and payment collections clients (#2608) --- packages/medusa-js/src/index.ts | 3 + .../medusa-js/src/resources/admin/index.ts | 4 ++ .../resources/admin/payment-collections.ts | 45 +++++++++++++++ .../medusa-js/src/resources/admin/payments.ts | 45 +++++++++++++++ .../src/resources/payment-collections.ts | 56 +++++++++++++++++++ packages/medusa/src/api/index.js | 1 + .../manage-payment-sessions.ts | 28 +++++++++- 7 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 packages/medusa-js/src/resources/admin/payment-collections.ts create mode 100644 packages/medusa-js/src/resources/admin/payments.ts create mode 100644 packages/medusa-js/src/resources/payment-collections.ts diff --git a/packages/medusa-js/src/index.ts b/packages/medusa-js/src/index.ts index dba8ba49df..cd527c7b49 100644 --- a/packages/medusa-js/src/index.ts +++ b/packages/medusa-js/src/index.ts @@ -8,6 +8,7 @@ import CustomersResource from "./resources/customers" import GiftCardsResource from "./resources/gift-cards" import OrdersResource from "./resources/orders" import OrderEditsResource from "./resources/order-edits" +import PaymentCollectionsResource from "./resources/payment-collections" import PaymentMethodsResource from "./resources/payment-methods" import ProductsResource from "./resources/products" import ProductTypesResource from "./resources/product-types" @@ -37,6 +38,7 @@ class Medusa { public collections: CollectionsResource public giftCards: GiftCardsResource public paymentMethods: PaymentMethodsResource + public paymentCollections: PaymentCollectionsResource constructor(config: Config) { this.client = new Client(config) @@ -59,6 +61,7 @@ class Medusa { this.collections = new CollectionsResource(this.client) this.giftCards = new GiftCardsResource(this.client) this.paymentMethods = new PaymentMethodsResource(this.client) + this.paymentCollections = new PaymentCollectionsResource(this.client) } } diff --git a/packages/medusa-js/src/resources/admin/index.ts b/packages/medusa-js/src/resources/admin/index.ts index 8f5cd31a82..1506cd1b2e 100644 --- a/packages/medusa-js/src/resources/admin/index.ts +++ b/packages/medusa-js/src/resources/admin/index.ts @@ -30,6 +30,8 @@ import AdminTaxRatesResource from "./tax-rates" import AdminUploadsResource from "./uploads" import AdminUsersResource from "./users" import AdminVariantsResource from "./variants" +import AdminPaymentCollectionsResource from "./payment-collections" +import AdminPaymentsResource from "./payments" class Admin extends BaseResource { public auth = new AdminAuthResource(this.client) @@ -63,6 +65,8 @@ class Admin extends BaseResource { public notifications = new AdminNotificationsResource(this.client) public taxRates = new AdminTaxRatesResource(this.client) public uploads = new AdminUploadsResource(this.client) + public paymentCollections = new AdminPaymentCollectionsResource(this.client) + public payments = new AdminPaymentsResource(this.client) } export default Admin diff --git a/packages/medusa-js/src/resources/admin/payment-collections.ts b/packages/medusa-js/src/resources/admin/payment-collections.ts new file mode 100644 index 0000000000..d0def6aaab --- /dev/null +++ b/packages/medusa-js/src/resources/admin/payment-collections.ts @@ -0,0 +1,45 @@ +import { + AdminUpdatePaymentCollectionRequest, + AdminPaymentCollectionDeleteRes, + AdminPaymentCollectionRes, + GetPaymentCollectionsParams, +} from "@medusajs/medusa" +import { ResponsePromise } from "../../typings" +import BaseResource from "../base" +import qs from "qs" + +class AdminPaymentCollectionsResource extends BaseResource { + retrieve( + id: string, + query?: GetPaymentCollectionsParams, + customHeaders: Record = {} + ): ResponsePromise { + let path = `/admin/payment-collections/${id}` + + if (query) { + const queryString = qs.stringify(query) + path += `?${queryString}` + } + + return this.client.request("GET", path, undefined, {}, customHeaders) + } + + update( + id: string, + payload: AdminUpdatePaymentCollectionRequest, + customHeaders: Record = {} + ): ResponsePromise { + const path = `/admin/payment-collections/${id}` + return this.client.request("POST", path, payload, {}, customHeaders) + } + + delete( + id: string, + customHeaders: Record = {} + ): ResponsePromise { + const path = `/admin/payment-collections/${id}` + return this.client.request("DELETE", path, undefined, {}, customHeaders) + } +} + +export default AdminPaymentCollectionsResource diff --git a/packages/medusa-js/src/resources/admin/payments.ts b/packages/medusa-js/src/resources/admin/payments.ts new file mode 100644 index 0000000000..b93766c775 --- /dev/null +++ b/packages/medusa-js/src/resources/admin/payments.ts @@ -0,0 +1,45 @@ +import { + AdminPaymentRes, + AdminPostPaymentRefundsReq, + AdminRefundRes, + GetPaymentsParams, +} from "@medusajs/medusa" +import qs from "qs" +import { ResponsePromise } from "../../typings" +import BaseResource from "../base" + +class AdminPaymentsResource extends BaseResource { + retrieve( + id: string, + query?: GetPaymentsParams, + customHeaders: Record = {} + ): ResponsePromise { + let path = `/admin/payments/${id}` + + if (query) { + const queryString = qs.stringify(query) + path = `/admin/payments/${id}?${queryString}` + } + + return this.client.request("GET", path, undefined, {}, customHeaders) + } + + capturePayment( + id: string, + customHeaders: Record = {} + ): ResponsePromise { + const path = `/admin/payments/${id}/capture` + return this.client.request("POST", path, undefined, {}, customHeaders) + } + + refundPayment( + id: string, + payload: AdminPostPaymentRefundsReq, + customHeaders: Record = {} + ): ResponsePromise { + const path = `/admin/payments/${id}/refund` + return this.client.request("POST", path, payload, {}, customHeaders) + } +} + +export default AdminPaymentsResource diff --git a/packages/medusa-js/src/resources/payment-collections.ts b/packages/medusa-js/src/resources/payment-collections.ts new file mode 100644 index 0000000000..8850a63809 --- /dev/null +++ b/packages/medusa-js/src/resources/payment-collections.ts @@ -0,0 +1,56 @@ +import { + GetPaymentCollectionsParams, + StoreManagePaymentCollectionSessionRequest, + StoreRefreshPaymentCollectionSessionRequest, + StorePaymentCollectionSessionRes, + StorePaymentCollectionRes, +} from "@medusajs/medusa" +import { ResponsePromise } from "../typings" +import BaseResource from "./base" +import qs from "qs" + +class PaymentCollectionsResource extends BaseResource { + retrieve( + id: string, + query?: GetPaymentCollectionsParams, + customHeaders: Record = {} + ): ResponsePromise { + let path = `/store/payment-collections/${id}` + + if (query) { + const queryString = qs.stringify(query) + path += `?${queryString}` + } + + return this.client.request("GET", path, undefined, {}, customHeaders) + } + + authorize( + id: string, + customHeaders: Record = {} + ): ResponsePromise { + const path = `/store/payment-collections/${id}/authorize` + return this.client.request("POST", path, undefined, {}, customHeaders) + } + + manageSessions( + id: string, + payload: StoreManagePaymentCollectionSessionRequest, + customHeaders: Record = {} + ): ResponsePromise { + const path = `/store/payment-collections/${id}/sessions` + return this.client.request("POST", path, payload, {}, customHeaders) + } + + refreshPaymentSession( + id: string, + session_id: string, + payload: StoreRefreshPaymentCollectionSessionRequest, + customHeaders: Record = {} + ): ResponsePromise { + const path = `/store/payment-collections/${id}/sessions/${session_id}/refresh` + return this.client.request("POST", path, payload, {}, customHeaders) + } +} + +export default PaymentCollectionsResource diff --git a/packages/medusa/src/api/index.js b/packages/medusa/src/api/index.js index 05d160175b..d08d2a8124 100644 --- a/packages/medusa/src/api/index.js +++ b/packages/medusa/src/api/index.js @@ -66,3 +66,4 @@ export * from "./routes/store/returns" export * from "./routes/store/shipping-options" export * from "./routes/store/swaps" export * from "./routes/store/variants" +export * from "./routes/store/payment-collections" diff --git a/packages/medusa/src/api/routes/store/payment-collections/manage-payment-sessions.ts b/packages/medusa/src/api/routes/store/payment-collections/manage-payment-sessions.ts index 882b8a73dc..37bb8c294c 100644 --- a/packages/medusa/src/api/routes/store/payment-collections/manage-payment-sessions.ts +++ b/packages/medusa/src/api/routes/store/payment-collections/manage-payment-sessions.ts @@ -45,7 +45,33 @@ import { PaymentCollectionService } from "../../../../services" * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) * // must be previously logged in or use api token - * medusa.paymentCollections.sessions(payment_id, payload) + * + * // Total amount = 10000 + * + * // Adding two new sessions + * medusa.paymentCollections.manageSessions(payment_id, [ + * { + * provider_id: "stripe", + * customer_id: "cus_123", + * amount: 5000, + * }, + * { + * provider_id: "manual", + * customer_id: "cus_123", + * amount: 5000, + * }, + * ]) + * .then(({ payment_collection }) => { + * console.log(payment_collection.id); + * }); + * + * // Updating one session and removing the other + * medusa.paymentCollections.manageSessions(payment_id, { + * provider_id: "stripe", + * customer_id: "cus_123", + * amount: 10000, + * session_id: "ps_123456" + * }) * .then(({ payment_collection }) => { * console.log(payment_collection.id); * });