Aller au contenu

💰 PayPal API

Typ: REST API
Auth: OAuth 2.0 (Client ID + Secret)
Status: ✅ Alternative zu Stripe


Übersicht

PayPal für: - Einmalzahlungen - Internationale Mieter - Käuferschutz - PayPal-Checkout


API Endpoints

Methode Endpunkt Beschreibung Cache TTL
POST /api/paypal/orders Zahlung erstellen -
POST /api/paypal/orders/:id/capture Zahlung abschließen -
GET /api/paypal/orders/:id Zahlungsstatus 1min
POST /api/paypal/invoices Rechnung erstellen -
POST /api/paypal/payouts Auszahlung -

PayPal Integration

import paypal from '@paypal/checkout-server-sdk';

// PayPal Client
function getPayPalClient() {
  const environment = process.env.NODE_ENV === 'production'
    ? new paypal.core.LiveEnvironment(
        process.env.PAYPAL_CLIENT_ID,
        process.env.PAYPAL_CLIENT_SECRET
      )
    : new paypal.core.SandboxEnvironment(
        process.env.PAYPAL_CLIENT_ID,
        process.env.PAYPAL_CLIENT_SECRET
      );

  return new paypal.core.PayPalHttpClient(environment);
}

// Zahlung erstellen
async function createPayment(amount: number, description: string) {
  const client = getPayPalClient();

  const request = new paypal.orders.OrdersCreateRequest();
  request.prefer('return=representation');
  request.requestBody({
    intent: 'CAPTURE',
    purchase_units: [{
      amount: {
        currency_code: 'EUR',
        value: (amount / 100).toFixed(2)  // PayPal erwartet String
      },
      description
    }],
    application_context: {
      return_url: 'https://app.example.com/payment-success',
      cancel_url: 'https://app.example.com/payment-cancel'
    }
  });

  const response = await client.execute(request);
  return response.result;
}

// Zahlung abschließen
async function capturePayment(orderId: string) {
  const client = getPayPalClient();
  const request = new paypal.orders.OrdersCaptureRequest(orderId);

  const response = await client.execute(request);
  return response.result;
}

Umgebungsvariablen

# PayPal
PAYPAL_CLIENT_ID=""
PAYPAL_CLIENT_SECRET=""
PAYPAL_MODE="sandbox"  # sandbox | live

Internationale Zahlungen • Käuferschutz • OAuth 2.0