123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace huolib\pay\driver;
- use huolib\constant\PaywayConst;
- use huolib\pay\Driver;
- use think\Exception;
- use think\Log;
- class Wxpay extends Driver {
-
- public function __construct() {
- }
-
- public function clientPay() {
- $_trade_type = 'APP';
- $_class = new WxNotifyCallBack();
-
- try {
- $_token = $_class->unifiedOrder(
- $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
- $this->notify_url, $_trade_type
- );
- return $this->clientAjax(PaywayConst::PAYWAY_WXPAY, $_token, 1, 2, 1);
- } catch (\WxPayException $e) {
- return false;
- }
- }
-
- public function mpPay($config = []) {
- $_trade_type = 'MPAPI';
- $_class = new WxNotifyCallBack();
- if (!empty($config)) {
- $_class->setConf($config);
- }
-
- $_token = $_class->unifiedOrder(
- $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
- $this->notify_url, $_trade_type, $this->open_id
- );
- return $this->clientAjax(PaywayConst::PAYWAY_WXPAYMP, $_token, 1, 2, 1);
- }
-
- public function mobilePay($config = []) {
- $_trade_type = 'MWEB';
- $_class = new WxNotifyCallBack();
- if (!empty($config)) {
- $_class->setConf($config);
- }
- try {
-
- $_token = $_class->unifiedOrder(
- $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_name,
- $this->notify_url, $_trade_type
- );
- if (!empty($_token) && isset($_token['return_code']) && 'SUCCESS' == $_token['return_code']
- && !empty($_token['mweb_url'])
- ) {
- if (isset($_SERVER['HTTP_REFERER']) && strstr($_SERVER['HTTP_REFERER'], DOCDOMAIN) !== false) {
- $_pay_url = $_token['mweb_url'];
- $_ch = curl_init();
- curl_setopt($_ch, CURLOPT_URL, $_pay_url);
- if (strpos($_pay_url, 'https') === 0) {
- curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($_ch, CURLOPT_SSL_VERIFYHOST, 2);
- }
- $_headers['CLIENT-IP'] = $_SERVER['REMOTE_ADDR'];
- $_headers['X-FORWARDED-FOR'] = $_SERVER['REMOTE_ADDR'];
- $_header_arr = array();
- foreach ($_headers as $n => $v) {
- $_header_arr[] = $n.':'.$v;
- }
- curl_setopt($_ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($_ch, CURLOPT_HTTPHEADER, $_header_arr);
- curl_setopt($_ch, CURLOPT_CONNECTTIMEOUT, 5);
-
- curl_setopt($_ch, CURLOPT_REFERER, H5MSITE);
- curl_setopt($_ch, CURLOPT_TIMEOUT, 5);
- $_out_put = curl_exec($_ch);
- if ($_out_put === false) {
- echo curl_error($_ch);
- die;
- }
- curl_close($_ch);
-
- preg_match('/weixin(.*)"/', $_out_put, $_match);
- if (!isset($_match[1])) {
- throw new Exception('无法获取支付链接:'.$_out_put);
- }
- $_client_pay_url = 'weixin'.$_match[1];
- $_url = $_client_pay_url;
- } else {
- $_url = $_token['mweb_url'].'&redirect_url='.urlencode($this->return_url);
- }
-
- $_spare_url = $_token['mweb_url'].'&redirect_url='.urlencode($this->return_url);
- return $this->clientAjax(PaywayConst::PAYWAY_WXPAYH5, $_url, 1, 1, 1, $_spare_url);
- }
- return false;
- } catch (\WxPayException $e) {
- return false;
- }
- }
-
- public function pcPay($config = []) {
- $_trade_type = 'NATIVE';
- $_class = new WxNotifyCallBack();
- if (!empty($config)) {
- $_class->setConf($config);
- }
- try {
- $_token = $_class->unifiedOrder(
- $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
- $this->notify_url, $_trade_type
- );
- if (!empty($_token) && isset($_token['code_url'])) {
- $_code_url = $_token['code_url'];
- return $this->clientAjax(PaywayConst::PAYWAY_WXPAYQR, $_code_url, 1, 1, 1);
- }
- } catch (\WxPayException $e) {
- return false;
- }
- return false;
- }
-
- public function jsPay($config = []) {
- $_trade_type = 'JSAPI';
- $_class = new WxNotifyCallBack();
- if (!empty($config)) {
- $_class->setConf($config);
- }
- try {
-
- $_token = $_class->unifiedOrder(
- $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
- $this->notify_url, $_trade_type, $this->open_id
- );
- if (!empty($_token)) {
- return $this->clientAjax(PaywayConst::PAYWAY_WXPAYJS, $_token, 1, 1, 1);
- }
- } catch (\WxPayException $e) {
- return false;
- }
- return false;
- }
-
- public function walletNotify() {
- }
-
- public function gmNotify() {
- }
-
- public function notifyUrl($config = []) {
- $notify = new WxNotifyCallBack();
- if (!empty($config)) {
- $notify->setConf($config);
- }
- $notify->selfHandle(false, $this->order_class, $this->func);
- }
-
- public function returnUrl($order_id = '') {
- $_status = 1;
- return $this->clientAjax($this->payway, '', $_status, 1, 1);
- }
-
- public function orderQuery($order_id, $transaction_id, $ext = null) {
- $_config = get_val($ext, 'config', []);
- $_class = new WxNotifyCallBack();
- if (!empty($_config)) {
- $_class->setConf($_config);
- }
- $_rs = $_class->orderQuery($order_id, $transaction_id);
- return $_rs;
- }
- }
|