...
|
...
|
@@ -4,20 +4,14 @@ namespace app\api\controller; |
|
|
|
|
|
use app\common\controller\Api;
|
|
|
use EasyWeChat\Factory;
|
|
|
use function GuzzleHttp\Promise\inspect;
|
|
|
use think\Db;
|
|
|
use app\admin\model\User;
|
|
|
use think\Log;
|
|
|
use fast\Http;
|
|
|
use think\Validate;
|
|
|
use Exception;
|
|
|
use function EasyWeChat\Kernel\Support\generate_sign;
|
|
|
/**
|
|
|
* 支付接口
|
|
|
*/
|
|
|
class Pay extends Api
|
|
|
{
|
|
|
protected $noNeedLogin = ['payMemberNotify','paySecurityNotify','payOrderNotify'];
|
|
|
protected $noNeedLogin = ['payOrderNotify','purchaseNotify'];
|
|
|
protected $noNeedRight = ['*'];
|
|
|
protected $uid = '';//token存贮uid
|
|
|
protected $order_status = [];//订单状态
|
...
|
...
|
@@ -25,6 +19,7 @@ class Pay extends Api |
|
|
{
|
|
|
parent::_initialize();
|
|
|
$this->uid = $this->auth->getUserId();
|
|
|
$this->order_status = config('verify.status');
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -32,64 +27,39 @@ class Pay extends Api |
|
|
* @ApiSummary (支付订单)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/pay/pay)
|
|
|
* @ApiInternal
|
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
*
|
|
|
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
|
|
|
* @ApiParams (name="order_sn", type="inter", required=false, description="支付订单号")
|
|
|
* @ApiParams (name="pay_order_sn", type="inter", required=true, description="支付订单号")
|
|
|
*
|
|
|
*/
|
|
|
public function pay(){
|
|
|
if($this->request->isPost()){
|
|
|
$openid = $this->request->post('openid');
|
|
|
$order_sn = $this->request->post('order_sn');
|
|
|
$pay_order_sn = $this->request->post('pay_order_sn');
|
|
|
|
|
|
$rule = config('verify.pay_member');
|
|
|
$rule = config('verify.pay');
|
|
|
$validate = new Validate($rule['rule'],$rule['msg']);
|
|
|
if (!$validate->check(['openid'=>$openid])) {
|
|
|
if (!$validate->check(['openid'=>$openid,'pay_order_sn'=>$pay_order_sn])) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
|
|
|
$hotel_url = config('verify.hotel_url');
|
|
|
if($order_sn){
|
|
|
//支付订单
|
|
|
$res = Db::name('order')->where(['order_sn'=>$order_sn,'uid'=>$this->uid])->find();
|
|
|
if($res){
|
|
|
if($res['status'] == 0){
|
|
|
//判断该房间是否已被预订
|
|
|
$res2 = $this->auth->getHouseDetailId(['house_id'=>$res['house_id']]);
|
|
|
$full_time = [];
|
|
|
foreach($res2 as $r_value){
|
|
|
if($res['book_count'] <= $r_value['book_count']){
|
|
|
//满房时间
|
|
|
array_push($full_time,$r_value['book_time']);
|
|
|
}
|
|
|
}
|
|
|
if(in_array($res['start_time'],$full_time)){
|
|
|
$this->error('该房屋已被预订');
|
|
|
}
|
|
|
$pay_order = $order_sn;
|
|
|
$notify_url = $hotel_url.'/api/pay/payOrderNotify';
|
|
|
$total_fee = $res['security_price'];
|
|
|
}else{
|
|
|
$this->error('该订单已支付过了');
|
|
|
}
|
|
|
}else{
|
|
|
$this->error('无效的订单');
|
|
|
}
|
|
|
}else{
|
|
|
//购买会员
|
|
|
$pay_order = $this->auth->genPayOrderSn('pay');//支付订单号
|
|
|
$notify_url = $hotel_url.'/api/pay/payMemberNotify';
|
|
|
$total_fee = 0.01;//会员价格暂为0.01元
|
|
|
$ffg_host = config('verify.ffg_host');
|
|
|
$status = $this->order_status;
|
|
|
//支付订单
|
|
|
$res = Common::findSoftWhereData('order',['uid'=>$this->uid,'pay_order_sn'=>$pay_order_sn,'status'=>$status[0]],'id,total_price');
|
|
|
if(!$res){
|
|
|
$this->error('无效的订单');
|
|
|
}
|
|
|
//创建支付对象
|
|
|
$config = config('verify.wx_pay');
|
|
|
$app = Factory::payment($config);
|
|
|
$result = $app->order->unify([
|
|
|
'body' => '佛山市理想中网络科技有限公司',
|
|
|
'out_trade_no' => $pay_order,//支付订单号
|
|
|
'total_fee' => $total_fee*100,//单位分
|
|
|
'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
|
|
'out_trade_no' => $pay_order_sn,//支付订单号
|
|
|
'total_fee' => $res['total_price']*100,//单位分
|
|
|
'notify_url' => $ffg_host.'/api/pay/payOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
|
|
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
|
|
|
'openid' => $openid,
|
|
|
]);
|
...
|
...
|
@@ -111,29 +81,10 @@ class Pay extends Api |
|
|
}
|
|
|
}
|
|
|
|
|
|
//购买会员回调通知(无需调用)
|
|
|
public function payMemberNotify(){
|
|
|
$config = config('verify.wx_pay');
|
|
|
$app = Factory::payment($config);
|
|
|
$response = $app->handlePaidNotify(function($message, $fail){
|
|
|
//return_code 表示通信状态
|
|
|
Log::info($message);
|
|
|
if ($message['return_code'] === 'SUCCESS') {
|
|
|
|
|
|
if ($message['result_code'] === 'SUCCESS') {
|
|
|
|
|
|
}elseif($message['result_code'] === 'FAIL') {
|
|
|
//支付失败
|
|
|
}
|
|
|
} else {
|
|
|
return $fail('通信失败,请稍后再通知我');
|
|
|
}
|
|
|
return true; // 返回处理完成
|
|
|
});
|
|
|
$response->send();
|
|
|
}
|
|
|
|
|
|
//支付订单(无需调用)
|
|
|
/**
|
|
|
* 支付订单回调
|
|
|
* @ApiInternal
|
|
|
*/
|
|
|
public function payOrderNotify(){
|
|
|
$config = config('verify.wx_pay');
|
|
|
$app = Factory::payment($config);
|
...
|
...
|
@@ -160,15 +111,22 @@ class Pay extends Api |
|
|
* @ApiSummary (立即购买)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/pay/purchase)
|
|
|
* @ApiInternal
|
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
*
|
|
|
* @ApiParams (name="receive_name", type="string", required=true, description="收货人")
|
|
|
* @ApiParams (name="receive_mobile", type="string", required=true, description="联系方式")
|
|
|
* @ApiParams (name="receive_address", type="string", required=true, description="收货地址")
|
|
|
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
|
|
|
* @ApiParams (name="goods_id", type="string", required=true, description="商品id(多个以逗号隔开,如:1,3,4)")
|
|
|
* @ApiParams (name="price", type="string", required=true, description="商品价格(多个以逗号隔开,如:100,10,200)")
|
|
|
* @ApiParams (name="goods_number", type="string", required=true, description="商品数量(多个以逗号隔开,如:10,10,10)")
|
|
|
* @ApiParams (name="style", type="string", required=true, description="商品款式(多个以逗号隔开,如:‘款式1,款式2,款式3’)")
|
|
|
* @ApiParams (name="leave_message", type="string", required=false, description="留言")
|
|
|
*
|
|
|
* @ApiParams (name="total_goods_price", type="inter", required=true, description="商品总金额")
|
|
|
* @ApiParams (name="discount_price", type="inter", required=true, description="优惠金额")
|
|
|
* @ApiParams (name="total_price", type="inter", required=true, description="总金额")
|
|
|
* @ApiParams (name="total_expense_price", type="inter", required=true, description="运费总金额")
|
|
|
*/
|
|
|
public function purchase(){
|
|
|
if($this->request->isPost()){
|
...
|
...
|
@@ -183,13 +141,13 @@ class Pay extends Api |
|
|
//创建支付对象
|
|
|
$config = config('verify.wx_pay');
|
|
|
$app = Factory::payment($config);
|
|
|
$pay_order = Common::genPayOrderSn('pay');//支付订单号
|
|
|
$hotel_url = config('verify.hotel_url');
|
|
|
$pay_order_sn = Common::genPayOrderSn('pay');//支付订单号
|
|
|
$ffg_host = config('verify.ffg_host');
|
|
|
$result = $app->order->unify([
|
|
|
'body' => '佛山市理想中网络科技有限公司',
|
|
|
'out_trade_no' => $pay_order,//支付订单号
|
|
|
'out_trade_no' => $pay_order_sn,//支付订单号
|
|
|
'total_fee' => $data['security_price']*100,//单位分
|
|
|
'notify_url' => $hotel_url.'/api/pay/purchaseNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
|
|
'notify_url' => $ffg_host.'/api/pay/purchaseNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
|
|
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
|
|
|
'openid' => $data['openid'],
|
|
|
]);
|
...
|
...
|
@@ -205,9 +163,9 @@ class Pay extends Api |
|
|
// 注意这里用的是商户平台的Key进行二次签名
|
|
|
$params['paySign'] = generate_sign($params, $config['key']);
|
|
|
//写入订单
|
|
|
$res = Common::createOrder($data);
|
|
|
$res = Common::createOrder($data,$pay_order_sn,$this->uid);
|
|
|
if($res){
|
|
|
$this->success('成功',['data'=>$params,'order_sn'=>$pay_order,'order_id'=>$res['order_id']]);
|
|
|
$this->success('成功',['data'=>$params,'pay_order_sn'=>$pay_order_sn,'order_id'=>$res['order_id']]);
|
|
|
}
|
|
|
$this->error('创建订单失败');
|
|
|
}
|
...
|
...
|
@@ -217,7 +175,10 @@ class Pay extends Api |
|
|
}
|
|
|
}
|
|
|
|
|
|
//立即购买回调(无需调用)
|
|
|
/**
|
|
|
* 立即购买回调
|
|
|
* @ApiInternal
|
|
|
*/
|
|
|
public function purchaseNotify(){
|
|
|
$config = config('verify.wx_pay');
|
|
|
$app = Factory::payment($config);
|
...
|
...
|
@@ -243,6 +204,8 @@ class Pay extends Api |
|
|
* @ApiSummary (更新订单号(支付订单失败后调用))
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/pay/updateOrder)
|
|
|
* @ApiInternal
|
|
|
*
|
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
*
|
|
|
* @ApiParams (name="order_sn", type="inter", required=false, description="支付订单号")
|
...
|
...
|
@@ -265,7 +228,7 @@ class Pay extends Api |
|
|
}
|
|
|
|
|
|
$orderModel = new \app\admin\model\Order();
|
|
|
$pay_order = $this->auth->genPayOrderSn('pay');//支付订单号
|
|
|
$pay_order = Common::genPayOrderSn('pay');//支付订单号
|
|
|
$res = $orderModel->where(['order_sn'=>$order_sn,'uid'=>$this->uid])->update(['order_sn'=>$pay_order]);
|
|
|
if($res){
|
|
|
$this->success('成功');
|
...
|
...
|
|