Recharge.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\library\Shande;
  4. use app\common\library\ShandeRapid;
  5. use app\common\library\Moyun;
  6. use think\Db;
  7. use think\Exception;
  8. use app\common\library\LianLianPay;
  9. /**
  10. * @title 余额充值
  11. * @controller Recharge
  12. * @group base
  13. */
  14. class Recharge extends Base
  15. {
  16. public function initialize(){
  17. parent::initialize();
  18. parent::check_login();
  19. }
  20. /**
  21. * @title 余额充值
  22. * @desc 余额充值
  23. * @author Qc
  24. * @url /api/Recharge/voucher
  25. * @method POST
  26. * @header name:Authorization require:1 desc:Token
  27. * @param name:money type:int require:1 default:1 desc:充值金额
  28. * @param name:pay_type type:int require:1 default:sd_rapid desc:sd:杉德h5,sd_rapid:杉德快捷支付,moyun:墨云[支付宝],ll:连连支付
  29. * @param name:from type:int require:1 default1 desc:1:h5,2:app[Android],3:app[ios]
  30. * @return name:order_no type:string default:0 desc:支付单号
  31. * @return name:id type:int default:0 desc:支付订单id
  32. * @return name:extend type:int default:0 desc:用户id
  33. * @return name:pay type:array default:0 desc:支付信息
  34. * @return name:pay.body type:array default:0 desc:支付信息
  35. * @return name:pay.totalAmount type:string default:0 desc:支付金额【000000010000是100元】
  36. * @return name:pay.orderCode type:string default:0 desc:支付单号
  37. * @return name:pay.credential type:string default:0 desc:正书
  38. */
  39. public function voucher()
  40. {
  41. // if($this->uid != 73) $this->error('暂未开通');
  42. if (redisSetNx($this->request->action().$this->uid,3)) {
  43. $money = input('post.money',0);
  44. $pay_type = input('post.pay_type','sd_rapid');
  45. $from = input('post.from',1);
  46. $typecode = $from == 1 ? 4:5;
  47. if($money <=0 ) $this->error('充值金额有误!');
  48. $money = bcadd($money,0,2);
  49. if($money < 10)$this->error('最低充值10元!');
  50. $user = Db::name('store_member')->where('id',$this->uid)->find();
  51. if(!$user['true_name'] || !$user['id_card']) $this->error('请实名认证,在进行订单支付');
  52. Db::startTrans();
  53. try{
  54. $voucher_order = [
  55. 'mid' => $this->uid ,
  56. 'total_price' => $money,
  57. 'order_no' =>get_order_sn(),
  58. 'pay_no' =>get_order_sn(),
  59. 'create_at' => date("Y-m-d H:i:s"),
  60. 'pay_type' => $pay_type,
  61. 'ext' => json_encode(['name'=>$user['true_name'],'phone'=>$user['phone'],'id_card'=>$user['id_card']])
  62. ];
  63. $order_id = Db::table('store_recharge_order')->insertGetId($voucher_order);
  64. $notify_url = 'https://'.$_SERVER['HTTP_HOST'] . '/api/Pay/walletNotify';
  65. switch ($pay_type)
  66. {
  67. case 'sd':
  68. $client = new Shande();
  69. $total_fee = $money*100;
  70. $lenth = strlen($total_fee);
  71. $total_fee = get0number($lenth).$total_fee;
  72. $result = $client->orderPay($voucher_order['order_no'],$total_fee,'订单充值',$notify_url,'https://'.$_SERVER['HTTP_HOST'].'/h5/pages/mine/mine',$user['bank_num']);
  73. $retrun_data['order_no'] = $voucher_order['order_no'];
  74. $retrun_data['extend'] = $this->uid;
  75. $retrun_data['pay'] = json_decode($result['data'],true);
  76. if( $retrun_data['pay']['head']['respCode'] != '00000')$this->error($retrun_data['pay']['head']['respMsg'].'请重新下单');
  77. break;
  78. case 'sd_rapid':
  79. $rapid = new ShandeRapid();
  80. $return_url ='https://'.$_SERVER['HTTP_HOST']."/h5/pages/mine/mine?id={$order_id}&orderNo={$voucher_order['order_no']}";//回调地址
  81. $pay_extra = json_encode(['userId'=>"$this->uid",'userName'=>$user['true_name'],'idCard'=>$user['id_card']]);
  82. $pay_url = $rapid->payOrder($voucher_order['order_no'],$money,$notify_url,$return_url,$pay_extra,'余额充值');
  83. parse_str(parse_url($pay_url)['query'],$pay_query);
  84. $retrun_data['parse_url'] = parse_url($pay_url)['query'];
  85. $retrun_data['order_no'] = $voucher_order['order_no'];
  86. $retrun_data['extend'] = $this->uid;
  87. $retrun_data['pay_url'] = $pay_url;
  88. $retrun_data['pay_query'] = $pay_query;
  89. break;
  90. case 'moyun':
  91. $obj = new Moyun();
  92. $notifyUrl = 'https://'.$_SERVER['HTTP_HOST'].'/api/Pay/moYunWalletNotify';//回调地址
  93. $return_url ='https://'.$_SERVER['HTTP_HOST']."/h5/pages/mine/mine?id={$order_id}&orderNo={$voucher_order['order_no']}";//回调地址
  94. $res = $obj->alipay('余额充值',$voucher_order['order_no'],$money,'',$notifyUrl,$return_url,$typecode);
  95. // if($this->uid == 73) var_dump($retrun_data);
  96. if(!isset($res['data']) || !$res['data']) throw new Exception($res['msg']);
  97. $retrun_data = $res['data'];
  98. break;
  99. case 'll':
  100. $notify_url = 'https://'.$_SERVER['HTTP_HOST'] . '/api/Pay/lianLianNotify';
  101. $return_url ='https://'.$_SERVER['HTTP_HOST']."/h5/pages/mine/mine?id={$order_id}&orderNo={$voucher_order['order_no']}";//回调地址
  102. $risk_item= risk_item($user,'余额充值',$from == 1 ? 16 : 10);
  103. $obj = new LianLianPay();
  104. $res= $obj->cashierDesk($voucher_order['order_no'],$money,$this->uid,$notify_url,$return_url,'余额充值',$risk_item);
  105. $retrun_data = $res['pay_result'];
  106. $retrun_data['request_params'] = $res['request_params'];
  107. break;
  108. }
  109. Db::commit();
  110. }catch (\Exception $e){
  111. Db::rollback();
  112. DelRedisSetNx($this->request->action().$this->uid);
  113. $this->error($e->getMessage());
  114. }
  115. DelRedisSetNx($this->request->action().$this->uid);
  116. $this->success('ok',$retrun_data);
  117. }else{
  118. $this->error('服务器繁忙');
  119. }
  120. }
  121. }