Guide to Integrating WeChat Mobile Payment (WeChat Pay) for Android and iOS
This article explains how to apply for WeChat Pay, obtain required credentials, and integrate the payment flow—including unified order, signed parameter generation, client SDK invocation, and asynchronous result handling—on both Android and iOS platforms using PHP examples.
This article provides a step‑by‑step guide for applying, configuring, and integrating WeChat Mobile Payment (WeChat Pay) on both Android and iOS platforms.
1. Application – Follow the official documentation to apply for the Open Platform and the Merchant Platform. After approval you obtain AppID, AppSecret, mch_id, API key and merchant certificates.
2. Integration Process – The main flow includes unified order, generating signed payment parameters on the server, invoking the client SDK, and handling asynchronous payment notifications.
2.1 Unified Order (PHP example)
$appid = ""; //你的appid
$mch_id = ""; //商户id
$wx_api_key = ""; //商户api秘钥
$out_trade_no = ""; //自己业务系统生成的交易no,可以唯一标识
$client_ip = ""; //客户端ip
$notify_url = ""; //接收支付结果通知url
$UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //统一下单地址
$data = array();
$data['appid'] = $appid;
$data['mch_id'] =$mch_id;
$data['nonce_str'] = randomStr(20); //随机20位字符串
$data['body'] = "微信移动支付测试";
$data['detail'] = "微信移动支付测试";
$data['out_trade_no'] = $out_trade_no;
$data['total_fee'] = 1; //注意 单位是分
$data['spbill_create_ip'] = $client_ip;
$data['notify_url'] = $notify_url;
$data['trade_type'] = "APP"; //交易类型
$data['sign'] =sign($data, $wx_api_key); //签名
//转为xml格式
$xml_str = arrayToXmlStr($data);
//发送请求 使用封装好的curl_post
$result = curl_post($UNIFIED_ORDER_URL, $xml_str);
//解析得到的值
$get_data = simplexml_load_string($raw_data, 'SimpleXMLElement', LIBXML_NOCDATA);
$get_para = array();
$get_sign = "";
foreach ($get_data->children() as $child)
{
if($child->getName() == 'sign') {
$get_sign = strval($child);
} else {
$get_para[strval($child->getName())] = strval($child);
}
}
if($get_para['return_code'] !== "SUCCESS") {
//return code fail
}
//验证签名
if(!verifySign($get_sign, $get_para, $wx_api_key)) {
//验证签名非法
}
//可以自行处理解析获得的参数
//todo...2.2 Generating Payment Parameters
//生成支付参数
$data = array();
$data['appid'] = $appid;
$data['mch_id'] =$mch_id;
$data['prepayid'] = $prepayid; //刚才统一下单生成的prepayid
$data['package'] = "Sign=WXPay";
$data['noncestr'] = randomStr(20);
$data['timestamp'] = time();
$data['sign'] =sign($data, $wx_api_key);
$pay_param = json_encode($data);3. Calling Payment
Android – Use a custom Android SDK (GitHub: https://github.com/tsy12321/PayAndroid). Ensure the package name and signing certificate match the values entered in the Open Platform; the app must be signed when testing.
iOS – Use a custom iOS SDK (GitHub: https://github.com/tsy12321/PayiOS).
4. Asynchronous Result Notification
$raw_data = $GLOBALS["HTTP_RAW_POST_DATA"];
$get_data = simplexml_load_string($raw_data, 'SimpleXMLElement', LIBXML_NOCDATA);
$get_para = array();
$get_sign = "";
foreach ($get_data->children() as $child)
{
if($child->getName() == 'sign') {
$get_sign = strval($child);
} else {
$get_para[strval($child->getName())] = strval($child);
}
}
if($get_para['return_code'] !== "SUCCESS") {
//return code fail
die("
FAIL
");
}
//验证签名
if(!verifySign($get_sign, $get_para, $wx_api_key)) {
//验证签名非法
//todo
die("
FAIL
");
}
//在这其实通知已经接受成功 可以返回成功告诉微信不用再次通知了
echo("
SUCCESS
");
//业务状态码判断
if ($get_para['result_code'] !== 'SUCCESS') {
//状态码错误
//支付错误 更改订单状态 记录log等
//...
}
//支付成功 更改订单状态 记录log等
//todoImportant notes: verify signatures, handle duplicate notifications, and confirm final status on the server.
5. Other Recommendations
After receiving a synchronous result on the client, periodically poll the server for the final status; the server’s result is authoritative.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.