docs: migrated from javascript to typescript (#2398)

This commit is contained in:
Shahed Nasser
2022-10-11 16:36:08 +03:00
committed by GitHub
parent 2ac8d8e979
commit f1605ae2a1
7 changed files with 256 additions and 179 deletions
@@ -10,7 +10,7 @@ By default, Medusa has a [manual payment provider](https://github.com/medusajs/m
Adding a Payment Provider is as simple as creating a [service](../services/create-service.md) file in `src/services`. A Payment Provider is essentially a service that extends `AbstractPaymentService` from the core Medusa package `@medusajs/medusa`.
Payment Provider Services must have a static property `identifier`. It is the name that will be used to install and refer to the Payment Provider in the Medusa server.
Payment Provider Services must have a static property `identifier`. It's the name that will be used to install and refer to the Payment Provider in the Medusa server.
:::tip
@@ -44,12 +44,51 @@ These methods are used at different points in the Checkout flow as well as when
## Create a Payment Provider
The first step to create a payment provider is to create a file in `src/services` with the following content:
The first step to create a payment provider is to create a JavaScript or TypeScript file in `src/services`. The file's name should be the name of the payment provider.
```jsx
import { AbstractPaymentService } from "@medusajs/medusa"
For example, create the file `src/services/my-payment.ts` with the following content:
class MyPaymentService extends AbstractPaymentService {
```ts
import { AbstractPaymentService, Cart, Data, Payment, PaymentSession, PaymentSessionStatus, TransactionBaseService } from "@medusajs/medusa"
import { EntityManager } from "typeorm";
class MyPaymentService extends AbstractPaymentService<TransactionBaseService> {
protected manager_: EntityManager;
protected transactionManager_: EntityManager;
getPaymentData(paymentSession: PaymentSession): Promise<Data> {
throw new Error("Method not implemented.");
}
updatePaymentData(paymentSessionData: Data, data: Data): Promise<Data> {
throw new Error("Method not implemented.");
}
createPayment(cart: Cart): Promise<Data> {
throw new Error("Method not implemented.");
}
retrievePayment(paymentData: Data): Promise<Data> {
throw new Error("Method not implemented.");
}
updatePayment(paymentSessionData: Data, cart: Cart): Promise<Data> {
throw new Error("Method not implemented.");
}
authorizePayment(paymentSession: PaymentSession, context: Data): Promise<{ data: Data; status: PaymentSessionStatus; }> {
throw new Error("Method not implemented.");
}
capturePayment(payment: Payment): Promise<Data> {
throw new Error("Method not implemented.");
}
refundPayment(payment: Payment, refundAmount: number): Promise<Data> {
throw new Error("Method not implemented.");
}
cancelPayment(payment: Payment): Promise<Data> {
throw new Error("Method not implemented.");
}
deletePayment(paymentSession: PaymentSession): Promise<void> {
throw new Error("Method not implemented.");
}
getStatus(data: Data): Promise<PaymentSessionStatus> {
throw new Error("Method not implemented.");
}
}
@@ -82,8 +121,9 @@ You can also use the constructor to initialize your integration with the third-p
Additionally, if youre creating your Payment Provider as an external plugin to be installed on any Medusa server and you want to access the options added for the plugin, you can access it in the constructor. The options are passed as a second parameter:
```jsx
constructor({}, options) {
```ts
constructor({ productService }, options) {
super();
//you can access options here
}
```
@@ -98,8 +138,11 @@ This method must return an object that is going to be stored in the `data` field
An example of a minimal implementation of `createPayment` that does not interact with any third-party providers:
```jsx
async createPayment(cart) {
```ts
import { Cart, Data } from "@medusajs/medusa"
//...
async createPayment(cart: Cart): Promise<Data> {
return {
id: 'test-payment',
status: 'pending'
@@ -117,8 +160,11 @@ This method must return an object containing the data from the third-party provi
An example of a minimal implementation of `retrievePayment` where you dont need to interact with the third-party provider:
```jsx
async retrievePayment(cart) {
```ts
import { Data } from "@medusajs/medusa"
//...
async retrievePayment(paymentData: Data): Promise<Data> {
return {};
}
```
@@ -141,9 +187,12 @@ This method returns a string that represents the status. The status must be one
An example of a minimal implementation of `getStatus` where you dont need to interact with the third-party provider:
```jsx
async getStatus (data) {
return data.status;
```ts
import { Data, PaymentSessionStatus } from "@medusajs/medusa"
//...
async getStatus(data: Data): Promise<PaymentSessionStatus> {
return PaymentSessionStatus.AUTHORIZED;
}
```
@@ -171,9 +220,12 @@ This method must return an object that will be stored in the `data` field of the
An example of a minimal implementation of `updatePayment` that does not need to make any updates on the third-party provider or the `data` field of the Payment Session:
```jsx
async updatePayment(sessionData, cart) {
return sessionData;
```ts
import { Cart, Data } from "@medusajs/medusa";
//...
async updatePayment(paymentSessionData: Data, cart: Cart): Promise<Data> {
return paymentSessionData;
}
```
@@ -189,8 +241,11 @@ This method must return an object that will be stored in the `data` field of the
An example of a minimal implementation of `updatePaymentData` that returns the `updatedData` passed in the body of the request as-is to update the `data` field of the Payment Session.
```jsx
async updatePaymentData(sessionData, updatedData) {
```ts
import { Data } from "@medusajs/medusa";
//...
async updatePaymentData(paymentSessionData: Data, updatedData: Data): Promise<Data> {
return updatedData;
}
```
@@ -210,8 +265,11 @@ You can use this method to interact with the third-party provider to delete data
An example of a minimal implementation of `deletePayment` where no interaction with a third-party provider is required:
```jsx
async deletePayment(paymentSession) {
```ts
import { PaymentSession } from "@medusajs/medusa";
//...
async deletePayment(paymentSession: PaymentSession): Promise<void> {
return;
}
```
@@ -245,15 +303,18 @@ You can utilize this method to interact with the third-party provider and perfor
An example of a minimal implementation of `authorizePayment` that doesnt need to interact with any third-party provider:
```jsx
async authorizePayment(paymentSession, context) {
return {
status: 'authorized',
data: {
id: 'test'
}
};
}
```ts
import { Data, PaymentSession, PaymentSessionStatus } from "@medusajs/medusa";
//...
async authorizePayment(paymentSession: PaymentSession, context: Data): Promise<{ data: Data; status: PaymentSessionStatus; }> {
return {
status: PaymentSessionStatus.AUTHORIZED,
data: {
id: 'test'
}
};
}
```
### getPaymentData
@@ -266,8 +327,11 @@ This method must return an object to be stored in the `data` field of the Paymen
An example of a minimal implementation of `getPaymentData`:
```jsx
async getPaymentData(paymentSession) {
```ts
import { Data, PaymentSession } from "@medusajs/medusa";
//...
async getPaymentData(paymentSession: PaymentSession): Promise<Data> {
return paymentSession.data;
}
```
@@ -286,8 +350,11 @@ This method must return an object that will be stored in the `data` field of the
An example of a minimal implementation of `capturePayment` that doesnt need to interact with a third-party provider:
```jsx
async capturePayment(payment) {
```ts
import { Data, Payment } from "@medusajs/medusa";
//...
async capturePayment(payment: Payment): Promise<Data> {
return {
status: 'captured'
};
@@ -308,8 +375,11 @@ This method must return an object that is stored in the `data` field of the Paym
An example of a minimal implementation of `refundPayment` that doesnt need to interact with a third-party provider:
```jsx
async refundPayment(payment, amount) {
```ts
import { Data, Payment } from "@medusajs/medusa";
//...
async refundPayment(payment: Payment, refundAmount: number): Promise<Data> {
return {
id: 'test'
}
@@ -333,8 +403,11 @@ This method must return an object that is stored in the `data` field of the Paym
An example of a minimal implementation of `cancelPayment` that doesnt need to interact with a third-party provider:
```jsx
async cancelPayment(payment) {
```ts
import { Data, Payment } from "@medusajs/medusa";
//...
async cancelPayment(payment: Payment): Promise<Data> {
return {
id: 'test'
}
@@ -361,13 +434,16 @@ If youre using Medusas [Next.js](../../../starters/nextjs-medusa-starter.m
An example of the implementation of `retrieveSavedMethods` taken from Stripes Payment Provider:
```jsx
```ts
import { Customer, Data } from "@medusajs/medusa"
//...
/**
* Fetches a customers saved payment methods if registered in Stripe.
* @param {object} customer - customer to fetch saved cards for
* @returns {Promise<Array<object>>} saved payments methods
*/
async retrieveSavedMethods(customer) {
async retrieveSavedMethods(customer: Customer): Promise<Data[]> {
if (customer.metadata && customer.metadata.stripe_id) {
const methods = await this.stripe_.paymentMethods.list({
customer: customer.metadata.stripe_id,