123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /**
- * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
- * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
- * @author pillar<ltmn@qq.com>
- * 支付接口
- */
- namespace app\citys\controller\api\v1;
- use app\citys\controller\api\Base;
- use app\citys\model\Citys;
- use app\citys\model\CitysOrder;
- use app\citys\model\CitysConfig;
- use app\citys\model\CitysPhone;
- use app\common\facade\WechatPay;
- use filter\Filter;
- class Infopay extends Base{
- /**
- * 初始化当前应用是否登录
- * @return void
- */
- public function initialize() {
- parent::initialize();
- $this->isUserAuth();
- }
- /**
- * 购买商品
- */
- public function index(){
- if (request()->isPost()) {
- $param['id'] = $this->request->param('id/d');
- $param['telphone'] = $this->request->param('telphone/s','');
- $param['message'] = $this->request->param('message','');
- $param['ucode'] = $this->request->param('ucode','');
- $param['fields'] = $this->request->param('fields/s','[]','htmlspecialchars_decode');
- $this->apiSign($param);
- $param['fields'] = json_encode(Filter::filter_escape(json_decode($param['fields'],true)));
- //判断是否已下架
- $item = Citys::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'is_lock' => 0])->find();
- if(empty($item)){
- return enjson(403,'信息不存在');
- }
- //读取订单
- $order_no = order_no($this->user->invite_code);
- $amount = $item->price;
- //唤醒微信支付参数
- $payparm = [
- 'openid' => $this->user->miniapp_uid,
- 'miniapp_id' => $this->miniapp_id,
- 'name' => $item->mp->title,
- 'order_no' => $order_no,
- 'total_fee' => $amount <= 0 ? 1 : $amount * 100,
- 'notify_url' => api(1,'ais/notify/infoOrder',$this->miniapp_id),
- ];
- //读取配置
- if($this->member_miniapp->is_psp){
- $setting = CitysConfig::getConfig($this->miniapp_id);
- if (empty($setting->mchid)) {
- return enjson(403,'服务商模式下,必须配置默认商户号');
- }
- $payparm['mchid'] = $setting->mchid;
- }
- $ispay = WechatPay::orderPay($payparm);
- if ($ispay['code'] == 0) {
- return enjson(403,$ispay['msg']);
- }
- //判断是否新订单
- $param['member_miniapp_id'] = $this->miniapp_id;
- $param['uid'] = $this->user->id;
- $param['info_uid'] = $item->uid;
- $param['info_id'] = $item->id;
- $param['phone'] = $param['telphone'];
- $param['amount'] = $amount;
- $param['order_no'] = $order_no;
- $param['message'] = $param['message'];
- $param['fields'] = $param['fields'];
- $param['cache'] = $item->toJson();
- $rel = CitysOrder::insertOrder($param,$order_no);
- if(empty($rel)){
- return enjson(204,'购买商品失败');
- }
- return enjson(200,'成功',$ispay['data']);
- }
- }
- /**
- * 我的订单
- * @return void
- */
- public function order(){
- $param['active'] = $this->request->param('active/d',0);
- $param['page'] = $this->request->param('page/d',1);
- $this->apiSign($param);
- $condition['uid'] = $this->user->id;
- $condition['is_del'] = 0;
- switch ($param['active']) {
- case 1:
- $condition['paid_at'] = 0;
- break;
- case 2:
- $condition['paid_at'] = 1;
- $condition['status'] = 0;
- break;
- case 3:
- $condition['paid_at'] = 1;
- $condition['status'] = 1;
- break;
- }
- $lists = CitysInfoOrder::where($condition)->field('cache',true)->order('id desc')->paginate(10)->toArray();
- $data = [];
- foreach ($lists['data'] as $key => $value) {
- $data[$key] = $value;
- $data[$key]['fields'] = json_decode($value['fields'],true);
- }
- return enjson(200,'成功',$data);
- }
- /**
- * 显示号码
- * @return void
- */
- public function showPhone(){
- if (request()->isPost()) {
- $param['id'] = $this->request->param('id/d');
- $this->apiSign($param);
- //判断是否已下架
- $item = Citys::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'is_lock' => 0])->find();
- if(empty($item)){
- return enjson(403,'信息不存在');
- }
- $price = ($item->types->price??0)+($item->cate->price??0);
- //判断是否重复购买
- $phone = CitysPhone::where(['info_id' => $item->id,'uid'=>$this->user->id])->find();
- if(!empty($phone) && $phone->paid_at == 1){
- return enjson(403,'不用重复购买');
- }
- $setting = CitysConfig::config($this->miniapp_id);
- //读取订单
- $order_no = order_no($this->user->invite_code);
- if($phone){
- $phone->price = $price;
- $phone->order_no = $order_no;
- $rel = $phone->save();
- }else{
- //判断是否新订单
- $data['member_miniapp_id'] = $this->miniapp_id;
- $data['uid'] = $this->user->id;
- $data['info_id'] = $item->id;
- $data['price'] = $price;
- $data['order_no'] = $order_no;
- $rel = CitysPhone::create($data);
- }
- if(empty($rel)){
- return enjson(204,'下单失败失败');
- }
- //吊起支付
- $payparm = [
- 'openid' => $this->user->miniapp_uid,
- 'miniapp_id' => $this->miniapp_id,
- 'name' => '电话服务',
- 'order_no' => $order_no,
- 'total_fee' => $price*100,
- 'notify_url' => api(1,'citys/notify/showPhone',$this->miniapp_id),
- ];
- //读取配置
- if($this->member_miniapp->is_psp){
- if (empty($setting->mch_id)) {
- return enjson(403,'服务商模式下,必须配置默认商户号');
- }
- $payparm['mchid'] = $setting->mch_id;
- }
- $ispay = WechatPay::orderPay($payparm);
- if ($ispay['code'] == 0) {
- return enjson(403,$ispay['msg']);
- }
- return enjson(200,'成功',$ispay['data']);
- }
- }
- }
|