...
|
...
|
@@ -10,6 +10,8 @@ use Yansongda\Pay\Pay; |
|
|
use app\admin\model\Account;
|
|
|
use app\admin\model\User;
|
|
|
use app\admin\model\Message;
|
|
|
use app\admin\model\Car;
|
|
|
use app\admin\model\Product;
|
|
|
use think\Log;
|
|
|
use fast\Http;
|
|
|
use think\Validate;
|
...
|
...
|
@@ -19,8 +21,8 @@ use Exception; |
|
|
*/
|
|
|
class Wxpay extends Api
|
|
|
{
|
|
|
protected $noNeedLogin = ['notify','notifyCharge'];
|
|
|
protected $noNeedRight = ['notify','notifyCharge'];
|
|
|
protected $noNeedLogin = ['notify','notifyCharge','handle'];
|
|
|
protected $noNeedRight = ['notify','notifyCharge','handle'];
|
|
|
protected $user_id = '';//token存贮user_id
|
|
|
protected $order_status = [];//订单状态
|
|
|
public function _initialize()
|
...
|
...
|
@@ -38,7 +40,6 @@ 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(){
|
...
|
...
|
@@ -47,7 +48,6 @@ 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('无效的参数');
|
|
|
}
|
...
|
...
|
@@ -78,12 +78,9 @@ 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/notify','','',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));
|
|
|
|
...
|
...
|
@@ -134,18 +131,13 @@ class Wxpay extends Api |
|
|
*/
|
|
|
public function notify(){
|
|
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
|
|
$share_uid = $this->request->param('share_uid');
|
|
|
try {
|
|
|
$data = $pay->verify();
|
|
|
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]]);
|
|
|
|
|
|
//如果携带分享uid,则按照积分增加
|
|
|
if(!empty($share_uid)){
|
|
|
$person = new Person();
|
|
|
$person->getScore($share_uid,'share_purchase');
|
|
|
}
|
|
|
//减库存,删相应购物车记录
|
|
|
$this->handle($data['out_trade_no']);
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
echo "验签失败";
|
...
|
...
|
@@ -227,4 +219,66 @@ class Wxpay extends Api |
|
|
return $pay->success()->send();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (分享购买增加积分)
|
|
|
* @ApiSummary (分享购买增加积分)
|
|
|
* @ApiMethod (GET)
|
|
|
* @ApiRoute (/api/wxpay/addScore)
|
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
* @ApiParams (name="share_uid", type="integer", required=true, description="分享uid")
|
|
|
* @ApiReturn ({
|
|
|
"code": 1,
|
|
|
"msg": "成功",
|
|
|
"time": "1554184134",
|
|
|
"data": null
|
|
|
})
|
|
|
*/
|
|
|
public function addScore(){
|
|
|
if($this->request->isGet()){
|
|
|
$share_uid = $this->request->get('share_uid');
|
|
|
//如果携带分享uid,则按照积分增加
|
|
|
$person = new Person();
|
|
|
$res = $person->getScore($share_uid,'share_purchase');
|
|
|
if($res){
|
|
|
$this->error('成功');
|
|
|
}else{
|
|
|
$this->error('失败');
|
|
|
}
|
|
|
}else{
|
|
|
$this->error('请求方式错误');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public function handle($pay_order_sn){
|
|
|
//查询订单信息的商品id,数量num
|
|
|
$data = Db::table('gc_porder')
|
|
|
->where(['pay_order_sn'=>$pay_order_sn])
|
|
|
->field('p_id,num')
|
|
|
->select();
|
|
|
$pIds = array_column($data,'p_id');
|
|
|
//减库存
|
|
|
$product = Db::table('gc_product')
|
|
|
->whereIn('id',$pIds)
|
|
|
->field('id,stock')
|
|
|
->select();
|
|
|
foreach($product as &$p_value){
|
|
|
foreach($data as $value){
|
|
|
if($p_value['id'] == $value['p_id']){
|
|
|
$p_value['stock'] = $p_value['stock'] - $value['num'];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//更新库存
|
|
|
$productModel = new Product();
|
|
|
$productModel->saveAll($product);
|
|
|
|
|
|
$userModel = new User();
|
|
|
$user = $userModel->where(['openid' => $data['openid']])->find();
|
|
|
|
|
|
//清理购物车
|
|
|
$carModel = new Car();
|
|
|
$carModel->whereIn('p_id',$pIds)->where('uid',$user['id'])->delete();
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|