123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace huolib\withdraw\driver;
- use huolib\constant\SettleConst;
- use huolib\withdraw\Driver;
- use WxPayException;
- require_once EXTEND_PATH."pay/wxpay/WxPay.Data.php";
- class Wxpay extends Driver {
- private $config
- = [
- 'app_id' => '',
- 'mch_id' => '',
- 'key' => '',
- 'app_secret' => '',
- 'curl_proxy_host' => '0.0.0.0',
- 'curl_proxy_port' => '0',
- 'report_levenl' => '1',
- 'device_info' => 'WEB',
- 'sslcert_path' => '',
- 'sslkey_path' => '',
- ];
-
- public function __construct(array $config = []) {
- $_config = $config;
- if (empty($config)) {
- $_conf_file = GLOBAL_CONF_PATH.'extra/pay/wxpay/config.php';
- if (file_exists($_conf_file)) {
- $_config = include $_conf_file;
- } else {
- $_config = array();
- }
- $this->config = array_merge($this->config, $_config);
- $this->config['sslcert_path'] = GLOBAL_CONF_PATH.'extra/pay/wxpay/cert/apiclient_cert.pem';
- $this->config['sslkey_path'] = GLOBAL_CONF_PATH.'extra/pay/wxpay/cert/apiclient_key.pem';
- } else {
- $this->config = array_merge($this->config, $_config);
- }
- }
-
- public function withDraw() {
- $_re_data = [];
- $_input = new \WxPayTransfers();
- $_input->SetAppid($this->config['app_id']);
- $_input->SetMch_id($this->config['mch_id']);
- $_input->setKey($this->config['key']);
- $_input->SetDevice_info($this->config['device_info']);
- $_input->SetNonce_str(self::getNonceStr());
- $_input->SetOut_trade_no($this->order_id);
- $_input->setOpenId($this->open_id);
- $_input->setCheck_name($this->check_name);
- $_input->setRe_user_name($this->payee_real_name);
- $_input->setAmount(intval($this->real_amount * 100));
- $_input->setDesc($this->remark);
- $_ip = request()->ip();
- $_input->setSpbill_create_ip($_ip);
- $_url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
-
- $_input->SetSign();
- $_xml = $_input->ToXml();
- $response = self::postXmlCurl($_xml, $_url, true, 6, $this->config);
- $_wx_data_obj = new \WxPayDataBase();
- $_wx_data_obj->FromXml($response);
- $_result = $_wx_data_obj->GetValues();
- $_re_data['result'] = json_encode($_result);
- $_re_data['code'] = $_result['result_code'];
- $_re_data['msg'] = $_result['return_msg'];
- if (!empty($_result['return_code']) && 'SUCCESS' == $_result['return_code']) {
- if (!empty($_result['result_code']) && 'SUCCESS' == $_result['result_code']) {
- $_re_data['code'] = SettleConst::SETTLE_SUCCESS;
- } else {
-
- switch ($_result['err_code']) {
- case 'SEND_FAILED':
- case 'SYSTEMERROR':
- $_re_data['code'] = $_result['err_code'];
- $_re_data['msg'] = $_result['err_code_des'];
- $_rs = $this->orderQuery($this->order_id);
- $_re_data['query_result'] = $_rs['result'];
- if (isset($_rs['status'])
- && SettleConst::SETTLE_PAY_FAILED != $_rs['status']) {
- $_re_data['code'] = SettleConst::SETTLE_SUCCESS;
- }
- break;
- default:
- $_re_data['code'] = $_result['err_code'];
- $_re_data['msg'] = $_result['err_code_des'];
- }
- }
- }
- return $_re_data;
- }
-
- public function orderQuery($order_id, $ext = null) {
- $_input = new \WxPayGetTransfer();
- $_input->SetAppid($this->config['app_id']);
- $_input->SetMch_id($this->config['mch_id']);
- $_input->setKey($this->config['key']);
- $_input->SetNonce_str(self::getNonceStr());
- $_input->SetOut_trade_no($this->order_id);
- $_url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo";
-
- $_input->SetSign();
- $_xml = $_input->ToXml();
- $response = self::postXmlCurl($_xml, $_url, true, 6, $this->config);
- $_wx_data_obj = new \WxPayDataBase();
- $_wx_data_obj->FromXml($response);
- $_result = $_wx_data_obj->GetValues();
- $_rdata = [
- 'code' => $_result['return_code'],
- 'msg' => $_result['return_msg'],
- 'result' => json_encode($_result)
- ];
- if (!empty($_result['return_code']) && 'SUCCESS' == $_result['return_code']) {
- $_rdata['code'] = $_result['err_code'];
- $_rdata['msg'] = $_result['err_code_des'];
- if (!empty($_result['result_code']) && 'SUCCESS' == $_result['result_code']) {
- switch ($_rdata['status']) {
- case 'SUCCESS':
- $_rdata['status'] = SettleConst::SETTLE_PAY_SUCCESS;
- break;
- case 'PROCESSING':
- $_rdata['status'] = SettleConst::SETTLE_PAY_PROCESSING;
- break;
- default:
- $_rdata['status'] = SettleConst::SETTLE_PAY_FAILED;
- }
- $_rdata['reason'] = $_result['reason'];
- }
- }
- return $_rdata;
- }
-
- 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;
- }
-
- private static function getMillisecond() {
-
- $time = explode(" ", microtime());
- $time = $time[1].($time[0] * 1000);
- $time2 = explode(".", $time);
- $time = $time2[0];
- return $time;
- }
-
- private static function postXmlCurl($xml, $url, $useCert = false, $second = 30, array $config) {
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
-
- if ($config['curl_proxy_host'] != "0.0.0.0"
- && $config['curl_proxy_port'] != 0
- ) {
- curl_setopt($ch, CURLOPT_PROXY, $config['curl_proxy_host']);
- curl_setopt($ch, CURLOPT_PROXYPORT, $config['curl_proxy_port']);
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
-
- curl_setopt($ch, CURLOPT_HEADER, false);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if ($useCert == true) {
-
-
- curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
- curl_setopt($ch, CURLOPT_SSLCERT, $config['sslcert_path']);
- curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
- curl_setopt($ch, CURLOPT_SSLKEY, $config['sslkey_path']);
- }
-
- 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");
- }
- }
- }
|