106 lines
3.9 KiB
PHP
Executable File
106 lines
3.9 KiB
PHP
Executable File
<?php
|
||
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\common\model\User;
|
||
use app\common\model\Order;
|
||
use app\common\model\ShopConfig;
|
||
use Exception;
|
||
use think\Request;
|
||
use think\response\Json;
|
||
use tools\Crypt;
|
||
use tools\WxxiaopayApi;
|
||
use think\facade\Log;
|
||
|
||
class PaymentController extends Controller
|
||
{
|
||
protected $authExcept = [
|
||
'wxnotify'
|
||
];
|
||
|
||
/**
|
||
* Notes: 预支付
|
||
*/
|
||
public function prepay(Request $request,Order $omodel)
|
||
{
|
||
$param = $request->param();
|
||
$user_id = $param['uid']?Crypt::decrypt($param['uid']):'';
|
||
if(!$user_id){
|
||
return unauthorized('请先授权');
|
||
}
|
||
$order_sn=$param['order_sn']??'';
|
||
if(!isset($order_sn)) {
|
||
return client_error('订单不存在');
|
||
}
|
||
$order = $omodel
|
||
->alias('o')
|
||
->field("order_status,openid")
|
||
->join('user u', 'u.id = o.user_id')
|
||
->where(array('order_sn'=>$order_sn,'user_id'=>$user_id))
|
||
->find();
|
||
//找不到订单
|
||
if (empty($order)) {
|
||
return client_error('订单不存在');
|
||
}
|
||
if ($order['order_status'] !='0') {
|
||
return client_error('该订单无法进行支付,请查看订单状态');
|
||
}
|
||
$order_wechatsn =$order_sn.str_pad(mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
|
||
$WXPAYKEY=ShopConfig::get('website', 'WXPAYKEY');
|
||
$MCHID=ShopConfig::get('website', 'MCHID');
|
||
$APPID=ShopConfig::get('website', 'APPID');
|
||
$notify_url=ShopConfig::get('website', 'notify_url');
|
||
$WxxiaopayApi = new WxxiaopayApi($APPID,$MCHID,$WXPAYKEY,$notify_url);
|
||
$log_ip=$request->ip();
|
||
$wxData =$WxxiaopayApi->prePay($order_sn,$order_wechatsn,$order_sn,$order['openid'],$log_ip);
|
||
return success($wxData);
|
||
}
|
||
|
||
//微信小程序回调
|
||
public function wxnotify(Request $request,FrontMemberLog $fmlmodel,FrontUserMember $fummodel)
|
||
{
|
||
$xml = file_get_contents('php://input', 'r');
|
||
$data = $this->toArray($xml);
|
||
Log::write('微信回调2:'.var_export($data, true));
|
||
if($data['result_code']==='SUCCESS' && $data['return_code']==='SUCCESS'){
|
||
$order_sn = mb_substr($data['out_trade_no'],0,18);
|
||
$parameter['transaction_id']= $data['transaction_id']; //支付宝交易号
|
||
$parameter['pay_time'] =time();
|
||
$parameter['is_pay'] =1;
|
||
$order_info = $fmlmodel
|
||
->alias('fml')
|
||
->field('fml.*,fm.days as days')
|
||
->where(array('order_sn'=>$order_sn))
|
||
->join('front_member fm', 'fm.id = fml.member_id')
|
||
->find();
|
||
if($order_info){
|
||
$fummodel_info=$fummodel->where(array('user_id'=>$order_info['user_id'],'cid'=>$order_info['cid']))->find();
|
||
$days_num=(60*60*24)*$order_info['days'];
|
||
$data_info['end_time']=time()+$days_num;
|
||
if($fummodel_info){
|
||
$fummodel->where(array('id'=>$fummodel_info['id']))->update($data_info);
|
||
}else{
|
||
$data_info['user_id']=$order_info['user_id'];
|
||
$data_info['cid']=$order_info['cid'];
|
||
$fummodel::create($data_info);
|
||
}
|
||
$fmlmodel->where(array('order_sn'=>$order_sn))->update($parameter);
|
||
exit('success');
|
||
}
|
||
}
|
||
exit('fail');
|
||
}
|
||
|
||
/**
|
||
* 将xml转为array
|
||
* @param string $xml xml字符串
|
||
* @return array 转换得到的数组
|
||
*/
|
||
public function toArray($xml) {
|
||
//禁止引用外部xml实体
|
||
libxml_disable_entity_loader(true);
|
||
$result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
return $result;
|
||
}
|
||
} |