Yspay.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. $trade_type = $this->getType($type);
  76. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
  77. if (isset($this->config['prefix']) && $this->config['prefix']) {
  78. $request['merOrderId'] = $this->config['prefix'] . '_' . $order_id;
  79. } else {
  80. $request['merOrderId'] = $order_id;
  81. }
  82. $request['mid'] = $this->config['mid'];
  83. $request['tid'] = $this->config['tid'];
  84. $request['instMid'] = 'MINIDEFAULT';
  85. $request['totalAmount'] = $cash * 100;
  86. //$request['totalAmount'] = 100;
  87. $request['subAppId'] = $this->config['sub_appid'];
  88. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  89. $request['expireTime'] = date("Y-m-d H:i:s", time() + $this->config['timeout']);
  90. $request['notifyUrl'] = $this->config['notify'];
  91. $request['tradeType'] = 'MINI';
  92. if ($other) {
  93. $request['originalAmount'] = $other['oprice'] * 100;
  94. $request['divisionFlag'] = true;
  95. # 平台分账金额
  96. $other['per'] = $other['per']/100;
  97. $request['platformAmount'] = $request['totalAmount'] * $other['per'];
  98. $request['subOrders'] = array();
  99. $request['subOrders']['mid'] = $other['mid'];
  100. if (isset($this->config['prefix']) && $this->config['prefix']) {
  101. $request['subOrders']['merOrderId'] = $this->config['prefix'] . '_' . $other['order_id'];
  102. } else {
  103. $request['subOrders']['merOrderId'] = $other['order_id'];
  104. }
  105. $request['subOrders']['totalAmount'] = $request['totalAmount'] - $request['platformAmount'];
  106. $request['subOrders'] = array($request['subOrders']);
  107. }
  108. if (!$openid) {
  109. # 测试的openid
  110. $request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  111. } else {
  112. $request['subOpenId'] = $openid;
  113. }
  114. $result = Base::pay($request, $this->config);
  115. if (isset($result['miniPayRequest'])) {
  116. $result['request'] = $request;
  117. $result['payMsg'] = $result['miniPayRequest'];
  118. unset($result['miniPayRequest']);
  119. if ($other) {
  120. $result['other'] = $other;
  121. }
  122. $this->updateOrderParam($order_id, $result);
  123. return $result['payMsg'];
  124. } else {
  125. return false;
  126. }
  127. }
  128. # 退款
  129. public function refund($order_id, $cash, $order, $refund_order_id = false)
  130. {
  131. if (isset($this->config['prefix']) && $this->config['prefix']) {
  132. $request['merOrderId'] = $this->config['prefix'] . '_' . $order_id;
  133. if ($refund_order_id) {
  134. $request['refundOrderId'] = $this->config['prefix'] . '_' . $refund_order_id;
  135. }
  136. } else {
  137. $request['merOrderId'] = $order_id;
  138. if ($refund_order_id) {
  139. $request['refundOrderId'] = $refund_order_id;
  140. }
  141. }
  142. $request['mid'] = $this->config['mid'];
  143. $request['tid'] = $this->config['tid'];
  144. $request['instMid'] = 'MINIDEFAULT';
  145. if (isset($order['param']['targetOrderId'])) {
  146. $request['targetOrderId'] = $order['param']['targetOrderId'];
  147. }
  148. $request['subAppId'] = $this->config['appid'];
  149. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  150. $request['refundAmount'] = $cash * 100;
  151. if (isset($order['param']['other']) && $order['param']['other']) {
  152. $other = $order['param']['other'];
  153. $other['per'] = $other['per']/100;
  154. $request['platformAmount'] = $request['refundAmount'] * $other['per'];
  155. $request['subOrders'] = array();
  156. $request['subOrders']['mid'] = $other['mid'];
  157. if (isset($this->config['prefix']) && $this->config['prefix']) {
  158. $request['subOrders']['merOrderId'] = $this->config['prefix'] . '_' . $other['order_id'];
  159. if ($refund_order_id) {
  160. $request['subOrders']['refundOrderId'] = $this->config['prefix'] . '_' . $refund_order_id;
  161. }
  162. } else {
  163. $request['subOrders']['merOrderId'] = $other['order_id'];
  164. if ($refund_order_id) {
  165. $request['subOrders']['refundOrderId'] = $refund_order_id;
  166. }
  167. }
  168. $request['subOrders']['totalAmount'] = $request['refundAmount'] - $request['platformAmount'];
  169. $request['subOrders'] = array($request['subOrders']);
  170. }
  171. $result = Base::refund($request, $this->config);
  172. if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
  173. return true;
  174. }
  175. return false;
  176. }
  177. # 查询
  178. public function query($order_id)
  179. {
  180. $request['merOrderId'] = $order_id;
  181. $request['mid'] = $this->config['mchid'];
  182. $request['tid'] = $this->config['key'];
  183. $request['instMid'] = 'MINIDEFAULT';
  184. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  185. $result = Base::refund($request, $this->config);
  186. return $result;
  187. }
  188. # 关闭订单
  189. public function close($order_id)
  190. {
  191. $request['merOrderId'] = $order_id;
  192. $request['mid'] = $this->config['mchid'];
  193. $request['tid'] = $this->config['key'];
  194. $request['instMid'] = 'MINIDEFAULT';
  195. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  196. $result = Base::close($request, $this->config);
  197. return $result;
  198. }
  199. /**
  200. * 获取二维码支付
  201. */
  202. public function qrcode($order, $refer)
  203. {
  204. $notify = new \NativePay();
  205. $result = $notify->GetPayUrl($order);
  206. $url = $result['code_url'];
  207. return $url;
  208. }
  209. /**
  210. * 获取小程序支付
  211. */
  212. public function applet($order)
  213. {
  214. $result = array();
  215. if (isset($order['package'])) {
  216. $prepay_id = str_replace('prepay_id=', '', $order['package']);
  217. $result['time'] = $order['timeStamp'];
  218. $result['nonce_str'] = $order['nonceStr'];
  219. $result['prepay_id'] = $prepay_id;
  220. $result['sign_type'] = $order['signType'];
  221. $result['sign'] = $order['paySign'];
  222. }
  223. return $result;
  224. }
  225. /**
  226. * 获取页面支付
  227. */
  228. public function page($order, $refer)
  229. {
  230. $refer = urldecode($refer);
  231. $tools = new \JsApiPay($this->config);
  232. $info = $tools->GetJsApiParameters($order);
  233. $html = '<script type="text/javascript">
  234. function jsApiCall()
  235. {
  236. WeixinJSBridge.invoke(
  237. "getBrandWCPayRequest",
  238. '.$info.',
  239. function(res){
  240. //WeixinJSBridge.log(res.err_msg);
  241. if(res.err_msg == "get_brand_wcpay_request:ok")
  242. {
  243. location.href = "'.$refer.'";
  244. } else {
  245. alert(res.err_code+res.err_desc+res.err_msg);
  246. }
  247. }
  248. );
  249. }
  250. function callpay()
  251. {
  252. if (typeof WeixinJSBridge == "undefined"){
  253. if( document.addEventListener ){
  254. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  255. }else if (document.attachEvent){
  256. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  257. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  258. }
  259. }else{
  260. jsApiCall();
  261. }
  262. }
  263. callpay();
  264. </script>';
  265. return $html;
  266. }
  267. private function getType($type)
  268. {
  269. switch ($type) {
  270. case 1:
  271. $type = 'JSAPI';
  272. break;
  273. case 2:
  274. $type = 'NATIVE';
  275. break;
  276. case 3:
  277. $type = 'APP';
  278. break;
  279. case 4:
  280. $type = 'MWEB';
  281. break;
  282. }
  283. return $type;
  284. }
  285. }
  286. class Base
  287. {
  288. static $test_host = 'https://test-api-open.chinaums.com/';
  289. static $host = 'https://api-mop.chinaums.com/';
  290. static $signMethod = 'SHA256'; //签名方式
  291. # 获取token
  292. static $token_url = 'v1/token/access';
  293. # 微信下单支付
  294. static $pay_wechat_url = 'v1/netpay/wx/unified-order';
  295. # 支付宝下单支付
  296. static $pay_alipay_url = 'v1/netpay/trade/create';
  297. # 云闪付下单支付
  298. static $pay_uac_url = 'v1/netpay/uac/mini-order';
  299. # 交易查询
  300. static $query_url = 'v1/netpay/query';
  301. # 退款
  302. static $refund_url = 'v1/netpay/refund';
  303. # 退款查询
  304. static $refund_query_url = 'v1/netpay/refund-query';
  305. # 订单关闭
  306. static $close_url = 'v1/netpay/close';
  307. //===================== 支付相关 ==============================
  308. /**
  309. * 支付交易
  310. */
  311. static public function pay($param, $config)
  312. {
  313. $url = self::$pay_wechat_url;
  314. $result = self::get($url, $param, $config);
  315. return $result;
  316. }
  317. /**
  318. * 支付撤销
  319. */
  320. static public function close()
  321. {
  322. $url = self::$close_url;
  323. $result = self::get($url, $param, $config);
  324. return $result;
  325. }
  326. /**
  327. * 交易退款
  328. */
  329. static public function refund($param, $config)
  330. {
  331. $url = self::$refund_url;
  332. $result = self::get($url, $param, $config);
  333. return $result;
  334. }
  335. /**
  336. * 交易查询
  337. */
  338. static public function query()
  339. {
  340. $url = self::$query_url;
  341. $result = self::get($url, $param, $config);
  342. return $result;
  343. }
  344. /**
  345. * 交易退款查询
  346. */
  347. static public function refundQuery()
  348. {
  349. $url = self::$refund_query_url;
  350. $result = self::get($url, $param, $config);
  351. return $result;
  352. }
  353. //====================== 获取调用凭证===========================
  354. /**
  355. * 通用调用凭证获取
  356. * 默认使用token方式,使用签名方式需要传入参数
  357. */
  358. static public function getAuth($type = 'token', $param, $config)
  359. {
  360. if ($type === 'token') {
  361. return self::getAccessTokenByToken($config);
  362. }
  363. return self::getAccessTokenBySig($param, $config);
  364. }
  365. /**
  366. * 方式一,OPEN-ACCESS-TOKEN方式
  367. */
  368. static function getAccessTokenByToken($config)
  369. {
  370. $param = [
  371. 'appId' => $config['appid'],
  372. 'timestamp' => date('YmdHis'),
  373. 'nonce' => md5(uniqid(microtime(true),true)),
  374. 'signMethod' => self::$signMethod,
  375. ];
  376. $param['signature'] = self::signature($param, $config['appsecret']);
  377. $result = self::get(self::$token_url, $param, $config);
  378. if (isset($result['errCode']) && isset($result['accessToken'])) {
  379. return 'OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken'];
  380. }
  381. return '';
  382. }
  383. /**
  384. * 方式二,OPEN-BODY-SIG方式
  385. */
  386. static function getAccessTokenBySig($data, $config)
  387. {
  388. $param = [
  389. 'AppId' => $config['appid'],
  390. 'Timestamp' => date('YmdHis'),
  391. 'Nonce' => md5(uniqid(microtime(true),true))
  392. ];
  393. return 'OPEN-BODY-SIG AppId="'.$param['AppId'].'", Timestamp="'.$param['Timestamp'].'", Nonce="'.$param['Nonce'].'", Signature="'.self::signature2($data, $param, $config['appsecret']).'"';
  394. }
  395. /**
  396. * 计算签名,方式一
  397. */
  398. static function signature($param, $appsecret)
  399. {
  400. return bin2hex(hash(self::$signMethod, $param['appId'].$param['timestamp'].$param['nonce'].$appsecret, true));
  401. }
  402. /**
  403. * 计算签名,方式二
  404. */
  405. static function signature2($data, $param, $appsecret)
  406. {
  407. $str = bin2hex(hash('sha256', Dever::json_encode($data), true));
  408. return base64_encode(hash_hmac('sha256', $param['AppId'].$param['Timestamp'].$param['Nonce'].$str, $appsecret, true));
  409. }
  410. /**
  411. * 获取信息
  412. */
  413. static function get($url, $param, $config)
  414. {
  415. $url = $config['box'] == 1 ? self::$host . $url : self::$test_host . $url;
  416. $header = array();
  417. $header['Authorization'] = self::getAuth('sig', $param, $config);
  418. $result = Dever::curl($url, $param, 'post', true, $header);
  419. if (strstr($result, '<html><head>')) {
  420. Dever::alert('系统错误');
  421. }
  422. $result = Dever::json_decode($result);
  423. if (isset($result['errCode'])) {
  424. if ($result['errCode'] == '0000' || $result['errCode'] == 'SUCCESS') {
  425. return $result;
  426. } elseif (isset($result['errInfo'])) {
  427. Dever::alert($result['errInfo']);
  428. } elseif (isset($result['errMsg'])) {
  429. Dever::alert($result['errMsg']);
  430. }
  431. } else {
  432. Dever::alert('系统错误');
  433. }
  434. return $result;
  435. }
  436. }