123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 试课
- */
- class Trylesson extends Api
- {
- // 无需登录的接口,*表示全部
- protected $noNeedLogin = [];
- // 无需鉴权的接口,*表示全部
- protected $noNeedRight = ['*'];
- public function lists(){
- $list = Db::name('trylesson')->where('is_show',1)->order('weigh desc')->autopage()->select();
- $list = list_domain_image($list,['image']);
- $list = $this->list_lang($list,['name','content']);
- $this->success(1,$list);
- }
- public function info(){
- $id = input('id');
- $info = Db::name('trylesson')->where('id',$id)->find();
- $info = info_domain_image($info,['image']);
- $info = $this->info_lang($info,['name','content']);
- $info['map_image'] = localpath_to_netpath(config('site.index_bottom_image'));
- $info['public_store_address'] = config('site.public_store_address');
- $info['trylesson_cancel_text'] = config('site.trylesson_cancel_text');
- $this->success(1,$info);
- }
- public function apply(){
- $id = input('id',0);
- $info = Db::name('trylesson')->where('id',$id)->where('is_show',1)->find();
- if(empty($info)){
- $this->error('该试课已下架');
- }
- //试课数据
- $field = ['firstname','lastname','mobile','email','height','age','weight','address','health','emergency','comefrom','is_first'];
- $data = request_post_hub($field);
- $sysdata = [
- 'order_no' => createUniqueNo('T',$this->auth->id),
- 'user_id' => $this->auth->id,
- 'trylesson_id' => $id,
- 'order_amount' => $info['price'],
- 'order_status' => 0,
- 'createtime' => time(),
- ];
- $data = array_merge($data,$sysdata);
- //支付订单数据
- $pay_order = [];
- $pay_order['user_id'] = $sysdata['user_id'];
- $pay_order['out_trade_no'] = $sysdata['order_no'];
- $pay_order['order_amount'] = $sysdata['order_amount'];
- $pay_order['createtime'] = $sysdata['createtime'];
- $pay_order['pay_type'] = 'hitpay';
- $pay_order['platform'] = 'app';
- $pay_order['order_status'] = 0;
- $pay_order['table_name'] = 'trylesson_order';
- $pay_order['table_id'] = 0;
- $pay_order['args'] = '';
- //拉起支付
- $notify_url = config('notify_cdnurl');
- $hitpay_return = $this->hitpay_payment($pay_order['out_trade_no'],$pay_order['order_amount'],$notify_url);
- if($hitpay_return['status'] !== true){
- $this->error($hitpay_return['msg']);
- }
- //支付请求id
- $pay_order['payment_request_id'] = $hitpay_return['id'];
- //入库试课订单
- Db::startTrans();
- $id = Db::name('trylesson_order')->insertGetId($data);
- if(!$id){
- Db::rollback();
- $this->error('预约失败');
- }
- //支付订单下单
- $pay_order['table_id'] = $id;
- $pay_order_id = Db::name('pay_order')->insertGetId($pay_order);
- if(!$pay_order_id){
- Db::rollback();
- $this->error('下单失败');
- }
- Db::commit();
- $return = [
- 'url' => $hitpay_return['url'],
- 'id' => $hitpay_return['id'],
- ];
- $this->success(1,$return);
- }
- //拉起支付
- private function hitpay_payment($out_trade_no,$money,$notifyurl)
- {
- $return = [
- 'status' => false,
- 'msg' => '',
- 'url' => '',
- 'id' => 0,
- ];
- $apiKey = config('hitpay.apikey');
- try {
- $hitPayClient = new \HitPay\Client($apiKey, true);
- $request = new \HitPay\Request\CreatePayment();
- $request->setAmount($money)
- ->setCurrency('SGD')
- ->setPaymentMethods(['paynow_online','card'])
- ->setPurpose('Elin Dance Studio')
- ->setWebhook($notifyurl)
- ->setReferenceNumber($out_trade_no);
- $result = $hitPayClient->createPayment($request);
- $return['status'] = true;
- $return['url'] = $result->getUrl();
- $return['id'] = $result->getId();
- //print_r($result);
- /*$data = $hitPayClient->getPaymentStatus($result->getId());
- dump($data);
- dump($data->status);*/
- /*$data = $hitPayClient->deletePaymentRequest($data->getId());
- print_r($data);*/
- } catch (\Exception $e) {
- $return['msg'] = $e->getMessage();
- }
- return $return;
- }
- }
|