React Native中PayPal自适应支付流实现:平台代收转商家
Hey there! Let's sort out this PayPal transfer flow you need for your React Native app. I see you already nailed it with Stripe, so let's get PayPal working the same way—starting with a crucial note: PayPal's Adaptive Payments is long deprecated (retired back in 2018), so that's why you're running into issues. Instead, we'll use PayPal's modern, supported APIs to build this three-party transfer flow.
Recommended Approach: PayPal Connect + Payouts API / Transfer API
This setup lets you securely handle the "collect from buyer, hold in platform, auto-transfer to merchant" flow, just like you did with Stripe. Here's the step-by-step breakdown:
1. Onboard Merchants with PayPal Connect
First, you need to get your partner merchants linked to your PayPal platform account. This lets you initiate transfers to their PayPal accounts later:
- Have merchants go through PayPal's Connect onboarding flow (you can embed this in your app or send them a web link).
- Once onboarded, you'll receive a unique
merchant_id(orpayer_id) for each merchant—store this in your database, you'll need it for transfers.
2. Accept Buyer Payments via PayPal in React Native
Use PayPal's official React Native SDK to let buyers pay into your platform's PayPal account:
- Install the SDK:
npm install @paypal/react-native-paypal - Initialize the SDK with your PayPal client ID (sandbox for testing):
import { PayPalProvider, PayPalButtons } from '@paypal/react-native-paypal'; // Wrap your payment screen with the provider <PayPalProvider clientID="YOUR_PAYPAL_CLIENT_ID" environment="sandbox"> <PayPalButtons createOrder={(data, actions) => { // Create an order for the buyer to pay your platform return actions.order.create({ purchase_units: [{ amount: { value: '10.00', // Match the buyer's total } }] }); }} onApprove={(data, actions) => { // Capture the payment to your platform's PayPal account return actions.order.capture().then((details) => { // Once captured, trigger your backend to initiate the merchant transfer fetch('YOUR_BACKEND_API/transfer-to-merchant', { method: 'POST', body: JSON.stringify({ merchantId: 'STORED_MERCHANT_ID', amount: '8.00', // Amount to send to merchant (after platform fees) transactionId: details.id }) }); }); }} /> </PayPalProvider>
3. Auto-Transfer Funds from Platform to Merchant (Backend Step)
You can't do this directly from React Native—you need a backend server to call PayPal's Payouts API or Transfer API (since these require your PayPal secret key):
Option A: Payouts API (for one-time transfers)
Use this if you're sending funds to a single merchant per buyer transaction:
// Example backend code (Node.js) const paypal = require('@paypal/checkout-server-sdk'); async function transferToMerchant(merchantId, amount) { const environment = new paypal.core.SandboxEnvironment('CLIENT_ID', 'CLIENT_SECRET'); const client = new paypal.core.PayPalHttpClient(environment); const request = new paypal.payouts.PayoutsPostRequest(); request.requestBody({ sender_batch_header: { sender_batch_id: `batch-${Date.now()}`, email_subject: 'Your payment from [Your Platform]' }, items: [{ recipient_type: 'PAYPAL_ID', amount: { value: amount, currency: 'USD' }, receiver: merchantId, // The merchant's PayPal ID from Connect note: 'Payment for your services' }] }); try { const response = await client.execute(request); return response.result; } catch (error) { console.error('Transfer failed:', error); throw error; } }
Option B: Transfer API (for moving funds between platform and linked merchants)
If you have ongoing transfers or want more control over fund movement, use the Transfer API (part of PayPal Connect). This lets you move funds from your platform's balance directly to the merchant's linked account.
Key Notes to Avoid Headaches
- Sandbox Testing First: Use PayPal's Sandbox environment to test the entire flow—create test buyer, platform, and merchant accounts to mimic real transactions.
- Compliance: PayPal requires you to clearly disclose fees, fund holding policies, and transfer timelines to both buyers and merchants. Make sure your app's terms reflect this.
- Webhooks: Set up PayPal webhooks to get notified when payments are captured and transfers are completed—this lets you update your app's order statuses automatically.
内容的提问来源于stack exchange,提问作者Atef

