H5微信登录

<?php

namespace App\Service\waph5;

/*

  • 小程序微信支付
    */

    class WapPayService {

    protected $appid;
    protected $mch_id;
    protected $key;
    protected $openid;
    protected $total_fee;
    protected $out_trade_no;
    protected $body;

function init(array $data) {
    $this->appid = '';
    $this->mch_id = '';
    $this->key = '';
    $this->total_fee = $data['total_fee'];
    $this->out_trade_no = $data['out_trade_no'];
    $this->body = $data['remark'];
}

//统一下单接口
public function unifiedOrder() {
    $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
    $parameters = array(
        'appid' => $this->appid, //小程序ID
        'mch_id' => $this->mch_id, //商户号
        'nonce_str' => $this->createNoncestr(), //随机字符串
        'body' => $this->body, //商品描述
        'out_trade_no' => $this->out_trade_no, //商户订单号
        'total_fee' => $this->total_fee, //floatval($this->total_fee * 100), //总金额 单位 分
        'spbill_create_ip' => $this->getIp(), //终端IP
        'notify_url' => \App::config()->get('app', 'host') . 'api/onNotify', //通知地址
        'trade_type' => 'MWEB', //交易类型
        'scene_info' => '{"h5_info": {"type":"Wap","wap_url": "https://xieyi.gzyoufa.com","wap_name": "携逸订房"}}'
    );
    //统一下单签名
    $parameters['sign'] = $this->getSign($parameters,$this->key);

    // print_r($parameters);die;
    $xmlData = $this->arrayToXml($parameters);
    $return = $this->http($url, 'POST', $xmlData);
    return $this->xmlToArray($return);
}



//作用:产生随机字符串,不长于32位
private function createNoncestr($length = 32) {
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
        $str.= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}

//作用:生成签名
private function getSign($params, $appkey, $separator = '&') {

    if (isset($params['sign'])) {
        unset($params['sign']);
    }
    ksort($params);
    $str = "";
    foreach ($params as $key => $value) {
        if (empty($value) && $value !== 0) {
            continue;
        }

        $str .= $key . "=" . $value . $separator;
    }

    $str = trim($str, $separator);
    $raw = $str . $separator .'key='.$appkey;
    return \strtoupper(md5($raw));
}

/**
 * 作用:将xml转为array
 */
public function xmlToArray($xml) {
    //将XML转为array
    $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $array_data;
}

function arrayToXml($arr) {
    $xml = "<root>";
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $xml.="<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
        } else {
            $xml.="<" . $key . ">" . $val . "</" . $key . ">";
        }
    }
    $xml.="</root>";
    return $xml;
}

public function http($url, $method = 'GET', $postfields = null, $headers = array(), $debug = false) {
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ci, CURLOPT_TIMEOUT, 30);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

    switch ($method) {
        case 'POST':
            curl_setopt($ci, CURLOPT_POST, true);
            if (!empty($postfields)) {
                curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                $this->postdata = $postfields;
            }
            break;
    }
    curl_setopt($ci, CURLOPT_URL, $url);
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);

    $response = curl_exec($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

    if ($debug) {
        echo "=====post data======\r\n";
        var_dump($postfields);

        echo '=====info=====' . "\r\n";
        print_r(curl_getinfo($ci));

        echo '=====$response=====' . "\r\n";
        print_r($response);
    }
    curl_close($ci);
    return $response;
}

public function getIp() {
    //获取用户IP
    if (getenv('HTTP_CLIENT_IP')) {
        $onlineip = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $onlineip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif (getenv('REMOTE_ADDR')) {
        $onlineip = getenv('REMOTE_ADDR');
    } else {
        $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
    }

    return $onlineip;
}

}

例子:
$wapService = new WapPayService();
$wapService->init($paymentConf);
$return = $wapService->unifiedOrder();//统一下单

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,645评论 25 708
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • 把当前目录作为Root Document只需要这条命令即可:php -S localhost:3300 也可以指定...
    绚烂的时光阅读 747评论 0 1
  • 使用RVM(Ruby Version Manager) 切换之后,整个ruby环境切换,随之gem的环境也切换了所...
    Derek重名了阅读 1,079评论 0 0