123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace huolib\pay\driver;
- use think\Exception;
- use think\Log;
- require_once EXTEND_PATH."pay/wxpay/WxPay.Api.php";
- require_once EXTEND_PATH."pay/wxpay/WxPay.Notify.php";
- class WxNotifyCallBack extends \WxPayNotify {
- 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' => '',
- ];
- function __construct() {
- $_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';
- }
-
- public function setConf($config = []) {
- $this->config['app_id'] = $config['app_id'];
- $this->config['mch_id'] = $config['mch_id'];
- $this->config['key'] = $config['key'];
- $this->config['app_secret'] = $config['app_secret'];
- }
-
- public function unifiedOrder(
- $order_id, $real_amount, $ip, $product_id, $product_desc, $notify_url, $trade_type = 'APP', $open_id = ''
- ) {
- if (empty($notify_url)) {
- $notify_url = get_val($this->config, 'notify_url', '');
- }
- if ('MPAPI' == $trade_type) {
- $trade_type = 'JSAPI';
- }
-
- $_input = new \WxPayUnifiedOrder();
- $_input->SetAppid($this->config['app_id']);
- $_input->SetMch_id($this->config['mch_id']);
- $_input->SetDevice_info($this->config['device_info']);
- $_input->SetBody($product_desc);
- $_input->SetAttach($product_id);
- $_input->SetOut_trade_no($order_id);
- $_input->SetTotal_fee((int)($real_amount * 100));
- $_input->SetSpbill_create_ip($ip);
- $_input->SetTime_start(date("YmdHis"));
- $_input->SetTime_expire(date("YmdHis", time() + 600));
- $_input->SetNotify_url($notify_url);
- $_input->SetTrade_type($trade_type);
- $_input->setKey($this->config['key']);
- if ('JSAPI' == $trade_type) {
- $_input->SetOpenid($open_id);
- }
- $result = \WxPayApi::unifiedOrder($_input, 6, $this->config);
- if ('FAIL' == $result['return_code']) {
- \think\Log::write($result, Log::ERROR);
- }
- if ('APP' == $trade_type) {
-
- return $this->setRData($result);
- } else if ('MWEB' == $trade_type) {
-
- return $result;
- } else if ('NATIVE' == $trade_type) {
-
- return $result;
- } else if ('JSAPI' == $trade_type) {
-
- return $this->setJsapiData($result);
- }
- return false;
- }
-
- public function setRData($result) {
- $_input = new \WxPayUnifiedOrder();
- $_token['appid'] = $result['appid'];
- $_token['partnerid'] = $result['mch_id'];
- $_token['prepayid'] = $result['prepay_id'];
- $_token['package'] = 'Sign=WXPay';
- $_token['noncestr'] = $result['nonce_str'];
- $_token['timestamp'] = time();
- $_input->setKey($this->config['key']);
- $_input->FromArray($_token);
- $_token['sign'] = $_input->SetSign();
- return json_encode($_token);
- }
-
- public function setJsapiData($result) {
- if (!array_key_exists("appid", $result)
- || !array_key_exists("prepay_id", $result)
- || empty($result['prepay_id'])) {
- return false;
- }
- $_jsapi_class = new \WxPayJsApiPay();
- $_jsapi_class->SetAppid($result["appid"]);
- $_ts = time();
- $_jsapi_class->setKey($this->config['key']);
- $_jsapi_class->SetTimeStamp("$_ts");
- $_jsapi_class->SetNonceStr(\WxPayApi::getNonceStr());
- $_jsapi_class->SetPackage("prepay_id=".$result['prepay_id']);
- $_jsapi_class->SetSignType("MD5");
- $_jsapi_class->SetPaySign($_jsapi_class->MakeSign());
- $_parameters = json_encode($_jsapi_class->GetValues());
- return $_parameters;
- }
-
- public function orderQuery($order_id, $transaction_id = '', $trade_type = '') {
-
- $_input = new \WxPayOrderQuery();
- $_input->SetOut_trade_no($order_id);
- if ('JSAPI' == $trade_type) {
- $this->config['app_id'] = $this->config['js_app_id'];
- unset($this->config['js_app_id']);
- $this->config['app_secret'] = $this->config['js_app_secret'];
- unset($this->config['js_app_secret']);
- } else {
- unset($this->config['js_app_id']);
- unset($this->config['js_app_secret']);
- }
- try {
- $_result = \WxPayApi::orderQuery($_input, 6, $this->config);
- if (isset($_result['trade_state'])) {
- return $_result['trade_state'];
- }
- } catch (\WxPayException $e) {
- return false;
- }
- return false;
- }
-
- public function NotifyProcess($data, &$msg) {
- if (!array_key_exists("openid", $data)
- || !array_key_exists("mch_id", $data)) {
- $msg = "回调数据异常";
- return false;
- }
-
- $_product_id = $data['attach'];
-
- $_out_trade_no = $data['out_trade_no'];
-
- $_trade_no = $data['transaction_id'];
-
- $_amount = number_format($data['total_fee'] / 100, 2, '.', '');
- try {
- $_class = $data['huo_order_class'];
- $_func = $data['huo_func'];
- if (class_exists($_class)) {
- $_order = new $_class();
- $_rs = $_order->$_func($_product_id, $_out_trade_no, $_trade_no, $_amount, 'wxpay');
- if (true == $_rs) {
- $msg = '支付通知成功';
- return true;
- }
- } else {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&order_class=".$_class
- ."&func=".$_func, LOG::ERROR
- );
- }
- $msg = '支付通知成功,内部处理失败';
- return true;
- } catch (Exception $_e) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&order_class=".$_class
- ."&func=".$_func."&err=".$_e->getMessage()."&errCode=".$_e->getCode(), LOG::ERROR
- );
- $msg = '支付通知成功,内部处理失败';
- return false;
- }
- }
- public function selfHandle($needSign = true, $order_class, $func) {
- $this->config['huo_order_class'] = $order_class;
- $this->config['huo_func'] = $func;
- $this->Handle($needSign, $this->config);
- }
- }
|