下面是详细讲解“PHP开发APP端微信支付功能”的完整攻略:
1. 准备工作
1.1 获取微信支付商户号和API密钥
在微信支付平台上注册商户账号,并开通支付功能,获取商户号和API密钥。商户号是唯一标识商户身份的ID,API密钥是用于加密签名的密钥。获取商户号和API密钥后需保存好,并妥善保管,不要泄露。
1.2 配置APP端支付信息
根据微信支付的要求,在APP中设置好支付信息,如支付金额、订单号、商品描述等。同时,需要生成一个唯一的交易号,用于和微信支付商户号进行交互。
1.3 安装微信支付SDK
在PHP项目中安装微信支付SDK,如“wechatpay/wechatpay-guzzlehttp”或“jdpay/merchant-sdk-php”等。
2. 实现支付功能
2.1 获取预付订单
通过微信支付API获取预付订单号,有两种方式可以获取预付订单:
2.1.1 通过SDK实现
use WeChatPay\Builder;
$builder = new Builder();
$builder->setAppId('your app id')
->setMerchantId('your merchant id')
->setApiKey('your api key')
->setNotifyUrl('your notify url')
->setTradeType('JSAPI')
->setBody('your order body')
->setOutTradeNo('your order number')
->setTotalFee('your order amount');
$result = $builder->send();
$prepayId = $result->get('prepay_id');
2.1.2 通过HTTP接口实现
$postData = [
'appid' => 'your app id',
'mch_id' => 'your merchant id',
'nonce_str' => uniqid(),
'body' => 'your order body',
'out_trade_no' => 'your order number',
'total_fee' => 'your order amount',
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => 'your notify url',
'trade_type' => 'JSAPI',
'openid' => 'customer openid'
];
$postData['sign'] = paySign($postData, 'your api key');
$xml = arrayToXml($postData);
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$response = httpPost($url, $xml);
$arrayData = xmlToArray($response);
$prepayId = $arrayData['prepay_id'];
2.2 构造JSAPI支付参数
将获取到的预付订单号及API密钥等参数通过SDK封装成JSAPI支付参数,以传递给APP端:
use WeChatPay\Builder;
$builder = new Builder();
$builder->setPrepayId($prepayId)
->setAppId('your app id')
->setApiKey('your api key');
$jsApiParameters = $builder->buildJsApiParameters();
2.3 生成签名
对JSAPI支付参数进行签名,以确保信息安全,私有信息不被攻击被篡改:
use WeChatPay\Security;
$security = new Security();
$jsApiSign = $security->sign($jsApiParameters, 'your api key');
2.4 发起支付请求
将JSAPI支付参数和签名传递给APP端,APP端再通过微信SDK调用支付功能,完成支付请求。
以上是实现PHP开发APP端微信支付功能的完整攻略,下面是两条示例如下:
示例1:使用wechatpay/wechatpay-guzzlehttp进行支付
use WeChatPay\Builder;
use WeChatPay\Security;
use GuzzleHttp\Client;
$client = new Client();
$builder = new Builder();
$builder->setAppId('your app id')
->setMerchantId('your merchant id')
->setApiKey('your api key')
->setNotifyUrl('your notify url')
->setTradeType('JSAPI')
->setBody('your order body')
->setOutTradeNo('your order number')
->setTotalFee('your order amount');
$response = $client->post('https://api.mch.weixin.qq.com/pay/unifiedorder', [
'body' => $builder->toXml(),
]);
$xmlArray = $builder->fromXml($response->getBody()->getContents());
$prepayId = $xmlArray['prepay_id'];
$builder->setPrepayId($prepayId);
$security = new Security();
$jsApiParameters = $builder->buildJsApiParameters();
$jsApiSign = $security->sign($jsApiParameters, 'your api key');
//将JSAPI支付参数和签名传递给APP端,由APP端进行发起支付请求
示例2:使用jdpay/merchant-sdk-php进行支付
use JD\Pay\Model\Order;
use JD\Pay\PayClient;
$order = new Order();
$order->setMerchant('your merchant id')
->setOutTradeNo('your order number')
->setAmount('your order amount')
->setTradeType('JSAPI')
->setBody('your order body');
$payClient = new PayClient('your app id', 'your api key');
$payClient->setNotifyUrl('your notify url');
$response = $payClient->createOrder($order);
$responseData = $response->getData();
$prepayId = $responseData['data']['orderInfo']['prepay_id'];
$jsApiParameters = $payClient->getJsApiParameters($prepayId);
$jsApiSign = $payClient->sign($jsApiParameters);
//将JSAPI支付参数和签名传递给APP端,由APP端进行发起支付请求
以上两条示例,第一条使用了wechatpay/wechatpay-guzzlehttp进行支付,第二条使用了jdpay/merchant-sdk-php进行支付,两种方式可以根据自己的需求选择使用。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PHP开发APP端微信支付功能 - Python技术站