新版订单消耗系统

WxPayClient.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ws655
  5. * Date: 2020/7/23
  6. * Time: 16:15
  7. */
  8. namespace App\Support;
  9. use App\Models\SecretConfig;
  10. use Illuminate\Support\Arr;
  11. require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Config.php');
  12. require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Api.php');
  13. require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.JsApiPay.php');
  14. require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Data.php');
  15. require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Exception.php');
  16. require_once(dirname(__DIR__) . '/libs/wxpay/lib/WxPay.Notify.php');
  17. class WxPayClient
  18. {
  19. private static $_instance;
  20. // 成功
  21. const TRADE_STATE_SUCCESS = 'SUCCESS'; // 支付成功
  22. // 等待
  23. const TRADE_STATE_NOTPAY = 'NOTPAY';
  24. public function __construct()
  25. {
  26. $config = SecretConfig::getConfig(SecretConfig::NAME_WEIXIN_PAY);
  27. \WxPayConfig::$appId = Arr::get($config, 'appId', '');
  28. \WxPayConfig::$mchId = Arr::get($config, 'mchId', '');
  29. \WxPayConfig::$key = Arr::get($config, 'key', '');
  30. \WxPayConfig::$appSecret = Arr::get($config, 'appSecret', '');
  31. }
  32. public static function getInstance()
  33. {
  34. if (empty(self::$_instance)) {
  35. self::$_instance = new self();
  36. }
  37. return self::$_instance;
  38. }
  39. public function nativePay($product_id, $subject, $out_trade_no, $total_fee, $notify_url)
  40. {
  41. $inputObj = new \WxPayUnifiedOrder();
  42. $inputObj->SetProduct_id($product_id);
  43. $inputObj->SetBody($subject);
  44. $inputObj->SetOut_trade_no($out_trade_no);
  45. $inputObj->SetTotal_fee($total_fee);
  46. $inputObj->SetNotify_url($notify_url);
  47. $inputObj->SetTrade_type("NATIVE");
  48. return \WxPayApi::unifiedOrder($inputObj);
  49. }
  50. /**
  51. * 成功时返回,其他抛异常
  52. * @param $product_id
  53. * @param $subject
  54. * @param $out_trade_no
  55. * @param $total_fee
  56. * @param $notify_url
  57. * @return array
  58. * @throws \WxPayException
  59. */
  60. public function wapPay($product_id, $subject, $out_trade_no, $total_fee, $notify_url)
  61. {
  62. $inputObj = new \WxPayUnifiedOrder();
  63. $inputObj->SetProduct_id($product_id);
  64. $inputObj->SetBody($subject);
  65. $inputObj->SetOut_trade_no($out_trade_no);
  66. $inputObj->SetTotal_fee($total_fee);
  67. $inputObj->SetNotify_url($notify_url);
  68. $inputObj->SetTrade_type("MWEB");
  69. return \WxPayApi::unifiedOrder($inputObj);
  70. }
  71. /**
  72. * @param $product_id
  73. * @param $subject
  74. * @param $out_trade_no
  75. * @param $total_fee
  76. * @param $notify_url
  77. * @return \json数据,可直接填入js函数作为参数
  78. * @throws \WxPayException
  79. */
  80. public function jsApiPay($product_id, $subject, $out_trade_no, $total_fee, $notify_url)
  81. {
  82. $inputObj = new \WxPayUnifiedOrder();
  83. $inputObj->SetProduct_id($product_id);
  84. $inputObj->SetBody($subject);
  85. $inputObj->SetOut_trade_no($out_trade_no);
  86. $inputObj->SetTotal_fee($total_fee);
  87. $inputObj->SetNotify_url($notify_url);
  88. $inputObj->SetTrade_type("JSAPI");
  89. $weixinJsApi = WxPayClient::getInstance()->getJsApiPay();
  90. $openId = $weixinJsApi->GetOpenid();
  91. if (empty($openId)) {
  92. return;
  93. }
  94. $inputObj->SetOpenid($openId);
  95. // [
  96. // "appid" => "wx63b01c65be95bb89",
  97. // "mch_id" => "1505538321",
  98. // "nonce_str" => "2C2ofPMUegyc5k51",
  99. // "prepay_id" => "wx29134547410141279bb2a1b21781141400",
  100. // "result_code" => "SUCCESS",
  101. // "return_code" => "SUCCESS",
  102. // "return_msg" => "OK",
  103. // "sign" => "A26A8351C9124EE05F1BEE8E38247910",
  104. // "trade_type" => "JSAPI"
  105. // ];
  106. $signData = \WxPayApi::unifiedOrder($inputObj);
  107. return $weixinJsApi->GetJsApiParameters($signData);
  108. }
  109. /**
  110. * @param $out_trade_no
  111. * @return \成功时返回,其他抛异常
  112. * @throws \WxPayException
  113. */
  114. public function orderQuery($out_trade_no)
  115. {
  116. $inputObj = new \WxPayOrderQuery();
  117. $inputObj->SetOut_trade_no($out_trade_no);
  118. return \WxPayApi::orderQuery($inputObj);
  119. }
  120. /**
  121. * 支付结果通用通知
  122. * @return array|bool
  123. */
  124. public function notify()
  125. {
  126. //获取通知的数据
  127. $xml = file_get_contents('php://input');
  128. //如果返回成功则验证签名
  129. try {
  130. $result = \WxPayResults::Init($xml);
  131. EmailQueue::rPush('微信支付异步回调通知', json_encode($result), ['sai.wang@kuxuan-inc.com'], '微信支付通知');
  132. } catch (\WxPayException $e){
  133. $msg = $e->errorMessage();
  134. EmailQueue::rPush('微信支付异步回调异常', json_encode([$msg, $xml]), ['sai.wang@kuxuan-inc.com'], '微信支付通知');
  135. return false;
  136. }
  137. return $result;
  138. }
  139. public function getSignkey()
  140. {
  141. $inputObj = new \WxPaySignKey();
  142. return \WxPayApi::getSignkey($inputObj);
  143. }
  144. /**
  145. * @return \JsApiPay
  146. */
  147. public function getJsApiPay()
  148. {
  149. return new \JsApiPay();
  150. }
  151. }