🅿️

PayPal

Integrate PayPal payments, subscriptions, and marketplace functionality with comprehensive APIs.

Payment Processing92% popularityDocumentation

Features

PayPal Checkout
Express Checkout
Subscription Billing
Marketplace Payments
Dispute Management
Transaction Search

Code Example

// PayPal Payment Integration
const paypal = require('@paypal/checkout-server-sdk');

class PayPalPayments {
  constructor(clientId, clientSecret, environment = 'sandbox') {
    this.clientId = clientId;
    this.clientSecret = clientSecret;
    
    // Set up PayPal environment
    const Environment = environment === 'sandbox' 
      ? paypal.core.SandboxEnvironment 
      : paypal.core.LiveEnvironment;
    
    this.paypalClient = new paypal.core.PayPalHttpClient(
      new Environment(clientId, clientSecret)
    );
  }

  async createOrder(amount, currency = 'USD') {
    try {
      const request = new paypal.orders.OrdersCreateRequest();
      request.prefer('return=representation');
      request.requestBody({
        intent: 'CAPTURE',
        purchase_units: [{
          amount: {
            currency_code: currency,
            value: amount.toFixed(2)
          }
        }]
      });

      const order = await this.paypalClient.execute(request);
      
      return {
        success: true,
        orderId: order.result.id,
        approvalUrl: order.result.links.find(link => link.rel === 'approve').href
      };
    } catch (error) {
      console.error('PayPal Order Creation Error:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async captureOrder(orderId) {
    try {
      const request = new paypal.orders.OrdersCaptureRequest(orderId);
      request.requestBody({});

      const capture = await this.paypalClient.execute(request);
      
      return {
        success: true,
        captureId: capture.result.purchase_units[0].payments.captures[0].id,
        status: capture.result.status
      };
    } catch (error) {
      console.error('PayPal Capture Error:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async createSubscription(planId) {
    try {
      const request = new paypal.subscriptions.SubscriptionsCreateRequest();
      request.requestBody({
        plan_id: planId,
        start_time: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(), // Start tomorrow
        subscriber: {
          name: {
            given_name: 'John',
            surname: 'Doe'
          },
          email_address: 'customer@example.com'
        }
      });

      const subscription = await this.paypalClient.execute(request);
      
      return {
        success: true,
        subscriptionId: subscription.result.id,
        approvalUrl: subscription.result.links.find(link => link.rel === 'approve').href
      };
    } catch (error) {
      console.error('PayPal Subscription Error:', error);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async verifyWebhook(headers, body, webhookId) {
    try {
      const request = new paypal.notifications.VerifyWebhookSignatureRequest();
      request.requestBody({
        transmission_id: headers['paypal-transmission-id'],
        cert_id: headers['paypal-cert-id'],
        auth_algo: headers['paypal-auth-algo'],
        transmission_sig: headers['paypal-transmission-sig'],
        transmission_time: headers['paypal-transmission-time'],
        webhook_id: webhookId,
        webhook_event: JSON.parse(body)
      });

      const response = await this.paypalClient.execute(request);
      
      return {
        success: true,
        verified: response.result.verification_status === 'SUCCESS'
      };
    } catch (error) {
      return {
        success: false,
        error: error.message
      };
    }
  }
}

// Usage example
const payments = new PayPalPayments(
  'your_client_id',
  'your_client_secret',
  'sandbox'
);

payments.createOrder(29.99, 'USD').then(console.log);