...
|
...
|
@@ -9,6 +9,8 @@ use think\Db; |
|
|
use Yansongda\Pay\Pay;
|
|
|
use app\admin\model\Account;
|
|
|
use app\admin\model\User;
|
|
|
use app\admin\model\Message;
|
|
|
use app\admin\model\off\Line;
|
|
|
use think\Log;
|
|
|
use fast\Http;
|
|
|
use think\Validate;
|
...
|
...
|
@@ -18,8 +20,8 @@ use Exception; |
|
|
*/
|
|
|
class Wxpay extends Api
|
|
|
{
|
|
|
protected $noNeedLogin = ['notify','wechatReturn','notifyCharge'];
|
|
|
protected $noNeedRight = ['notify','wechatReturn','notifyCharge'];
|
|
|
protected $noNeedLogin = ['notify','notifyCharge'];
|
|
|
protected $noNeedRight = ['notify','notifyCharge'];
|
|
|
protected $user_id = '';//token存贮user_id
|
|
|
protected $order_status = [];//订单状态
|
|
|
public function _initialize()
|
...
|
...
|
@@ -37,6 +39,7 @@ class Wxpay extends Api |
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
|
|
|
* @ApiParams (name="pay_order_sn", type="string", required=true, description="支付订单号")
|
|
|
* @ApiParams (name="share_uid", type="integer", required=false, description="分享用户id")
|
|
|
* @ApiReturn ({code: 0, msg: "无效的订单", time: "1554176100", data: null})
|
|
|
*/
|
|
|
public function pay(){
|
...
|
...
|
@@ -45,7 +48,7 @@ class Wxpay extends Api |
|
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
|
|
$openid = $this->request->post('openid');//小程序传递openid
|
|
|
$pay_order_sn = $this->request->post('pay_order_sn');//支付订单号
|
|
|
|
|
|
$share_uid = $this->request->post('share_uid');//分享用户uid
|
|
|
if(empty($openid) && empty($pay_order_sn)){
|
|
|
$this->error('无效的参数');
|
|
|
}
|
...
|
...
|
@@ -76,9 +79,12 @@ class Wxpay extends Api |
|
|
// 'total_fee' => 1,
|
|
|
'total_fee' => floatval($total_price)*100,//单位:分
|
|
|
'openid' => $openid,
|
|
|
'notify_url' => url('api/Wxpay/notify','','',true),
|
|
|
'return_url' => url('api/Wxpay/wechatReturn','','',true),
|
|
|
];
|
|
|
if(!empty($share_uid)){
|
|
|
$order['notify_url'] = url('api/Wxpay/notify/share_uid/'.$share_uid,'','',true);
|
|
|
}else{
|
|
|
$order['notify_url'] = url('api/Wxpay/notify','','',true);
|
|
|
}
|
|
|
//跳转或输出
|
|
|
$this->success('成功',$pay->miniapp($order));
|
|
|
|
...
|
...
|
@@ -125,36 +131,35 @@ class Wxpay extends Api |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 支付成功回调
|
|
|
* 微信异步通知
|
|
|
*/
|
|
|
public function notify(){
|
|
|
$pay = Service::checkNotify('wechat');
|
|
|
if (!$pay) {
|
|
|
$this->error('签名错误');
|
|
|
}
|
|
|
//你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
|
|
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
|
|
$share_uid = $this->request->param('share_uid');
|
|
|
try {
|
|
|
$data = $pay->verify();
|
|
|
if($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {
|
|
|
if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS'){
|
|
|
$porderModel = new Porder();
|
|
|
$porderModel->where(['pay_order_sn'=>$data['out_trade_no']])->update(['status'=>$this->order_status[1]]);
|
|
|
//下面这句必须要执行,且在此之前不能有任何输出
|
|
|
$this->success('成功',$pay->success());
|
|
|
}
|
|
|
$this->success();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信异步通知
|
|
|
*/
|
|
|
public function wechatReturn(){
|
|
|
$pay = Service::checkReturn('wechat');
|
|
|
if (!$pay) {
|
|
|
$this->error('签名错误');
|
|
|
}
|
|
|
//你可以在这里定义你的提示信息,但切记不可在此编写逻辑
|
|
|
$this->success("恭喜你!支付成功!");
|
|
|
//如果携带分享uid,则按照积分增加
|
|
|
if(!empty($share_uid)){
|
|
|
$score = config('site.share_purchase');
|
|
|
//给分享用户增加积分
|
|
|
$user = new User();
|
|
|
$user->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
|
|
|
//成为分享用户的下线
|
|
|
$offlineModel = new Line();
|
|
|
$offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]);
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
echo "验签失败";
|
|
|
return;
|
|
|
}
|
|
|
return $pay->success()->send();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (充值)
|
|
|
* @ApiSummary (充值)
|
...
|
...
|
@@ -188,7 +193,6 @@ class Wxpay extends Api |
|
|
'total_fee' => floatval($amount)*100,//单位:分
|
|
|
'openid' => $openid,
|
|
|
'notify_url' => url('api/Wxpay/notifyCharge','','',true),
|
|
|
'return_url' => url('api/Wxpay/wechatReturn','','',true),
|
|
|
];
|
|
|
//跳转或输出
|
|
|
$this->success('成功',$pay->miniapp($order));
|
...
|
...
|
@@ -198,25 +202,35 @@ class Wxpay extends Api |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 充值成功回调
|
|
|
* 微信异步通知
|
|
|
*/
|
|
|
public function notifyCharge(){
|
|
|
$pay = Service::checkNotify('wechat');
|
|
|
if (!$pay) {
|
|
|
$this->error('签名错误');
|
|
|
}
|
|
|
//你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
|
|
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
|
|
try {
|
|
|
$data = $pay->verify();
|
|
|
if($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {
|
|
|
//你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
|
|
|
if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS'){
|
|
|
$money = $data['total_fee'] / 100;
|
|
|
$userModel = new User();
|
|
|
$user = $userModel->where(['openid'=>$data['openid']])->find();
|
|
|
$user = $userModel->where(['openid' => $data['openid']])->find();
|
|
|
//给用户增加充值金额
|
|
|
$userModel->where(['openid' => $data['openid']])->setInc('money',$money);
|
|
|
|
|
|
//充值记录
|
|
|
$accountModel = new Account();
|
|
|
$money = $data['total_fee']/100;
|
|
|
$accountModel::create(['uid'=>$user['id'],'type'=>config('site.pay_charge'),'money'=>$money]);
|
|
|
//下面这句必须要执行,且在此之前不能有任何输出
|
|
|
$this->success('成功',$pay->success());
|
|
|
$account = $accountModel::create(['uid' => $user['id'], 'money' => $money]);
|
|
|
|
|
|
//系统充值消息
|
|
|
$messageModel = new Message();
|
|
|
$messageModel::create(['account_id' => $account->id, 'title' => config('site.message')]);
|
|
|
Log::info('执行一次');
|
|
|
}
|
|
|
$this->success();
|
|
|
} catch (Exception $e) {
|
|
|
echo "验签失败";
|
|
|
return;
|
|
|
}
|
|
|
//下面这句必须要执行,且在此之前不能有任何输出
|
|
|
return $pay->success()->send();
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|