李忠征信平台后端管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
7.3 KiB

2 months ago
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\Order as OrderModel;
use app\admin\model\Content;
use app\admin\model\content\Type as Contenttype;
use think\Db;
use addons\epay\library\Service;
use fast\Random;
use think\addons\Controller;
use app\common\model\User;
use Exception;
use app\admin\model\user\Vipset;
use app\admin\model\user\Zanshang;
/**
* 微信支付宝整合插件首页
*
* 此控制器仅用于开发展示说明和测试,请自行添加一个新的控制器进行处理返回和回调事件,同时删除此控制器文件
*
* Class Index
* @package addons\epay\controller
*/
class Order extends Api
{
protected $model = null;
protected $noNeedRight = '*';
protected $noNeedLogin = ['*'];
public function _initialize()
{
parent::_initialize();
}
public function createOrder(){
if($this->request->isPost()){
$uid = $this->auth->id;
if(!$uid) $this->error("请先登录");
$data = $this->request->post();
$id = $data['id'];
$type = $data['type'];
// 判断TYPE类型
if($type==1){//type为1时查询事件
$orderinfo = Content::get($id);
if(!$orderinfo) $this->error('查询的事件不存在');
$pay_price = $orderinfo['money'];
}else if($type==2){
$orderinfo = Vipset::get($id);
$pay_price = $orderinfo['pay_money'];
}else if($type==3){
$orderinfo = Zanshang::get($id);
$pay_price = $orderinfo['money'];
}
// 查询是否存在订单
$order = OrderModel::where('content_id',$id)->where('user_id',$uid)->where('status',0)->where('type',$type)->find();
try {
$insert['content_id'] = $id;
$insert['type'] = $type;
$insert['user_id'] = $this->auth->id;
$insert['order_no'] = "ZX".Random::numeric(15);
$insert['money'] = $pay_price;
$insert['createtime'] = date('Y-m-d H:i:s',time());
$insert['retime'] = time();
$return['order_no'] = $insert['order_no'];
$return['retime'] = $insert['retime'];
$return['money'] = $pay_price;
if(!$return['order_id'] = OrderModel::insertGetId($insert)) throw new Exception("生成订单失败");
} catch (Exception $e) {
return $this->error($e->getMessage());
}
return $this->success('订单创建成功',$return);
}
}
public function updateOrder(){
}
public function beforePay(){
if($this->request->isPost()){
$min_user = new \app\admin\model\miniprogram\User;
$amount = $this->request->post('pay_price');
2 months ago
2 months ago
//订单号
$out_trade_no = $this->request->post('orderno');
$type = 'wechat';
$method = 'miniapp';
$min_user_info = $min_user->getbyUser_id($this->auth->id);
2 months ago
//判断是否是云股东
2 months ago
$openid = $min_user_info['openid'];
if (!$amount || $amount < 0) {
$this->error("支付金额必须大于0");
}
if (!$out_trade_no) {
$this->error("订单号不存在");
}
if (!$type || !in_array($type, ['alipay', 'wechat'])) {
$this->error("支付类型不能为空");
}
if (in_array($method, ['miniapp', 'mp']) && !$openid) {
$this->error("openid不能为空");
}
//订单标题
$title = '内容查看'.$out_trade_no;
//回调链接
$notifyurl = $this->request->root(true) . '/api/order/notifyx/paytype/' . $type;
$returnurl = $this->request->root(true) . '/api/order/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
try {
// 生成订单
if(!$response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method, $openid)) throw new Exception("生成订单失败");
} catch (Exception $e) {
return $this->error($e->getMessage());
}
return $this->success("微信支付预处理成功",$response);
}
return $this->error("支付信息失败");
}
public function notifyx()
{
\think\Log::record("进来了");
$paytype = $this->request->param('paytype');
$pay = Service::checkNotify($paytype);
if (!$pay) {
return json(['code' => 'FAIL', 'message' => '失败'], 500, ['Content-Type' => 'application/json']);
}
\think\Log::record("进来了");
// 获取回调数据,V3和V2的回调接收不同
$data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
Db::startTrans();
try {
\think\Log::record("进入try");
//微信支付V3返回和V2不同
if (Service::isVersionV3() && $paytype === 'wechat') {
\think\Log::record("判断v几");
$data = $data['resource']['ciphertext'];
$data['total_fee'] = $data['amount']['total'];
}
\think\Log::record($data);
//获取支付金额、订单号
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
$out_trade_no = $data['out_trade_no'];
\think\Log::record("回调成功,订单号:{$out_trade_no},金额:{$payamount}");
$order = OrderModel::where('order_no',$out_trade_no)->find();
if($order){
$order->status = 1;
$order->ispay = 1;
$order->paytime = date('Y-m-d H:i:s',time());
$order->money = $payamount;
$order->paytype = $paytype;
2 months ago
$order->system_money = $payamount;
2 months ago
if($order->type==1){
// 更改用户余额
// 查询事件用户ID
if($order->money>=1){
$contentinfo = Content::get($order->content_id);
$dashang = number_format($order->money*0.5, 2, '.', '');
User::money($dashang, $contentinfo['user_id'], $contentinfo->title,1);
2 months ago
$order->user_money = $dashang;
$order->system_money = $payamount-$dashang;
2 months ago
}
}else if($order->type==2){
$user = User::get($order->user_id);
$user->is_vip = 1;
$user->vipout_time = (int)$user->vipout_time>time()?(time() + 2592000):(int)$user->vipout_time+2592000;
$user->read_num = (int)$user->vipout_time>time()?10:(int)$user->read_num+10;
$user->save();
2 months ago
$order->outtime = date("Y-m-d H:i:s",(int)$user->vipout_time>time()?(time() + 2592000):(int)$user->vipout_time+2592000);
2 months ago
}
$order->save();
}
Db::commit();
//你可以在此编写订单逻辑
} catch (Exception $e) {
Db::rollback();
\think\Log::record("回调逻辑处理错误:" . $e->getMessage(), "error");
}
//下面这句必须要执行,且在此之前不能有任何输出
if (Service::isVersionV3()) {
return $pay->success()->getBody()->getContents();
} else {
return $pay->success()->send();
}
}
public function returnx()
{
$paytype = $this->request->param('paytype');
$out_trade_no = $this->request->param('out_trade_no');
$pay = Service::checkReturn($paytype);
if (!$pay) {
$this->error('签名错误', '');
}
//你可以在这里定义你的提示信息,但切记不可在此编写逻辑
$this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
}
public function orderRefunds($refund){
try {
if(!Service::refunds($refund)) throw new Exception("退出组局失败");
} catch (Exception $e) {
return false;
}
return true;
}
}