Recharge.php 4.6 KB

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