123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- <?php
- require_once "WxPay.Exception.php";
- require_once "WxPay.Data.php";
- class WxPayApi
- {
-
- public static function unifiedOrder($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
-
- if(!$inputObj->IsOut_trade_noSet()) {
- throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
- }else if(!$inputObj->IsBodySet()){
- throw new WxPayException("缺少统一支付接口必填参数body!");
- }else if(!$inputObj->IsTotal_feeSet()) {
- throw new WxPayException("缺少统一支付接口必填参数total_fee!");
- }else if(!$inputObj->IsTrade_typeSet()) {
- throw new WxPayException("缺少统一支付接口必填参数trade_type!");
- }
-
-
- if($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()){
- throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
- }
- if($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()){
- throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
- }
-
-
- if(!$inputObj->IsNotify_urlSet() && $config->GetNotifyUrl() != ""){
- $inputObj->SetNotify_url($config->GetNotifyUrl());
- }
-
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetSpbill_create_ip($config->GetIp());
- $inputObj->SetNonce_str(self::getNonceStr());
-
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
-
- public static function orderQuery($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/pay/orderquery";
-
- if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
- throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
-
- public static function closeOrder($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/pay/closeorder";
-
- if(!$inputObj->IsOut_trade_noSet()) {
- throw new WxPayException("订单查询接口中,out_trade_no必填!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
- public static function refund($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
-
- if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
- throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
- }else if(!$inputObj->IsOut_refund_noSet()){
- throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
- }else if(!$inputObj->IsTotal_feeSet()){
- throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
- }else if(!$inputObj->IsRefund_feeSet()){
- throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
- }else if(!$inputObj->IsOp_user_idSet()){
- throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
-
- public static function refundQuery($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/pay/refundquery";
-
- if(!$inputObj->IsOut_refund_noSet() &&
- !$inputObj->IsOut_trade_noSet() &&
- !$inputObj->IsTransaction_idSet() &&
- !$inputObj->IsRefund_idSet()) {
- throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
-
- public static function downloadBill($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/pay/downloadbill";
-
- if(!$inputObj->IsBill_dateSet()) {
- throw new WxPayException("对账单接口中,缺少必填参数bill_date!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- if(substr($response, 0 , 5) == "<xml>"){
- return "";
- }
- return $response;
- }
-
-
- public static function micropay($config, $inputObj, $timeOut = 10)
- {
- $url = "https://api.mch.weixin.qq.com/pay/micropay";
-
- if(!$inputObj->IsBodySet()) {
- throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!");
- } else if(!$inputObj->IsOut_trade_noSet()) {
- throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!");
- } else if(!$inputObj->IsTotal_feeSet()) {
- throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!");
- } else if(!$inputObj->IsAuth_codeSet()) {
- throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
- }
-
- $inputObj->SetSpbill_create_ip($config->GetIp());
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
-
- public static function reverse($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
-
- if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
- throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
- }
-
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
-
- public static function report($config, $inputObj, $timeOut = 1)
- {
- $url = "https://api.mch.weixin.qq.com/payitil/report";
-
- if(!$inputObj->IsInterface_urlSet()) {
- throw new WxPayException("接口URL,缺少必填参数interface_url!");
- } if(!$inputObj->IsReturn_codeSet()) {
- throw new WxPayException("返回状态码,缺少必填参数return_code!");
- } if(!$inputObj->IsResult_codeSet()) {
- throw new WxPayException("业务结果,缺少必填参数result_code!");
- } if(!$inputObj->IsUser_ipSet()) {
- throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
- } if(!$inputObj->IsExecute_time_Set()) {
- throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetUser_ip($_SERVER['REMOTE_ADDR']);
- $inputObj->SetTime(date("YmdHis"));
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- return $response;
- }
-
-
- public static function bizpayurl($config, $inputObj, $timeOut = 6)
- {
- if(!$inputObj->IsProduct_idSet()){
- throw new WxPayException("生成二维码,缺少必填参数product_id!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetTime_stamp(time());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
-
- return $inputObj->GetValues();
- }
-
-
- public static function shorturl($config, $inputObj, $timeOut = 6)
- {
- $url = "https://api.mch.weixin.qq.com/tools/shorturl";
-
- if(!$inputObj->IsLong_urlSet()) {
- throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!");
- }
- $inputObj->SetAppid($config->GetAppId());
- $inputObj->SetMch_id($config->GetMerchantId());
- $inputObj->SetNonce_str(self::getNonceStr());
-
- $inputObj->SetSign($config);
- $xml = $inputObj->ToXml();
-
- $startTimeStamp = self::getMillisecond();
- $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
- $result = WxPayResults::Init($config, $response);
- self::reportCostTime($config, $url, $startTimeStamp, $result);
-
- return $result;
- }
-
-
- public static function notify($config, $callback, &$msg)
- {
- $xml = file_get_contents("php://input");
- if (!$xml) {
-
- return false;
- }
-
- try {
-
- $result = WxPayNotifyResults::Init($config, $xml);
- } catch (WxPayException $e){
- $msg = $e->errorMessage();
- return false;
- }
-
- return call_user_func($callback, $result);
- }
-
-
- public static function getNonceStr($length = 32)
- {
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
- $str ="";
- for ( $i = 0; $i < $length; $i++ ) {
- $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
- }
- return $str;
- }
-
-
- public static function replyNotify($xml)
- {
- echo $xml;
- }
-
-
- private static function reportCostTime($config, $url, $startTimeStamp, $data)
- {
-
- $reportLevenl = $config->GetReportLevenl();
- if($reportLevenl == 0){
- return;
- }
-
- if($reportLevenl == 1 &&
- array_key_exists("return_code", $data) &&
- $data["return_code"] == "SUCCESS" &&
- array_key_exists("result_code", $data) &&
- $data["result_code"] == "SUCCESS")
- {
- return;
- }
-
-
- $endTimeStamp = self::getMillisecond();
- $objInput = new WxPayReport();
- $objInput->SetInterface_url($url);
- $objInput->SetExecute_time_($endTimeStamp - $startTimeStamp);
-
- if(array_key_exists("return_code", $data)){
- $objInput->SetReturn_code($data["return_code"]);
- }
-
- if(array_key_exists("return_msg", $data)){
- $objInput->SetReturn_msg($data["return_msg"]);
- }
-
- if(array_key_exists("result_code", $data)){
- $objInput->SetResult_code($data["result_code"]);
- }
-
- if(array_key_exists("err_code", $data)){
- $objInput->SetErr_code($data["err_code"]);
- }
-
- if(array_key_exists("err_code_des", $data)){
- $objInput->SetErr_code_des($data["err_code_des"]);
- }
-
- if(array_key_exists("out_trade_no", $data)){
- $objInput->SetOut_trade_no($data["out_trade_no"]);
- }
-
- if(array_key_exists("device_info", $data)){
- $objInput->SetDevice_info($data["device_info"]);
- }
-
- try{
- self::report($config, $objInput);
- } catch (WxPayException $e){
-
- }
- }
-
- private static function postXmlCurl($config, $xml, $url, $useCert = false, $second = 30)
- {
- $ch = curl_init();
- $curlVersion = curl_version();
- $ua = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
- .$config->GetMerchantId();
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
- $proxyHost = "0.0.0.0";
- $proxyPort = 0;
- $config->GetProxy($proxyHost, $proxyPort);
-
- if($proxyHost != "0.0.0.0" && $proxyPort != 0){
- curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
- curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
- }
- curl_setopt($ch,CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
- curl_setopt($ch,CURLOPT_USERAGENT, $ua);
-
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
- if($useCert == true){
-
-
-
- $sslCertPath = "";
- $sslKeyPath = "";
- $config->GetSSLCertPath($sslCertPath, $sslKeyPath);
- curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
- curl_setopt($ch,CURLOPT_SSLCERT, $sslCertPath);
- curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
- curl_setopt($ch,CURLOPT_SSLKEY, $sslKeyPath);
- }
-
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
-
- $data = curl_exec($ch);
-
- if($data){
- curl_close($ch);
- return $data;
- } else {
- $error = curl_errno($ch);
- curl_close($ch);
- throw new WxPayException("curl出错,错误码:$error");
- }
- }
-
-
- private static function getMillisecond()
- {
-
- $time = explode ( " ", microtime () );
- $time = $time[1] . ($time[0] * 1000);
- $time2 = explode( ".", $time );
- $time = $time2[0];
- return $time;
- }
- }
|