代公众号实现微信支付

微信开放平台 公众号第三平台类库

wx_component 2.0 微信公众号第三平台类库,PHP版本。

wx_component 2.0 是根据微信官方开放平台提供的文档开发的PHP类库,
第三方平台使用此类库,可以在部署代码和代公众号实现业务时,更加容易、方便上手。
wx_component 2.0 增加了代公众号实现对微信支付的支持

实现功能

  1. 实现公众号授权和绑定
  2. 代公众号实现业务
  3. 代公众号使用JS SDK
  4. 第三方平台事件通知处理
  5. 实现公众号全网发布接入检测的自动化测试代码
  6. 代公众号实现对微信支付的支持,包含:
    • 微信被扫支付
    • 微信扫码支付
    • 微信JSAPI支付
    • 微信地址本
    • 微信订单查询
    • 微信订单退款
    • 微信订单退款查询
    • 微信对账单下载

官网地址

官方地址:http://weixin.yunyicheng.cn
Github地址:https://github.com/lvfan2008/wx_component

微信支付样例代码

cache = new FileCache($GLOBALS['cacheDir']);
        $this->wxComponentService = new WxComponentService($wxComponentConfig, $this->cache);
        $this->payCfg = $GLOBALS['wxTestPayCfg'];
        $this->wxPay = new WxPay($this->payCfg);

        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $this->baseUrl = substr($url, 0, strrpos($url, "/"));
        $this->notify_url = $this->baseUrl . "/component_wx_pay_notify.php";
        $this->out_trade_no[] = "A" . time() . rand(1, 999);
        $this->out_trade_no[] = "B" . time() . rand(1, 999);
        $this->out_trade_no[] = "C" . time() . rand(1, 999);
        $this->refundNo = "R" . time() . rand(1, 999);
    }

    public function run()
    {
        $this->init();
        if (method_exists($this, $_REQUEST['act'])) {
            try {
                $this->$_REQUEST['act']();
            } catch (Exception $e) {
                die("404");
            }
        } else {
            $this->def_act();
        }
    }

    public function def_act()
    {
        if ($_GET['qr_url']) {
            QRcode::png($_GET['qr_url']);
            exit;
        }
    }

    public function microPay()
    {
        $result = $this->wxPay->microPay($_POST['authCode'], $_POST['out_trade_no'], $_POST['body'], intval($_POST['amount'] * 100));
        if ($result === false) {
            $this->msg = "支付失败,原因:" . $this->wxPay->lastErrMsg;
        } else {
            $this->msg = "被扫支付结果:" . print_r($result, true);
        }
        $this->msg = "

post:" . print_r($_POST, true) . "

" . $this->msg; } public function nativePay() { $result = $this->wxPay->getNativePayUrl2($_POST['productId'], $_POST['body'], $_POST['out_trade_no'], intval($_POST['amount'] * 100), $this->notify_url); if ($result === false) { $this->msg = "获取扫码URL失败,原因:" . $this->wxPay->lastErrMsg; } else { $qr_url = $this->baseUrl . "/component_wx_pay.php?qr_url=" . urlencode($result); $this->msg = "生成扫码URL:{$result}
二维码:![]({$qr_url})"; } $this->msg = "

post:" . print_r($_POST, true) . "

" . $this->msg; } public function jsApiPayCode() { $this->jsApiPay(); } public function jsApiPay() { if ($_POST['act'] == 'jsApiPay') { $this->cache->setCache('jsApiPay', json_encode($_POST), -1); $callbackUrl = $this->baseUrl . "/component_wx_pay.php?act=jsApiPayCode"; $this->wxComponentService->getOauthOpenId($this->payCfg['AppId'], $callbackUrl); } else { $_POST = json_decode($this->cache->getCache('jsApiPay'), true); $openId = $this->wxComponentService->getOauthOpenId($this->payCfg['AppId']); $jsApiParameters = $this->wxPay->getJsApiPayParams($openId, $_POST['body'], $_POST['out_trade_no'], intval($_POST['amount'] * 100), $this->notify_url); if ($jsApiParameters === false) { $this->msg = "获取getJsApiPayParams失败,原因:" . $this->wxPay->lastErrMsg; } else { $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $accessToken = $this->wxComponentService->getOauthAccessToken($this->payCfg['AppId']); $jsEditAddress = $this->wxPay->getJsEditAddressParams($this->payCfg['AppId'], $url, $accessToken); $this->msg = << //调用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', {$jsApiParameters}, function(res){ WeixinJSBridge.log(res.err_msg); var msg = res.err_code == undefined ? '': res.err_code ; msg += res.err_desc == undefined ? '': res.err_desc ; msg += res.err_msg; alert(msg); } ); } function callpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } //获取共享地址 function editAddress() { WeixinJSBridge.invoke( 'editAddress', {$jsEditAddress}, function(res){ var value1 = res.proviceFirstStageName; var value2 = res.addressCitySecondStageName; var value3 = res.addressCountiesThirdStageName; var value4 = res.addressDetailInfo; var tel = res.telNumber; if(value1=='' || value1 == undefined){ alert("cancle editAddress"); }else{ alert(value1 + value2 + value3 + value4 + ":" + tel); } } ); } var callEditAddress = function(){ if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', editAddress, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', editAddress); document.attachEvent('onWeixinJSBridgeReady', editAddress); } }else{ editAddress(); } }; EOF; $msg = "

post:" . print_r($_POST, true) . "

"; $msg .= "

"; $msg .= "

"; $this->msg = $msg . $this->msg; } } } public function queryOrder() { $tradeNo = $_POST['transaction_id']; $type = 0; if ($tradeNo == '') { $type = 1; $tradeNo = $_POST['out_trade_no']; } $result = $this->wxPay->queryOrder($tradeNo, $type); if ($result === false) { $this->msg = "查询失败,原因:" . $this->wxPay->lastErrMsg; } else { $this->msg = print_r($result, true); } $this->msg = "

post:" . print_r($_POST, true) . "

" . $this->msg; } public function refundOrder() { $transactionIdOrOutTradeNo = $_POST['transaction_id']; $type = 0; if ($transactionIdOrOutTradeNo == '') { $type = 1; $transactionIdOrOutTradeNo = $_POST['out_trade_no']; } $totalFee = intval($_POST['amount'] * 100); $refundFee = intval($_POST['refundAmount'] * 100); $refundNo = $_POST['refundNo']; $result = $this->wxPay->refundOrder($totalFee, $refundFee, $refundNo, $transactionIdOrOutTradeNo, $type); if ($result === false) { $this->msg = "查询失败,原因:" . $this->wxPay->lastErrMsg; } else { log_ex("refund", print_r($result, true)); $this->msg = print_r($result, true); } $this->msg = "

post:" . print_r($_POST, true) . "

" . $this->msg; } public function refundOrderQuery() { $refundQueryKey = $_POST['refund_id']; $type = 0; if ($refundQueryKey == '') { $type = 1; $refundQueryKey = $_POST['refundNo']; } if ($refundQueryKey == '') { $type = 2; $refundQueryKey = $_POST['transaction_id']; } if ($refundQueryKey == '') { $type = 3; $refundQueryKey = $_POST['out_trade_no']; } $result = $this->wxPay->refundOrderQuery($refundQueryKey, $type); if ($result === false) { $this->msg = "退款订单查询失败,原因:" . $this->wxPay->lastErrMsg; } else { $this->msg = print_r($result, true); } $this->msg = "

post:" . print_r($_POST, true) . "

" . $this->msg; } public function downloadBill() { $result = $this->wxPay->getBill($_POST['bill_date'], $_POST['bill_type'], $_POST['tar_type']); if ($result === false) { $this->msg = "退款订单查询失败,原因:" . $this->wxPay->lastErrMsg; } else { if (substr($result, 0, 5) == "") { $this->msg = htmlspecialchars($result); $this->msg = "

post:" . print_r($_POST, true) . "

" . $this->msg; } else { $filename = $_POST['bill_date'] . $_POST['bill_type'] . ($_POST['tar_type'] == "GZIP" ? ".gzip" : ".csv"); header("Content-Type: APPLICATION/OCTET-STREAM"); header("Content-Disposition: attachment;filename=\"" . $filename . "\""); header("Cache-Control: max-age=0"); echo $result; exit; } } } } $WxPayExample = new WxPayExample(); $WxPayExample->run(); ?> 第三方平台代公众号使用微信支付样例

第三方平台代公众号使用微信支付样例

msg) { echo "

提交结果:

{$WxPayExample->msg}

"; } ?>

被扫支付

请输入微信付款码串:

请输入商户订单号:

请输入商品名称:

请输入商品金额:

扫码支付

请输入商品标示串:

请输入商户订单号:

请输入商品名称:

请输入商品金额:

支付回调URL:notify_url; ?>

JSAPI支付,微信下测试,提交后可测试微信地址

请输入商户订单号:

请输入商品名称:

请输入商品金额:

支付回调URL:notify_url; ?>

微信订单查询

请输入微信交易单号:

请输入商户订单号:

微信交易单号和商户订单号,二选一填写。

微信订单退款

请输入微信交易单号:

请输入商户订单号:

请输入订单金额:

请输入退款金额:

请输入退款单号:

微信交易单号和商户订单号,二选一填写。

微信订单退款查询

请输入refund_id:

请输入退款单号:

请输入微信交易单号:

请输入商户订单号:

refund_id、退款单号、微信交易单号、商户订单号,四选一填写。

微信对账单下载

请输入日期:

账单类型:

压缩类型:

你可能感兴趣的:(代公众号实现微信支付)