You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

无对应服务端语言SDK时,网站接入PayPal收款的方法及文档指引

PayPal Integration Guide for Languages Without Official SDKs

Hey there! Let's break this down clearly for you since you're working with a language that doesn't have an official PayPal SDK. I'll start with the docs confusion, then walk through the integration steps.

Which PayPal Docs Should You Use?

First off: Prioritize the PayPal REST API documentation, with a side of the PayPal Checkout docs if you want a pre-built frontend button. Here's why:

  • PayPal Checkout is a ready-to-go integration kit that includes frontend JS buttons and backend SDK helpers. But since your language doesn't have an SDK, you'll skip the SDK part and use the REST API directly for all backend logic.
  • The REST API is the underlying, language-agnostic layer that all PayPal SDKs and integration tools build on. It uses standard HTTP requests (GET/POST/PUT) with JSON bodies, so you can call it from any language that can send HTTP requests.

You can ignore older APIs like PayPal NVP/SOAP—they're legacy and not recommended for new integrations.

Step-by-Step Integration for PayPal Payments

Since you don't have an SDK, you'll be working directly with HTTP requests. Here's the core workflow:

1. Set Up Your PayPal Developer Account & Credentials

  • Sign up for a PayPal Developer account and create a new "REST API App" (you'll need separate apps for sandbox testing and production).
  • Grab your Client ID and Secret from the app dashboard. These are used to authenticate every API request you make.

2. Choose Your Integration Flow

You have two main options, depending on whether you want a frontend checkout button or a pure backend redirect:

Option A: PayPal Checkout with Frontend Button (Recommended for User Experience)

  • Add PayPal's official frontend JS to your checkout page. This renders a "Pay with PayPal" button that handles the user's payment flow (logging into PayPal, confirming payment).
  • When the user clicks the button and completes payment, your frontend will get an order ID. Your backend then needs to call the REST API to capture the payment (more on that below).

Option B: Pure Backend Payment Link

  • If you don't want a frontend button, use the REST API to create an order, then extract the approval URL from the response. Redirect your user to this URL—they'll complete payment on PayPal's site, then be sent back to your specified return URL.
  • After the user returns, your backend calls the capture API to finalize the payment.

3. Core REST API Calls You'll Need

All API requests go to PayPal's endpoints (sandbox: https://api-m.sandbox.paypal.com, production: https://api-m.paypal.com). You'll need to include an authorization header with your Client ID/Secret encoded in Base64:
Authorization: Basic {base64_encode(client_id:secret)}

Create an Order

Send a POST request to /v2/checkout/orders with a JSON body like this:

{
  "intent": "CAPTURE",
  "purchase_units": [
    {
      "amount": {
        "currency_code": "USD",
        "value": "10.00"
      }
    }
  ],
  "application_context": {
    "return_url": "https://your-site.com/payment-success",
    "cancel_url": "https://your-site.com/payment-cancel"
  }
}
  • intent: CAPTURE means you want to capture payment immediately; use AUTHORIZE if you need to hold funds and capture later.
  • The response will include an id (order ID) and an approval_url (for Option B).

Capture the Payment

Once the user has approved the payment, send a POST request to /v2/checkout/orders/{order_id}/capture (replace {order_id} with the ID from the create order response). The response will include details like the transaction ID, status, and amount captured—this is where you confirm the payment was successful in your system.

4. Handle Webhooks (Critical for Reliability)

Don't rely solely on the return URL to confirm payments—users might close the tab before redirecting. Instead, set up PayPal Webhooks to receive real-time notifications for events like PAYMENT.CAPTURE.COMPLETED or PAYMENT.CAPTURE.DENIED.

  • In your PayPal developer app, add a webhook URL (a public endpoint on your server) and subscribe to the relevant events.
  • Your server should validate the webhook signature to ensure it's actually from PayPal, then update your order status accordingly.

5. Test Thoroughly in Sandbox

Before going live, use PayPal's sandbox environment to test every flow:

  • Create a sandbox buyer account from the developer dashboard.
  • Use sandbox credentials for all API requests.
  • Simulate successful payments, failed payments, and canceled payments to make sure your code handles all cases.

Key Tips for Language-Agnostic Integration

  • Make sure your HTTP client correctly sets the Content-Type: application/json header for POST/PUT requests.
  • Parse the JSON responses correctly—PayPal returns detailed error messages if something goes wrong (check the details field in error responses).
  • Store the order ID and transaction ID in your database for reconciliation and support purposes.

内容的提问来源于stack exchange,提问作者Korim

火山引擎 最新活动