Supay.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use app\common\model\Wallet;
  7. use think\Exception;
  8. /**
  9. * 苏宁支付h5拉起
  10. */
  11. class Supay extends Api
  12. {
  13. protected $noNeedLogin = ['testpay','notify'];
  14. protected $noNeedRight = ['*'];
  15. public function vip_recharge()
  16. {
  17. Db::startTrans();
  18. try {
  19. $rc_id = input('rc_id',0);
  20. $pay_type = input('pay_type','wechat');
  21. $uid = $this->auth->id;
  22. if(!$rc_id){
  23. throw new Exception('请选择会员套餐');
  24. }
  25. //赋值money
  26. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  27. $money = $recharge_config['money'];
  28. if($money <= 0) {
  29. throw new Exception('支付金额必须大于0');
  30. }
  31. if($money > 10000){
  32. throw new Exception('支付金额太大');
  33. }
  34. //会员等级冲突
  35. //当前是会员,但是却要向下级续费,直接提示报错
  36. //修改等级,向上立刻改,向下不允许
  37. $wallet_info = model('wallet')->getWallet($this->auth->id);
  38. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  39. throw new Exception('当前会员没有过期,不能续费');
  40. }
  41. //创建订单
  42. $data = [];
  43. $data['status'] = 0;
  44. $pay_no = createUniqueNo('V',$uid);
  45. $data['pay_no'] = $pay_no;
  46. $data['money'] = $money;
  47. $data['payment_class'] = $pay_type;
  48. $data['user_id'] = $uid;
  49. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  50. $data['memo'] = '充值会员支付';
  51. $data['createtime'] = time();
  52. //$data['payment'] = 'miniapp';
  53. $data['payment'] = 'app';
  54. $orderid = Db::name('pay_order')->insertGetId($data);
  55. //创建回调
  56. $even_data = [];
  57. $even_data['event'] = 'success';
  58. $even_data['class'] = 'app\common\model\Recharge';
  59. $even_data['method'] = 'vippaysucc';
  60. $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
  61. $even_data['pay_no'] = $pay_no;
  62. Db::name('pay_event')->insertGetId($even_data);
  63. $res[] = '';
  64. if ($pay_type == 'wechat') {
  65. //$money = 0.01;
  66. $sandpay = new \app\common\library\SuningPay();
  67. $sandpayParams = [
  68. 'order_no' => $pay_no,
  69. 'goods_name' => '充值vip支付',
  70. 'money' => $money,
  71. 'type' => 'vip',
  72. ];
  73. $sandRes = $sandpay->wechath5($sandpayParams);
  74. if ($sandRes['status'] != 1) {
  75. throw new Exception('失败');
  76. }
  77. $res = json_decode($sandRes['data']['params'],true);
  78. $url = 'https://h5-min-pay-1gczed24bbbe3db8-1317709175.tcloudbaseapp.com/suning-pay.html';
  79. $ress['url'] = $url.'?orderId='.$res['tradeOrderId'];
  80. } else {
  81. //下单
  82. $params = [
  83. 'type' => $pay_type,
  84. 'orderid' => $pay_no,
  85. 'title' => $data['memo'],
  86. 'amount' => $data['money'],
  87. //'amount' => 0.01,
  88. 'method' => 'app',
  89. 'notifyurl' => 'http://app.zhiyinvip001.com/notify.php/paytype/'.$pay_type,
  90. 'returnurl' => '',
  91. ];
  92. $res = Service::submitOrder($params);
  93. }
  94. Db::commit();
  95. $this->success('success',$ress);
  96. } catch (Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. //充值金币 创建订单
  102. public function gold_recharge()
  103. {
  104. Db::startTrans();
  105. try {
  106. $rc_id = input_post('rc_id',0);
  107. $pay_type = 'wechat';
  108. $freemoney = input_post('freemoney', 0, 'intval');
  109. $uid = $this->auth->id;
  110. if(!$rc_id && !$freemoney){
  111. throw new Exception('请选择或填写充值金额');
  112. }
  113. if (!in_array($pay_type,['wechat','alipay'])) {
  114. throw new Exception('错误的支付类型');
  115. }
  116. //赋值money
  117. if($rc_id){
  118. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  119. $money = $recharge_config ? $recharge_config['money']: 0;
  120. $gold = $recharge_config ? $recharge_config['gold'] : 0;
  121. $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
  122. $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
  123. $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
  124. }
  125. //自由输入覆盖
  126. if(!empty($freemoney)){
  127. $rc_id = 0;
  128. $money = floatval($freemoney);
  129. $bili = config('site.money_to_gold') ?: 10;
  130. $gold = bcmul($money,$bili,0);
  131. $first_gold = 0;
  132. $first_vipdays = 0;
  133. $vip_gold = 0;
  134. }
  135. if($money <= 0) {
  136. throw new Exception('支付金额必须大于0');
  137. }
  138. if($money > 10000){
  139. throw new Exception('支付金额太大');
  140. }
  141. //查询是不是会员,若不是则不赠送金币
  142. $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  143. if ($vip_endtime < time()) {
  144. $vip_gold = 0;
  145. }
  146. //创建订单
  147. $data = [];
  148. $data['status'] = 0;
  149. $pay_no = createUniqueNo('P',$uid);
  150. $data['pay_no'] = $pay_no;
  151. $data['money'] = $money;
  152. $data['payment_class'] = $pay_type;
  153. $data['user_id'] = $uid;
  154. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  155. $data['memo'] = '充值金币支付';
  156. $data['createtime'] = time();
  157. $data['payment'] = 'app';
  158. $orderid = Db::name('pay_order')->insertGetId($data);
  159. //创建回调
  160. $even_data = [];
  161. $even_data['event'] = 'success';
  162. $even_data['class'] = 'app\common\model\Recharge';
  163. $even_data['method'] = 'goldpaysucc';
  164. $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
  165. $even_data['pay_no'] = $pay_no;
  166. Db::name('pay_event')->insertGetId($even_data);
  167. //$money = 0.01;
  168. $suningpay = new \app\common\library\SuningPay();
  169. $suningpayParams = [
  170. 'order_no' => $pay_no,
  171. 'goods_name' => '充值金币支付',
  172. 'money' => $money,
  173. 'type' => 'gold',
  174. ];
  175. $suningRes = $suningpay->wechath5($suningpayParams);
  176. if ($suningRes['status'] != 1) {
  177. throw new Exception('失败');
  178. }
  179. $res = json_decode($suningRes['data']['params'],true);
  180. $url = 'https://h5-min-pay-1gczed24bbbe3db8-1317709175.tcloudbaseapp.com/suning-pay.html';
  181. $ress['url'] = $url.'?orderId='.$res['tradeOrderId'];
  182. Db::commit();
  183. $this->success('成功', $ress);
  184. } catch (Exception $e) {
  185. Db::rollback();
  186. $this->error($e->getMessage());
  187. }
  188. }
  189. /**
  190. * 支付成功
  191. */
  192. public function notify()
  193. {
  194. //$input_post_data = json_encode($_POST);
  195. //filePut('[wallet][paySucc]充值参数 recharge money post'.$input_post_data);
  196. //$postData = isset($_POST['data']) ? $_POST['data'] : [];
  197. //$sign = isset($_POST['sign']) ? $_POST['sign'] : '';
  198. $postData=file_get_contents('php://input');
  199. $postData = urldecode($postData);
  200. $arr = explode('&', $postData);
  201. $nowArr = array();
  202. foreach($arr as $v) {
  203. $kv = explode('=', $v);
  204. $nowArr[$kv[0]] = $kv[1];
  205. }
  206. if($nowArr['responseCode'] == 'SJ0000' && $nowArr['transactStatus'] == '00')
  207. {
  208. $data = $this->verification($postData);
  209. if($data == 1)
  210. { //验签名通过
  211. //查询订单信息
  212. $PayResult = Db::name('pay_order');
  213. $where['pay_no'] = $nowArr['orderId'];
  214. $order_info = $PayResult->where($where)->find();
  215. if(!$order_info)
  216. {
  217. filePut('[wallet][paySucc]充值入账更新余额失败 recharge money fail 订单数据库检索不到'.$nowArr['orderId'],'suningno.txt');
  218. exit;
  219. }
  220. if ($order_info['status'] == 1 || $order_info['status'] == 2)
  221. {
  222. filePut('[wallet][paySucc]充值入账更新余额失败 recharge money fail status已更新过'.$order_info['status'].'___'.$nowArr['orderId'],'suningno.txt');
  223. echo 'true';exit;
  224. }
  225. $orderCode = $nowArr['orderId'];
  226. $extendType = '';
  227. //区分vip 还是 冲金币
  228. $is_g = stripos($orderCode,'P');
  229. if($is_g !== false)
  230. {
  231. $extendType = 'gold';
  232. }else{
  233. $is_v = stripos($orderCode,'V');
  234. if($is_v !== false)
  235. {
  236. $extendType = 'vip';
  237. }
  238. }
  239. //status已更新过
  240. $_data['status'] = 2;
  241. $result = $PayResult->where($where)->setField($_data);
  242. if ($result || 1)
  243. {
  244. //你可以在此编写订单逻辑
  245. $payEventWhere['pay_no'] = $orderCode;
  246. $payEvent = Db::name('pay_event')->where($payEventWhere)->find();
  247. $args = isset($payEvent['args']) ? $payEvent['args'] : '';
  248. $args = json_decode($args,true);
  249. $rechargeM = new \app\common\model\Recharge();
  250. if ($extendType == 'gold') {
  251. $payRes = $rechargeM->goldpaysucc($orderCode,$args);
  252. $payTypeStr = '充值';
  253. } elseif($extendType == 'vip') {
  254. $payRes = $rechargeM->vippaysucc($orderCode,$args);
  255. $payTypeStr = 'vip';
  256. } else {
  257. $payRes = false;
  258. $payTypeStr = '未知支付类型';
  259. }
  260. if(!$payRes)
  261. {
  262. filePut('[wallet][paySucc]'.$payTypeStr.'更新失败请查看问题'.$orderCode,'suningno.txt');
  263. exit;
  264. }
  265. filePut('[wallet][paySucc]回调成功'.$orderCode,'suningno.txt');
  266. echo "true";exit;
  267. }
  268. filePut('[wallet][paySucc]充值入账更新余额失败 更新状态2不成功'.$orderCode."\n".' data:'.$postData."\n",'suningno.txt');
  269. exit;
  270. }
  271. }
  272. else
  273. {
  274. filePut(' recharge gold fail 用户交易操作失败'.$postData,'suningno.txt');
  275. echo '支付异常';exit;
  276. }
  277. filePut('[wallet][paySucc]充值入账更新余额失败 验证签名不通过'.$orderCode."\n".' data:'.$postData."\n",'suningno.txt');
  278. echo 'Fail';exit;
  279. }
  280. protected function verification($str)
  281. {
  282. if($str != '') {
  283. $arr = explode('&', $str);
  284. $nowArr = array();
  285. foreach($arr as $v) {
  286. $kv = explode('=', $v);
  287. $nowArr[$kv[0]] = $kv[1];
  288. }
  289. ksort($nowArr);
  290. $newstr = '';
  291. foreach($nowArr as $key => $value) {
  292. if($value == '' || $key == 'signature' || $key== 'signAlgorithm') {
  293. continue;
  294. }
  295. if($key == 'version')
  296. {
  297. $newstr .= $key.'='.$value;
  298. } else
  299. {
  300. $newstr .= $key.'='.$value.'&';
  301. }
  302. }
  303. // print_r($nowArr);
  304. //var_dump($newstr);
  305. $sign = base64_decode($nowArr['signature']);
  306. // exit;
  307. $newstr = strtoupper(md5($newstr));
  308. // var_dump($newstr);
  309. $pubfile = file_get_contents(APP_PATH.'common/library/suning/publicnew.pem');
  310. $ret = openssl_verify($newstr,$sign,$pubfile);
  311. return $ret;
  312. }
  313. }
  314. }