PHP 商家转账到零钱

public function wxTransferAccounts($setting, $data)
{
    $appid = $setting['appid'];
    $mchid = $setting['mchid'];
    $out_order_no = "订单号";
    $batch_name = "提现";
    $batch_remark = "提现";
    $total_amount = (int)bcmul($data['total_amount'], 100, 0);
    $total_num = 1;
    $transfer_detail_list = [[
        'out_detail_no' => "订单号",
        'transfer_amount' => (int)bcmul($data['total_amount'], 100, 0),
        'transfer_remark' => '提现',
        'openid' => $data['open_id']
    ]];

    $arr = [
        'appid' => $appid,
        'mchid' => $mchid,
        'out_batch_no' => $out_order_no,
        'transfer_detail_list' => $transfer_detail_list,
        'batch_name' => $batch_name,
        'batch_remark' => $batch_remark,
        'total_amount' => $total_amount,
        'total_num' => $total_num
    ];

    $url = "https://api.mch.weixin.qq.com/v3/transfer/batches";

    $arr = json_encode($arr);
    $header = $this->getV3Sign('POST', '/v3/transfer/batches', $arr, $setting);

    $res = $this->curl_post($url, $arr, $header);
    $result = json_decode($res, true);

    if (isset($result['batch_status']) && $result['batch_status'] == "ACCEPTED") {
        return ['result_code' => 'SUCCESS'];
    } else {
        return ['result_code' => 'FAIL', 'err_code_des' => $result['message']];
    }

}

//post请求
public function curl_post($url, $data, $headers = array(), $is_post = true)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    //设置header头
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    if ($is_post) {
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }

    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

public function toUrlParams($values)
{
    $buff = '';
    foreach ($values as $k => $v) {
        if ($k != 'sign' && $v != '' && !is_array($v)) {
            $buff .= $k . '=' . $v . '&';
        }
    }
    return trim($buff, '&');
}

public function makeSign($values, $key = null)
{
    //签名步骤一:按字典序排序参数
    ksort($values);
    $string = $this->toUrlParams($values);
    //签名步骤二:在string后加入KEY
    $string = $string . '&key=' . $key;
    $string = md5($string);
    $string = hash_hmac("sha256", $string, $key);
    //签名步骤四:所有字符转为大写
    $result = strtoupper($string);
    return $result;
}

public function getV3Sign($http_method = 'POST', $url = '', $body = '', $config)
{
	//私密证书 serial_no 使用 openssl x509 -in cert.pem -noout -serial获取
    $mch_private_key = $config['key_pem'];
    $timestamp = time();//时间戳
    $nonce = getRandom(32);//随机串

    //构造签名串
    $message = $http_method . "\n" . $url . "\n" . $timestamp . "\n" . $nonce . "\n" . $body . "\n";
    //计算签名值
    openssl_sign($message, $raw_sign, $mch_private_key, 'SHA256');
    $sign = base64_encode($raw_sign);

    //设置HTTP头
    $token = sprintf('WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
        $config['mchid'], $nonce, $timestamp, $config['serial_no'], $sign);
    $headers = [
        'Accept: application/json',
        'User-Agent: */*',
        'Content-Type: application/json; charset=utf-8',
        'Authorization: ' . $token,
    ];
    return $headers;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值