123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /**
- * Created by PhpStorm.
- * User: ws655
- * Date: 2020/7/23
- * Time: 16:15
- */
- namespace App\Support;
- use App\Models\SecretConfig;
- use Illuminate\Support\Arr;
- require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Config.php');
- require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Api.php');
- require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.JsApiPay.php');
- require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Data.php');
- require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Exception.php');
- require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Notify.php');
- class WxPayClient
- {
- private static $_instance;
- // 成功
- const TRADE_STATE_SUCCESS = 'SUCCESS'; // 支付成功
- // 等待
- const TRADE_STATE_NOTPAY = 'NOTPAY';
- public function __construct()
- {
- $config = SecretConfig::getConfig(SecretConfig::NAME_WEIXIN_PAY);
- \WxPayConfig::$appId = Arr::get($config, 'appId', '');
- \WxPayConfig::$mchId = Arr::get($config, 'mchId', '');
- \WxPayConfig::$key = Arr::get($config, 'key', '');
- \WxPayConfig::$appSecret = Arr::get($config, 'appSecret', '');
- }
- public static function getInstance()
- {
- if (empty(self::$_instance)) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- public function nativePay($product_id, $subject, $out_trade_no, $total_fee, $notify_url)
- {
- $inputObj = new \WxPayUnifiedOrder();
- $inputObj->SetProduct_id($product_id);
- $inputObj->SetBody($subject);
- $inputObj->SetOut_trade_no($out_trade_no);
- $inputObj->SetTotal_fee($total_fee);
- $inputObj->SetNotify_url($notify_url);
- $inputObj->SetTrade_type("NATIVE");
- return \WxPayApi::unifiedOrder($inputObj);
- }
- /**
- * 成功时返回,其他抛异常
- * @param $product_id
- * @param $subject
- * @param $out_trade_no
- * @param $total_fee
- * @param $notify_url
- * @return array
- * @throws \WxPayException
- */
- public function wapPay($product_id, $subject, $out_trade_no, $total_fee, $notify_url)
- {
- $inputObj = new \WxPayUnifiedOrder();
- $inputObj->SetProduct_id($product_id);
- $inputObj->SetBody($subject);
- $inputObj->SetOut_trade_no($out_trade_no);
- $inputObj->SetTotal_fee($total_fee);
- $inputObj->SetNotify_url($notify_url);
- $inputObj->SetTrade_type("MWEB");
- return \WxPayApi::unifiedOrder($inputObj);
- }
- /**
- * @param $product_id
- * @param $subject
- * @param $out_trade_no
- * @param $total_fee
- * @param $notify_url
- * @return \json数据,可直接填入js函数作为参数
- * @throws \WxPayException
- */
- public function jsApiPay($product_id, $subject, $out_trade_no, $total_fee, $notify_url)
- {
- $inputObj = new \WxPayUnifiedOrder();
- $inputObj->SetProduct_id($product_id);
- $inputObj->SetBody($subject);
- $inputObj->SetOut_trade_no($out_trade_no);
- $inputObj->SetTotal_fee($total_fee);
- $inputObj->SetNotify_url($notify_url);
- $inputObj->SetTrade_type("JSAPI");
- $weixinJsApi = WxPayClient::getInstance()->getJsApiPay();
- $openId = $weixinJsApi->GetOpenid();
- if (empty($openId)) {
- return;
- }
- $inputObj->SetOpenid($openId);
- // [
- // "appid" => "wx63b01c65be95bb89",
- // "mch_id" => "1505538321",
- // "nonce_str" => "2C2ofPMUegyc5k51",
- // "prepay_id" => "wx29134547410141279bb2a1b21781141400",
- // "result_code" => "SUCCESS",
- // "return_code" => "SUCCESS",
- // "return_msg" => "OK",
- // "sign" => "A26A8351C9124EE05F1BEE8E38247910",
- // "trade_type" => "JSAPI"
- // ];
- $signData = \WxPayApi::unifiedOrder($inputObj);
- return $weixinJsApi->GetJsApiParameters($signData);
- }
- /**
- * @param $out_trade_no
- * @return \成功时返回,其他抛异常
- * @throws \WxPayException
- */
- public function orderQuery($out_trade_no)
- {
- $inputObj = new \WxPayOrderQuery();
- $inputObj->SetOut_trade_no($out_trade_no);
- return \WxPayApi::orderQuery($inputObj);
- }
- /**
- * 支付结果通用通知
- * @return array|bool
- */
- public function notify()
- {
- //获取通知的数据
- $xml = file_get_contents('php://input');
- //如果返回成功则验证签名
- try {
- $result = \WxPayResults::Init($xml);
- EmailQueue::rPush('微信支付异步回调通知', json_encode($result), ['sai.wang@kuxuan-inc.com'], '微信支付通知');
- } catch (\WxPayException $e){
- $msg = $e->errorMessage();
- EmailQueue::rPush('微信支付异步回调异常', json_encode([$msg, $xml]), ['sai.wang@kuxuan-inc.com'], '微信支付通知');
- return false;
- }
- return $result;
- }
- public function getSignkey()
- {
- $inputObj = new \WxPaySignKey();
- return \WxPayApi::getSignkey($inputObj);
- }
- /**
- * @return \JsApiPay
- */
- public function getJsApiPay()
- {
- return new \JsApiPay();
- }
- }
|