微信JsApi支付

<?php

const APPID = '';

const MCHID = '';

const KEY = '';

const JS_API_CALL_URL = '';

$openid = '';

$ip = get_client_ip();

$data['openid'] = $openid;
$data['appid'] = APPID;                //微信分配的公众账号ID
$data['mch_id'] = MCHID;                 //微信支付分配的商户号
$data['body'] = '龙测试';                 //商品信息
$data['nonce_str'] = uniqid();             //随机字符串,不长于32位。推荐随机数生成算法
$data['out_trade_no'] = time() . rand('1111', '9999');    //商户定义的商品id 或者订单号
$data['spbill_create_ip'] = $ip;               //终端ip
$data['total_fee'] = 1;
$data['trade_type'] = 'JSAPI';
$data['notify_url'] = JS_API_CALL_URL . '/Test_1.php';   //外网地址


$sign = createSign($data);
$data['sign'] = $sign;                                  //签名
$data = dataToXml($data);                               //转xml
$request = request($url = 'https://api.mch.weixin.qq.com/pay/unifiedorder', $https = true, $method = 'post', $data);               //将数据发送到微信服务器

$arr = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);


$info = array();
$info['appId'] = APPID;
$info['nonceStr'] = uniqid();
$info['package'] = 'prepay_id=' . $arr['prepay_id'];
$info['signType'] = 'MD5';
$info['timeStamp'] = '' . time();

//计算签名
$sign = createSign($info);
$info['sign'] = $sign;                                  //签名
$this->ajaxReturn(array('info' => $info));



//生成签名
function createSign($data)
{
    $string = '';
    ksort($data);
    foreach ($data as $key => $value) {
        $string .= "{$key}={$value}&";
    }
    $string .= 'key=' . KEY;
    return strToUpper(md5($string));
}

//转xml
function dataToXml($data)
{
    $xml = '<xml>';
    foreach ($data as $key => $value) {
        $xml .= "<{$key}>{$value}</{$key}>";
    }
    $xml .= '</xml>';
    return $xml;
}

//curl发送请求
function request($url, $https, $method, $data)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($https == true) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    if ($method == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}


function get_client_ip()
{
    if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches[0] : '';
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script type="text/javascript">
    function onBridgeReady() {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId":"wx2421b1c4370ec43b",     //公众号名称,由商户传入     
                "timeStamp":"1395712654",         //时间戳,自1970年以来的秒数     
                "nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随机串     
                "package":"prepay_id=u802345jgfjsdfgsdg888",     
                "signType":"MD5",         //微信签名方式:     
                "paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
            },
            function(res) {
                console.log(res);
                return;
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    // 使用以上方式判断前端返回,微信团队郑重提示:
                    //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
                }
            });
    }
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } else {
        onBridgeReady();
    }
</script>

</html>

 

posted @ 2020-07-08 11:24  za_szybko  阅读(166)  评论(0编辑  收藏  举报