React Native集成PhonePe PG SDK:交易参数与实现咨询
React Native集成PhonePe支付网关SDK:参数说明与实现步骤
各参数详解
body
首先构造符合PhonePe交易规范的原始JSON请求体,示例结构如下:
{ "merchantId": "YOUR_MERCHANT_ID", "transactionId": "UNIQUE_TRANSACTION_ID", "amount": 1000, // 单位:派萨,1000=10卢比 "redirectUrl": "https://your-domain.com/verify-transaction", "mobileNumber": "9999999999", "paymentInstrument": { "type": "UPI_INTENT" } }
将上述JSON字符串做Base64编码,得到的结果就是body参数的值。
checkSum
使用PhonePe提供的商户密钥,对原始JSON请求体字符串(不是Base64后的)进行HMAC-SHA256加密,再将加密结果转成Base64字符串,就是checkSum。示例生成代码(依赖crypto-js):
import CryptoJS from 'crypto-js'; const generateCheckSum = (rawBody, merchantKey) => { const hmac = CryptoJS.HmacSHA256(rawBody, merchantKey); return CryptoJS.enc.Base64.stringify(hmac); };
packageName
- Android 必填:传入PhonePe官方应用的包名
com.phonepe.app - iOS 可选:直接传
null即可
appSchema
你在React Native项目中配置的自定义URL Scheme,用于支付完成后回调你的App(比如myapp://payment-callback)。需提前在项目配置中声明:
- iOS:在
Info.plist添加CFBundleURLSchemes数组(包含你的schema)和LSApplicationQueriesSchemes数组(包含phonepe) - Android:在
AndroidManifest.xml的主Activity中添加处理该schema的<intent-filter>
完整交易实现步骤
准备交易核心参数
构造符合PhonePe规范的交易请求JSON,确保transactionId全局唯一,amount以派萨为单位,redirectUrl用于后端校验交易状态。生成校验和
调用上述generateCheckSum方法,用商户密钥对原始JSON字符串生成校验和。编码请求体
将原始JSON字符串转换为Base64编码,得到body参数。配置URL Scheme
- iOS:修改
ios/[项目名]/Info.plist:<key>LSApplicationQueriesSchemes</key> <array> <string>phonepe</string> </array> <key>CFBundleURLSchemes</key> <array> <string>YOUR_APP_SCHEMA</string> </array> - Android:修改
android/app/src/main/AndroidManifest.xml,在主Activity中添加:<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="YOUR_APP_SCHEMA" /> </intent-filter>
- iOS:修改
调用SDK发起交易
集成SDK后,按以下方式调用并处理结果:import PhonePePaymentSDK from 'react-native-phonepe-pg'; import { Platform } from 'react-native'; const initiatePayment = async () => { // 构造原始请求体 const rawBody = JSON.stringify({ merchantId: "YOUR_MERCHANT_ID", transactionId: `TXN_${Date.now()}`, amount: 1000, redirectUrl: "https://your-domain.com/verify-transaction", mobileNumber: "9999999999", paymentInstrument: { type: "UPI_INTENT" } }); // 生成checkSum const merchantKey = "YOUR_MERCHANT_KEY"; const checkSum = generateCheckSum(rawBody, merchantKey); // 编码body(React Native中可使用react-native-base64库) import { encode } from 'base-64'; const body = encode(rawBody); try { const result = await PhonePePaymentSDK.startTransaction( body, checkSum, Platform.OS === 'android' ? 'com.phonepe.app' : null, 'YOUR_APP_SCHEMA' ); // 处理交易结果 switch(result.status) { case 'SUCCESS': // 务必通过后端再次校验交易状态,避免前端篡改 console.log('交易成功'); break; case 'FAILURE': console.log('交易失败', result.error); break; case 'INTERRUPTED': console.log('交易中断'); break; } } catch (error) { console.log('支付调用出错', error); } };
内容的提问来源于stack exchange,提问作者Jasim Khan
相关产品推荐
相关产品推荐

