Yspay.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. class Yspay extends Core
  4. {
  5. public function __construct($config)
  6. {
  7. $project = Dever::project('pay');
  8. # 通知接口
  9. $config['notify'] = $this->url($config['type'], $config['id']);
  10. $set = explode('|', $config['mchid']);
  11. $config['mid'] = $set[0];
  12. $config['tid'] = $set[1];
  13. if (isset($set[2])) {
  14. $config['prefix'] = $set[2];
  15. }
  16. $set = explode('|', $config['key']);
  17. $config['sub_appid'] = $set[0];
  18. $config['sub_appsecret'] = $set[1];
  19. if (isset($set[2])) {
  20. $config['key'] = $set[2];
  21. }
  22. $this->config = $config;
  23. }
  24. /**
  25. * 通知
  26. */
  27. public function notify()
  28. {
  29. $input = file_get_contents("php://input");
  30. if ($input) {
  31. parse_str($input, $input);
  32. } else {
  33. $input = Dever::input();
  34. }
  35. $test = Dever::input('test');
  36. if ($test == 1) {
  37. $input = json_decode('{"msgType":"wx.notify","payTime":"2021-12-29 18:24:42","buyerCashPayAmt":"1","connectSys":"UNIONPAY","sign":"E82978003E0121B320D3520B1930FB4C2072B639126F3427351631F1D0A998CF","merName":"中食民安(北京)科技有限公司","mid":"89810007372106N","invoiceAmount":"1","settleDate":"2021-12-29","billFunds":"现金:1","buyerId":"otdJ_uD-zTNmDn7-u13oActXu8lA","mchntUuid":"2d9081bd7db8ad75017dbbe68981314f","tid":"DM098308","instMid":"MINIDEFAULT","receiptAmount":"1","couponAmount":"0","targetOrderId":"4200001344202112290882463274","signType":"SHA256","billFundsDesc":"现金支付0.01元。","subBuyerId":"oGlMn5e_vdYK8uAaCgyexarFKBrY","orderDesc":"中食民安(北京)科技有限公司","seqId":"25267273719N","merOrderId":"138K_G202112297347788956443487","targetSys":"WXPay","bankInfo":"OTHERS","totalAmount":"1","createTime":"2021-12-29 18:24:38","buyerPayAmount":"1","iB":"KRHn","notifyId":"d4122965-b772-4f8f-b5c4-f3d483da0962","subInst":"100000","status":"TRADE_SUCCESS"}', true);
  38. }
  39. $sign = $input['sign'];
  40. unset($input['sign']);
  41. ksort($input);
  42. $string = array();
  43. foreach ($input as $k => $v) {
  44. if (strstr($v, '&') || strstr($v, '@')) {
  45. $v = urlencode($v);
  46. }
  47. $string[] = $k . '=' . $v;
  48. }
  49. $string = implode('&', $string);
  50. $string .= $this->config['key'];
  51. if ($input['signType'] == 'SHA256') {
  52. $string = hash("sha256", $string);
  53. } else {
  54. $string = md5($string);
  55. }
  56. $string = strtoupper($string);
  57. $input['sign'] = $sign;
  58. $input['e_sign'] = $string;
  59. $this->log('支付回调-初始化', $input);
  60. if ($sign == $string) {
  61. if (isset($this->config['prefix']) && $this->config['prefix']) {
  62. $input['merOrderId'] = str_replace($this->config['prefix'], '', $input['merOrderId']);
  63. }
  64. $this->updateOrder($input['merOrderId'], $input['totalAmount']);
  65. echo 'SUCCESS';die;
  66. } else {
  67. echo 'FAILED';die;
  68. }
  69. }
  70. /**
  71. * 获取统一下单的基本信息
  72. */
  73. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false)
  74. {
  75. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
  76. if (isset($this->config['prefix']) && $this->config['prefix']) {
  77. $request['merOrderId'] = $this->config['prefix'] . $order_id;
  78. } else {
  79. $request['merOrderId'] = $order_id;
  80. }
  81. $request['mid'] = $this->config['mid'];
  82. $request['tid'] = $this->config['tid'];
  83. $request['instMid'] = 'MINIDEFAULT';
  84. $request['totalAmount'] = $cash * 100;
  85. //$request['totalAmount'] = 100;
  86. $request['subAppId'] = $this->config['sub_appid'];
  87. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  88. $request['expireTime'] = date("Y-m-d H:i:s", time() + $this->config['timeout']);
  89. $request['notifyUrl'] = $this->config['notify'];
  90. $request['tradeType'] = 'MINI';
  91. if ($other) {
  92. $request['originalAmount'] = $other['oprice'] * 100;
  93. $request['divisionFlag'] = true;
  94. # 平台分账金额
  95. $other['per'] = $other['per']/100;
  96. $request['platformAmount'] = $request['totalAmount'] * $other['per'];
  97. $request['subOrders'] = array();
  98. $request['subOrders']['mid'] = $other['mid'];
  99. if (isset($this->config['prefix']) && $this->config['prefix']) {
  100. $request['subOrders']['merOrderId'] = $this->config['prefix'] . $other['order_id'];
  101. } else {
  102. $request['subOrders']['merOrderId'] = $other['order_id'];
  103. }
  104. $request['subOrders']['totalAmount'] = $request['totalAmount'] - $request['platformAmount'];
  105. $request['subOrders'] = array($request['subOrders']);
  106. }
  107. if (!$openid) {
  108. # 测试的openid
  109. $request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  110. } else {
  111. $request['subOpenId'] = $openid;
  112. }
  113. if ($type == 2) {
  114. # 二维码支付
  115. $result = Base::get_pay_code($request, $this->config);
  116. if (isset($result['billQRCode'])) {
  117. $result['request'] = $request;
  118. $result['payMsg'] = $result['billQRCode'];
  119. if ($other) {
  120. $result['other'] = $other;
  121. }
  122. $this->updateOrderParam($order_id, $result);
  123. return $result['payMsg'];
  124. }
  125. } else {
  126. # 小程序支付
  127. $result = Base::pay($request, $this->config);
  128. if (isset($result['miniPayRequest'])) {
  129. $result['request'] = $request;
  130. $result['payMsg'] = $result['miniPayRequest'];
  131. unset($result['miniPayRequest']);
  132. if ($other) {
  133. $result['other'] = $other;
  134. }
  135. $this->updateOrderParam($order_id, $result);
  136. return $result['payMsg'];
  137. }
  138. }
  139. return false;
  140. }
  141. # 退款
  142. public function refund($order_id, $cash, $order, $refund_order_id = false)
  143. {
  144. if (isset($this->config['prefix']) && $this->config['prefix']) {
  145. $request['merOrderId'] = $this->config['prefix'] . $order_id;
  146. if ($refund_order_id) {
  147. $request['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
  148. }
  149. } else {
  150. $request['merOrderId'] = $order_id;
  151. if ($refund_order_id) {
  152. $request['refundOrderId'] = $refund_order_id;
  153. }
  154. }
  155. $request['mid'] = $this->config['mid'];
  156. $request['tid'] = $this->config['tid'];
  157. $request['instMid'] = 'MINIDEFAULT';
  158. if (isset($order['param']['targetOrderId'])) {
  159. $request['targetOrderId'] = $order['param']['targetOrderId'];
  160. }
  161. $request['subAppId'] = $this->config['appid'];
  162. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  163. $request['refundAmount'] = $cash * 100;
  164. if (isset($order['param']['other']) && $order['param']['other']) {
  165. $other = $order['param']['other'];
  166. $other['per'] = $other['per']/100;
  167. $request['platformAmount'] = $request['refundAmount'] * $other['per'];
  168. $request['subOrders'] = array();
  169. $request['subOrders']['mid'] = $other['mid'];
  170. if (isset($this->config['prefix']) && $this->config['prefix']) {
  171. $request['subOrders']['merOrderId'] = $this->config['prefix'] . $other['order_id'];
  172. if ($refund_order_id) {
  173. $request['subOrders']['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
  174. }
  175. } else {
  176. $request['subOrders']['merOrderId'] = $other['order_id'];
  177. if ($refund_order_id) {
  178. $request['subOrders']['refundOrderId'] = $refund_order_id;
  179. }
  180. }
  181. $request['subOrders']['totalAmount'] = $request['refundAmount'] - $request['platformAmount'];
  182. $request['subOrders'] = array($request['subOrders']);
  183. }
  184. $result = Base::refund($request, $this->config);
  185. if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
  186. return true;
  187. }
  188. return false;
  189. }
  190. # 查询
  191. public function query($order_id)
  192. {
  193. $request['merOrderId'] = $order_id;
  194. $request['mid'] = $this->config['mchid'];
  195. $request['tid'] = $this->config['key'];
  196. $request['instMid'] = 'MINIDEFAULT';
  197. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  198. $result = Base::refund($request, $this->config);
  199. return $result;
  200. }
  201. # 关闭订单
  202. public function close($order_id)
  203. {
  204. $request['merOrderId'] = $order_id;
  205. $request['mid'] = $this->config['mchid'];
  206. $request['tid'] = $this->config['key'];
  207. $request['instMid'] = 'MINIDEFAULT';
  208. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  209. $result = Base::close($request, $this->config);
  210. return $result;
  211. }
  212. /**
  213. * 获取二维码支付
  214. */
  215. public function qrcode($order, $refer)
  216. {
  217. $result['type'] = 'qrcode';
  218. $result['url'] = $order;
  219. return $result;
  220. }
  221. /**
  222. * 获取小程序支付
  223. */
  224. public function applet($order)
  225. {
  226. $result = array();
  227. if (isset($order['package'])) {
  228. $prepay_id = str_replace('prepay_id=', '', $order['package']);
  229. $result['time'] = $order['timeStamp'];
  230. $result['nonce_str'] = $order['nonceStr'];
  231. $result['prepay_id'] = $prepay_id;
  232. $result['sign_type'] = $order['signType'];
  233. $result['sign'] = $order['paySign'];
  234. $result['type'] = 'applet';
  235. }
  236. return $result;
  237. }
  238. /**
  239. * 获取页面支付
  240. */
  241. public function page($order, $refer)
  242. {
  243. $refer = urldecode($refer);
  244. $tools = new \JsApiPay($this->config);
  245. $info = $tools->GetJsApiParameters($order);
  246. $html = '<script type="text/javascript">
  247. function jsApiCall()
  248. {
  249. WeixinJSBridge.invoke(
  250. "getBrandWCPayRequest",
  251. '.$info.',
  252. function(res){
  253. //WeixinJSBridge.log(res.err_msg);
  254. if(res.err_msg == "get_brand_wcpay_request:ok")
  255. {
  256. location.href = "'.$refer.'";
  257. } else {
  258. alert(res.err_code+res.err_desc+res.err_msg);
  259. }
  260. }
  261. );
  262. }
  263. function callpay()
  264. {
  265. if (typeof WeixinJSBridge == "undefined"){
  266. if( document.addEventListener ){
  267. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  268. }else if (document.attachEvent){
  269. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  270. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  271. }
  272. }else{
  273. jsApiCall();
  274. }
  275. }
  276. callpay();
  277. </script>';
  278. return $html;
  279. }
  280. }
  281. class Base
  282. {
  283. static $test_host = 'https://test-api-open.chinaums.com/';
  284. static $host = 'https://api-mop.chinaums.com/';
  285. static $signMethod = 'SHA256'; //签名方式
  286. # 获取token
  287. static $token_url = 'v1/token/access';
  288. # 微信下单支付
  289. static $pay_wechat_url = 'v1/netpay/wx/unified-order';
  290. # 支付宝下单支付
  291. static $pay_alipay_url = 'v1/netpay/trade/create';
  292. # 云闪付下单支付
  293. static $pay_uac_url = 'v1/netpay/uac/mini-order';
  294. # 交易查询
  295. static $query_url = 'v1/netpay/query';
  296. # 退款
  297. static $refund_url = 'v1/netpay/refund';
  298. # 退款查询
  299. static $refund_query_url = 'v1/netpay/refund-query';
  300. # 订单关闭
  301. static $close_url = 'v1/netpay/close';
  302. # 二维码支付
  303. static $qrcode_url = 'v1/netpay/bills/get-qrcode';
  304. //===================== 支付相关 ==============================
  305. /**
  306. * 支付交易
  307. */
  308. static public function pay($param, $config)
  309. {
  310. $url = self::$pay_wechat_url;
  311. $result = self::get($url, $param, $config);
  312. return $result;
  313. }
  314. /**
  315. * 获取支付二维码
  316. */
  317. static public function get_pay_code($param, $config)
  318. {
  319. $url = self::$qrcode_url;
  320. $result = self::get($url, $param, $config);
  321. return $result;
  322. }
  323. /**
  324. * 支付撤销
  325. */
  326. static public function close()
  327. {
  328. $url = self::$close_url;
  329. $result = self::get($url, $param, $config);
  330. return $result;
  331. }
  332. /**
  333. * 交易退款
  334. */
  335. static public function refund($param, $config)
  336. {
  337. $url = self::$refund_url;
  338. $result = self::get($url, $param, $config);
  339. return $result;
  340. }
  341. /**
  342. * 交易查询
  343. */
  344. static public function query()
  345. {
  346. $url = self::$query_url;
  347. $result = self::get($url, $param, $config);
  348. return $result;
  349. }
  350. /**
  351. * 交易退款查询
  352. */
  353. static public function refundQuery()
  354. {
  355. $url = self::$refund_query_url;
  356. $result = self::get($url, $param, $config);
  357. return $result;
  358. }
  359. //====================== 获取调用凭证===========================
  360. /**
  361. * 通用调用凭证获取
  362. * 默认使用token方式,使用签名方式需要传入参数
  363. */
  364. static public function getAuth($type = 'token', $param, $config)
  365. {
  366. if ($type === 'token') {
  367. return self::getAccessTokenByToken($config);
  368. }
  369. return self::getAccessTokenBySig($param, $config);
  370. }
  371. /**
  372. * 方式一,OPEN-ACCESS-TOKEN方式
  373. */
  374. static function getAccessTokenByToken($config)
  375. {
  376. $param = [
  377. 'appId' => $config['appid'],
  378. 'timestamp' => date('YmdHis'),
  379. 'nonce' => md5(uniqid(microtime(true),true)),
  380. 'signMethod' => self::$signMethod,
  381. ];
  382. $param['signature'] = self::signature($param, $config['appsecret']);
  383. $result = self::get(self::$token_url, $param, $config);
  384. if (isset($result['errCode']) && isset($result['accessToken'])) {
  385. return 'OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken'];
  386. }
  387. return '';
  388. }
  389. /**
  390. * 方式二,OPEN-BODY-SIG方式
  391. */
  392. static function getAccessTokenBySig($data, $config)
  393. {
  394. $param = [
  395. 'AppId' => $config['appid'],
  396. 'Timestamp' => date('YmdHis'),
  397. 'Nonce' => md5(uniqid(microtime(true),true))
  398. ];
  399. return 'OPEN-BODY-SIG AppId="'.$param['AppId'].'", Timestamp="'.$param['Timestamp'].'", Nonce="'.$param['Nonce'].'", Signature="'.self::signature2($data, $param, $config['appsecret']).'"';
  400. }
  401. /**
  402. * 计算签名,方式一
  403. */
  404. static function signature($param, $appsecret)
  405. {
  406. return bin2hex(hash(self::$signMethod, $param['appId'].$param['timestamp'].$param['nonce'].$appsecret, true));
  407. }
  408. /**
  409. * 计算签名,方式二
  410. */
  411. static function signature2($data, $param, $appsecret)
  412. {
  413. $str = bin2hex(hash('sha256', Dever::json_encode($data), true));
  414. return base64_encode(hash_hmac('sha256', $param['AppId'].$param['Timestamp'].$param['Nonce'].$str, $appsecret, true));
  415. }
  416. /**
  417. * 获取信息
  418. */
  419. static function get($url, $param, $config)
  420. {
  421. $url = $config['box'] == 1 ? self::$host . $url : self::$test_host . $url;
  422. $header = array();
  423. $header['Authorization'] = self::getAuth('sig', $param, $config);
  424. $result = Dever::curl($url, $param, 'post', true, $header);
  425. if (strstr($result, '<html><head>')) {
  426. Dever::alert('系统错误');
  427. }
  428. $result = Dever::json_decode($result);
  429. if (isset($result['errCode'])) {
  430. if ($result['errCode'] == '0000' || $result['errCode'] == 'SUCCESS') {
  431. return $result;
  432. } elseif (isset($result['errInfo'])) {
  433. Dever::alert($result['errInfo']);
  434. } elseif (isset($result['errMsg'])) {
  435. Dever::alert($result['errMsg']);
  436. }
  437. } else {
  438. Dever::alert('系统错误');
  439. }
  440. return $result;
  441. }
  442. }