CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-medusa-interfaces

Core interfaces for Medusa e-commerce framework service implementations

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

payment-service.mddocs/

Payment Service

Interface for payment service implementations providing methods for creating, authorizing, capturing, and refunding payments through various payment gateways.

Deprecation Notice: Use AbstractPaymentProcessor from @medusajs/medusa instead.

Capabilities

Static Methods

Type checking and identification methods for payment services.

/**
 * Static property identifying this as a payment service
 */
static _isPaymentService: boolean;

/**
 * Checks if an object is a payment service
 * @param {object} obj - Object to check
 * @returns {boolean} True if obj is a payment service
 */
static isPaymentService(obj: object): boolean;

Service Identification

Method to get the service identifier.

/**
 * Returns the service identifier from constructor
 * @returns {string} Service identifier
 */
getIdentifier(): string;

Usage Example:

import { PaymentService } from "medusa-interfaces";

class StripePaymentService extends PaymentService {
  static identifier = "stripe";
  
  getIdentifier() {
    return this.constructor.identifier; // Returns "stripe"
  }
}

Core Payment Operations

Abstract methods that must be implemented by child classes.

/**
 * Creates a payment to be processed with the service's payment gateway
 * @param {object} cart - The cart that the payment should cover
 * @returns {Promise<object>} Promise resolving to payment data
 * @throws {Error} If not overridden by child class
 */
createPayment(cart: object): Promise<object>;

/**
 * Retrieves an existing payment for a cart
 * @param {object} cart - The cart whose payment should be retrieved
 * @returns {Promise<object>} Promise resolving to payment object as stored with provider
 * @throws {Error} If not overridden by child class
 */
retrievePayment(cart: object): Promise<object>;

/**
 * Updates a payment when the cart is updated
 * @param {object} cart - The cart whose payment should be updated
 * @returns {Promise<object>} Promise resolving to updated payment object
 * @throws {Error} If not overridden by child class
 */
updatePayment(cart: object): Promise<object>;

/**
 * Gets the status of a payment
 * @returns {any} Payment status
 * @throws {Error} If not overridden by child class
 */
getStatus(): any;

/**
 * Authorizes a payment for later capture
 * @returns {any} Authorization result
 * @throws {Error} If not overridden by child class
 */
authorizePayment(): any;

/**
 * Captures a previously authorized payment
 * @returns {any} Capture result
 * @throws {Error} If not overridden by child class
 */
capturePayment(): any;

/**
 * Processes a refund for a payment
 * @returns {any} Refund result
 * @throws {Error} If not overridden by child class
 */
refundPayment(): any;

/**
 * Deletes a payment
 * @returns {any} Deletion result
 * @throws {Error} If not overridden by child class
 */
deletePayment(): any;

Optional Methods

Methods with default implementations that can be overridden.

/**
 * Retrieves saved payment methods for a customer
 * @param {object} customer - Customer object
 * @returns {Promise<array>} Promise resolving to array of saved payment methods
 */
retrieveSavedMethods(customer: object): Promise<array>;

Implementation Example

import { PaymentService } from "medusa-interfaces";

class CustomPaymentService extends PaymentService {
  static identifier = "custom-payment";
  
  constructor(options) {
    super();
    this.options_ = options;
  }

  async createPayment(cart) {
    // Create payment with external provider
    const paymentData = await this.externalProvider.createPayment({
      amount: cart.total,
      currency: cart.region.currency_code,
      customer: cart.customer_id
    });
    
    return {
      id: paymentData.id,
      status: "pending",
      data: paymentData
    };
  }

  async authorizePayment(paymentData) {
    // Authorize payment with external provider
    const result = await this.externalProvider.authorize(paymentData.id);
    
    return {
      status: result.status,
      data: result
    };
  }

  async capturePayment(paymentData) {
    // Capture authorized payment
    const result = await this.externalProvider.capture(paymentData.id);
    
    return {
      status: result.status,
      data: result
    };
  }

  async refundPayment(paymentData, refundAmount) {
    // Process refund
    const result = await this.externalProvider.refund(
      paymentData.id, 
      refundAmount
    );
    
    return {
      status: result.status,
      data: result
    };
  }

  async retrievePayment(cart) {
    // Retrieve existing payment
    return await this.externalProvider.getPayment(cart.payment_id);
  }

  async updatePayment(cart) {
    // Update payment details
    return await this.externalProvider.updatePayment(cart.payment_id, {
      amount: cart.total
    });
  }

  getStatus(paymentData) {
    return paymentData.status;
  }

  async deletePayment(paymentData) {
    // Delete payment
    return await this.externalProvider.deletePayment(paymentData.id);
  }

  async retrieveSavedMethods(customer) {
    // Retrieve customer's saved payment methods
    return await this.externalProvider.getSavedMethods(customer.id);
  }
}

Error Handling

All abstract methods throw descriptive errors when not implemented:

  • "createPayment must be overridden by the child class"
  • "getPayment must be overridden by the child class"
  • "updatePayment must be overridden by the child class"
  • "getStatus must be overridden by the child class"
  • "authorizePayment must be overridden by the child class"
  • "capturePayment must be overridden by the child class"
  • "refundPayment must be overridden by the child class"
  • "deletePayment must be overridden by the child class"

docs

base-service.md

file-service.md

fulfillment-service.md

index.md

notification-service.md

oauth-service.md

payment-service.md

search-service.md

tile.json