加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (https://www.0555zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

php实现微信支付之现金红包

发布时间:2021-05-24 10:09:23 所属栏目:站长百科 来源:网络整理
导读:副标题#e# 网上的很多PHP微信支付接入教程都颇为复杂,且需要配置和引入较多的文件,本人通过整理后给出一个单文件版的,希望可以给各位想接入微信支付的带来些许帮助和借鉴意义。 直接运行该文件即可给指定的微信用户发送现金红包。 需要注意的事项: 1.微

/**

  • 发送红包
  • @param string $openid 用户在该公众号下的Openid
  • @param float $totalFee 红包金额 单位元
  • @param string $outTradeNo 订单号
  • @param string $orderName 红包发送者名称
  • @param string $wishing 祝福语
  • @param string $actName 互动名称
  • @return string
    /
    public function createJsBizPackage($openid,$totalFee,$actName)
    {
    $config = array(
    'mch_id' => $this->mchid,'appid' => $this->appid,'key' => $this->apiKey,);
    $unified = array(
    'wxappid' => $config['appid'],'send_name' => $sendName,'mch_id' => $config['mch_id'],'nonce_str' => self::createNonceStr(),'re_openid' => $openid,'mch_billno' => $outTradeNo,'client_ip' => '127.0.0.1','total_amount' => intval($totalFee
    100),//单位 转为分
    'total_num'=>1,//红包发放总人数
    'wishing'=>$wishing,//红包祝福语
    'act_name'=>$actName,//活动名称
    'remark'=>'remark',//备注信息,如为中文注意转为UTF8编码
    'scene_id'=>'PRODUCT_2',//发放红包使用场景,红包金额大于200时必传。https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
    );
    $unified['sign'] = self::getSign($unified,$config['key']);
    $responseXml = $this->curlPost('https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack',self::arrayToXml($unified));
    $unifiedOrder = simplexml_load_string($responseXml,'SimpleXMLElement',LIBXML_NOCDATA);
    if ($unifiedOrder === false) {
    die('parse xml error');
    }
    if ($unifiedOrder->return_code != 'SUCCESS') {
    die($unifiedOrder->return_msg);
    }
    if ($unifiedOrder->result_code != 'SUCCESS') {
    die($unifiedOrder->err_code);
    }
    return true;
    }

public static function curlGet($url = '',$options = array())
{
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
if (!empty($options)) {
curl_setopt_array($ch,$options);
}
//https请求 不验证证书和host
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

public function curlPost($url = '',$postData = '',$options = array())
{
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,CURLOPT_POSTFIELDS,$postData);
curl_setopt($ch,30); //设置cURL允许执行的最长秒数
if (!empty($options)) {
curl_setopt_array($ch,false);

(编辑:网站开发网_马鞍山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!