Trylesson.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 试课
  7. */
  8. class Trylesson extends Api
  9. {
  10. // 无需登录的接口,*表示全部
  11. protected $noNeedLogin = [];
  12. // 无需鉴权的接口,*表示全部
  13. protected $noNeedRight = ['*'];
  14. public function lists(){
  15. $list = Db::name('trylesson')->where('is_show',1)->order('weigh desc')->autopage()->select();
  16. $list = list_domain_image($list,['image']);
  17. $list = $this->list_lang($list,['name','content']);
  18. $this->success(1,$list);
  19. }
  20. public function info(){
  21. $id = input('id');
  22. $info = Db::name('trylesson')->where('id',$id)->find();
  23. $info = info_domain_image($info,['image']);
  24. $info = $this->info_lang($info,['name','content']);
  25. $info['map_image'] = localpath_to_netpath(config('site.index_bottom_image'));
  26. $info['public_store_address'] = config('site.public_store_address');
  27. $info['trylesson_cancel_text'] = config('site.trylesson_cancel_text');
  28. $this->success(1,$info);
  29. }
  30. public function apply(){
  31. $id = input('id',0);
  32. $info = Db::name('trylesson')->where('id',$id)->where('is_show',1)->find();
  33. if(empty($info)){
  34. $this->error('该试课已下架');
  35. }
  36. //试课数据
  37. $field = ['firstname','lastname','mobile','email','height','age','weight','address','health','emergency','comefrom','is_first'];
  38. $data = request_post_hub($field);
  39. $sysdata = [
  40. 'order_no' => createUniqueNo('T',$this->auth->id),
  41. 'user_id' => $this->auth->id,
  42. 'trylesson_id' => $id,
  43. 'order_amount' => $info['price'],
  44. 'order_status' => 0,
  45. 'createtime' => time(),
  46. ];
  47. $data = array_merge($data,$sysdata);
  48. //支付订单数据
  49. $pay_order = [];
  50. $pay_order['user_id'] = $sysdata['user_id'];
  51. $pay_order['out_trade_no'] = $sysdata['order_no'];
  52. $pay_order['order_amount'] = $sysdata['order_amount'];
  53. $pay_order['createtime'] = $sysdata['createtime'];
  54. $pay_order['pay_type'] = 'hitpay';
  55. $pay_order['platform'] = 'app';
  56. $pay_order['order_status'] = 0;
  57. $pay_order['table_name'] = 'trylesson_order';
  58. $pay_order['table_id'] = 0;
  59. $pay_order['args'] = '';
  60. //拉起支付
  61. $notify_url = config('notify_cdnurl');
  62. $hitpay_return = $this->hitpay_payment($pay_order['out_trade_no'],$pay_order['order_amount'],$notify_url);
  63. if($hitpay_return['status'] !== true){
  64. $this->error($hitpay_return['msg']);
  65. }
  66. //支付请求id
  67. $pay_order['payment_request_id'] = $hitpay_return['id'];
  68. //入库试课订单
  69. Db::startTrans();
  70. $id = Db::name('trylesson_order')->insertGetId($data);
  71. if(!$id){
  72. Db::rollback();
  73. $this->error('预约失败');
  74. }
  75. //支付订单下单
  76. $pay_order['table_id'] = $id;
  77. $pay_order_id = Db::name('pay_order')->insertGetId($pay_order);
  78. if(!$pay_order_id){
  79. Db::rollback();
  80. $this->error('下单失败');
  81. }
  82. Db::commit();
  83. $return = [
  84. 'url' => $hitpay_return['url'],
  85. 'id' => $hitpay_return['id'],
  86. ];
  87. $this->success(1,$return);
  88. }
  89. //拉起支付
  90. private function hitpay_payment($out_trade_no,$money,$notifyurl)
  91. {
  92. $return = [
  93. 'status' => false,
  94. 'msg' => '',
  95. 'url' => '',
  96. 'id' => 0,
  97. ];
  98. $apiKey = config('hitpay.apikey');
  99. try {
  100. $hitPayClient = new \HitPay\Client($apiKey, true);
  101. $request = new \HitPay\Request\CreatePayment();
  102. $request->setAmount($money)
  103. ->setCurrency('SGD')
  104. ->setPaymentMethods(['paynow_online','card'])
  105. ->setPurpose('Elin Dance Studio')
  106. ->setWebhook($notifyurl)
  107. ->setReferenceNumber($out_trade_no);
  108. $result = $hitPayClient->createPayment($request);
  109. $return['status'] = true;
  110. $return['url'] = $result->getUrl();
  111. $return['id'] = $result->getId();
  112. //print_r($result);
  113. /*$data = $hitPayClient->getPaymentStatus($result->getId());
  114. dump($data);
  115. dump($data->status);*/
  116. /*$data = $hitPayClient->deletePaymentRequest($data->getId());
  117. print_r($data);*/
  118. } catch (\Exception $e) {
  119. $return['msg'] = $e->getMessage();
  120. }
  121. return $return;
  122. }
  123. }