php微信提现怎么写
时间 : 2023-04-26 17:43:02声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性
PHP微信提现需要使用微信商户平台接口和相关库来实现。下面是一个简单的示例代码展示了如何使用PHP实现微信提现功能。
首先,你需要在微信商户平台中注册并获取到商户号、API密钥等信息。
```php
<?php
// 加载微信支付类库
require_once('WxPayApi.php');
require_once('WxPayConfig.php');
require_once('WxPayData.php');
// 设置商户号和API密钥
$config = new WxPayConfig();
$config->SetAppId('你的AppId');
$config->SetMchId('你的商户号');
$config->SetKey('你的API密钥');
// 实例化微信支付API
$wxpay = new WxPayApi($config);
// 设置提现参数
$input = new WxPayData();
$input->SetPartnerTradeNo('提现单号');
$input->SetOpenid('接收者的openid');
$input->SetCheckName('NO_CHECK');
$input->SetAmount('提现金额'); // 单位为分
$input->SetDesc('提现备注');
$input->SetSpbillCreateIp($_SERVER['SERVER_ADDR']);
// 发起提现请求
$result = $wxpay->Transfers($input);
// 处理提现结果
if($result['result_code'] == 'SUCCESS') {
// 提现成功
} else {
// 提现失败
}
上述代码中,我们使用了`WxPayApi`类来实例化微信支付API,并设置了商户号和API密钥。然后,我们设置了提现参数 `$input`,包括提现单号、接收者的openid、提现金额、提现备注等信息。最后,我们通过调用 `$wxpay->Transfers($input)` 方法发起提现请求,并判断提现结果来处理业务逻辑。
需要注意的是,提现金额的单位为分,且需要满足以下条件才能进行提现:
1. 商户号必须为微信支付认证的商户号;
2. 提现金额不能超过商户余额;
3. 收款用户必须是微信支付认证的商家可用法人用户;
4. 收款用户必须绑定了企业微信版帐号;
5. 企业微信版帐号的实名认证必须通过。
如果以上条件都满足,那么可以通过上述代码实现微信提现功能。
实现微信提现功能需要借助微信支付接口和企业付款功能,以下是一步步实现微信提现的代码:
1. 配置微信支付
在微信支付后台开通企业付款功能,并设置API密钥,证书等信息。在使用API前需要通过微信提供的SDK获取access_token,代码示例:
require_once "WxPay/lib/WxPay.Api.php";
require_once "WxPay/lib/WxPay.Notify.php";
require_once "WxPay/lib/WxPay.Config.php";
$wxConfig = new WxPayConfig();
$accessToken = $wxConfig::getAccessToken();
2. 创建请求类
创建请求类,并定义请求参数和请求方法。示例代码如下:
class WxPayWithdrawal
{
protected $mch_appid; // 商户账号appid
protected $mchid; // 商户号
protected $nonce_str; // 随机字符串
protected $partner_trade_no; // 商户订单号
protected $openid; // 用户openid
protected $check_name; // 校验用户姓名选项
protected $amount; // 金额
protected $desc; // 企业付款描述信息
protected $spbill_create_ip; // Ip地址
protected $key; // 商户支付密钥
public function __construct($mch_appid, $mchid, $key, $partner_trade_no, $openid, $amount, $desc, $spbill_create_ip)
{
$this->mch_appid = $mch_appid;
$this->mchid = $mchid;
$this->key = $key;
$this->partner_trade_no = $partner_trade_no;
$this->openid = $openid;
$this->amount = $amount;
$this->desc = $desc;
$this->spbill_create_ip = $spbill_create_ip;
$this->check_name = 'NO_CHECK'; // 不校验用户姓名
$this->nonce_str = $this->getNonceStr(); // 随机字符串
}
/**
* 生成随机字符串
* @param int $length
* @return string
*/
public function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 生成签名
*/
protected function makeSign()
{
$data = [
"amount" => $this->amount,
"check_name" => $this->check_name,
"desc" => $this->desc,
"mch_appid" => $this->mch_appid,
"mchid" => $this->mchid,
"nonce_str" => $this->nonce_str,
"openid" => $this->openid,
"partner_trade_no" => $this->partner_trade_no,
"spbill_create_ip" => $this->spbill_create_ip
];
ksort($data);
$str = urldecode(http_build_query($data)) . "&key=" . $this->key;
return strtoupper(md5($str));
}
/**
* 获取XML数据
*/
protected function toXml()
{
$data = [
"amount" => $this->amount,
"check_name" => $this->check_name,
"desc" => $this->desc,
"mch_appid" => $this->mch_appid,
"mchid" => $this->mchid,
"nonce_str" => $this->nonce_str,
"openid" => $this->openid,
"partner_trade_no" => $this->partner_trade_no,
"spbill_create_ip" => $this->spbill_create_ip,
"sign" => $this->makeSign()
];
return $this->arrayToXml($data);
}
/**
* 数组转XML
* @param $arr
* @param string $root
* @return mixed
*/
protected function arrayToXml($arr, $root = '<xml>')
{
$xml = $root;
foreach ($arr as $k => $v) {
if (is_array($v)) {
$xml .= $this->arrayToXml($v, "<{$k}>");
} else {
$xml .= "<{$k}>{$v}</{$k}>";
}
}
$xml .= str_replace('<', '</', $root) . '>';
return $xml;
}
/**
* 发送请求
*/
public function send()
{
$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
$xml = $this->toXml();
$response = $this->postXmlCurl($xml, $url);
return $response;
}
/**
* CURL发送post请求
* @param $xml
* @param $url
* @param bool $useCert
* @param int $second
* @return bool|mixed
*/
private function postXmlCurl($xml, $url, $useCert = false, $second = 30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //严格校验
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($useCert == true) {
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, SSLCERT_PATH);
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, SSLKEY_PATH);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
return false;
}
}
}
3. 调用微信提现接口
在获取请求数据和实例化请求类之后,调用微信提现接口进行提现。示例代码如下:
/**
* 微信提现接口调用示例
* $mch_appid 商户账号appid
* $mchid 商户号
* $key 商户支付密钥
* $partner_trade_no 商户支付流水号
* $openid 用户openid
* $amount 提现金额
* $desc 描述信息
* $spbill_create_ip 用户IP地址
*/
$mch_appid = "your_mch_appid";
$mchid = "your_mchid";
$key = "your_key";
$partner_trade_no = "your_partner_trade_no";
$openid = "your_openid";
$amount = "your_amount";
$desc = "your_desc";
$spbill_create_ip = "your_spbill_create_ip";
$withdrawal = new WxPayWithdrawal($mch_appid, $mchid, $key, $partner_trade_no, $openid, $amount, $desc, $spbill_create_ip);
$response = $withdrawal->send();
// 处理响应结果
if ($response) {
$result = $this->xmlToArray($response);
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
// 提现成功
} else {
// 提现失败
}
}
以上就是实现微信提现功能的全部代码,通过以上步骤可以轻松进行微信提现操作。当然,具体的业务规则和实现可能因应用场景而不同,需要开发者自行参考微信提现接口文档和自身业务需求进行相应的开发。
上一篇
php字符怎么替换字符串
下一篇
怎么找php零基础教学
https/SSL证书广告优选IDC>>
推荐主题模板更多>>
推荐文章