feat(medusa-js): Add payment and payment collections clients (#2608)

This commit is contained in:
Carlos R. L. Rodrigues
2022-11-15 17:27:00 -03:00
committed by GitHub
parent 3de553c27f
commit ccfc5f666d
7 changed files with 181 additions and 1 deletions

View File

@@ -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)
}
}

View File

@@ -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

View File

@@ -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<string, any> = {}
): ResponsePromise<AdminPaymentCollectionRes> {
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<string, any> = {}
): ResponsePromise<AdminPaymentCollectionRes> {
const path = `/admin/payment-collections/${id}`
return this.client.request("POST", path, payload, {}, customHeaders)
}
delete(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminPaymentCollectionDeleteRes> {
const path = `/admin/payment-collections/${id}`
return this.client.request("DELETE", path, undefined, {}, customHeaders)
}
}
export default AdminPaymentCollectionsResource

View File

@@ -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<string, any> = {}
): ResponsePromise<AdminPaymentRes> {
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<string, any> = {}
): ResponsePromise<AdminPaymentRes> {
const path = `/admin/payments/${id}/capture`
return this.client.request("POST", path, undefined, {}, customHeaders)
}
refundPayment(
id: string,
payload: AdminPostPaymentRefundsReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminRefundRes> {
const path = `/admin/payments/${id}/refund`
return this.client.request("POST", path, payload, {}, customHeaders)
}
}
export default AdminPaymentsResource

View File

@@ -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<string, any> = {}
): ResponsePromise<StorePaymentCollectionRes> {
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<string, any> = {}
): ResponsePromise<StorePaymentCollectionRes> {
const path = `/store/payment-collections/${id}/authorize`
return this.client.request("POST", path, undefined, {}, customHeaders)
}
manageSessions(
id: string,
payload: StoreManagePaymentCollectionSessionRequest,
customHeaders: Record<string, any> = {}
): ResponsePromise<StorePaymentCollectionRes> {
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<string, any> = {}
): ResponsePromise<StorePaymentCollectionSessionRes> {
const path = `/store/payment-collections/${id}/sessions/${session_id}/refresh`
return this.client.request("POST", path, payload, {}, customHeaders)
}
}
export default PaymentCollectionsResource

View File

@@ -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"

View File

@@ -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);
* });