作者 郭鑫

支付

<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/6/14
* Time: 16:05
*/
namespace api\portal\controller;
use api\portal\controller\NotifyCommonController;
use think\Db;
use traits\controller\Jump;
use api\portal\model\OrderModel;
class AlipayController extends NotifyCommonController
{
public function _initialize()
{
require_once VENDOR_PATH.'alipay/config.php';
require_once VENDOR_PATH.'alipay/pagepay/service/AlipayTradeService.php';
require_once VENDOR_PATH.'alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';
$this->config = $config;
}
public function alipay() {
$data = $this->request->param();
$config = $this->config;
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = trim($data['order_sn']);
//订单名称,必填
$subject = trim($data['name']);
//付款金额,必填
// $total_amount = trim(I('get.price'));
$total_amount = trim(0.01);
//商品描述,可空
$body = '';
//构造参数
$payRequestBuilder = new \AlipayTradePagePayContentBuilder();
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$aop = new \AlipayTradeService($config);
/**
* pagePay 电脑网站支付请求
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @param $return_url 同步跳转地址,公网可以访问
* @param $notify_url 异步通知地址,公网可以访问
* @return $response 支付宝返回的信息
*/
$response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);
//输出表单
var_dump($response);
}
// 回调
public function notify_url() {
$config = $this->config;
$arr=$_POST;
$alipaySevice = new \AlipayTradeService($config);
$alipaySevice->writeLog(var_export($_POST,true));
$result = $alipaySevice->check($arr);
if($result) {
//商户订单号
$out_trade_no = $_POST['out_trade_no'];
//支付宝交易号
$trade_no = $_POST['trade_no'];
//交易状态
$trade_status = $_POST['trade_status'];
if($_POST['trade_status'] == 'TRADE_FINISHED') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
$where['order_sn'] = $out_trade_no;
// $where['money'] = $data['total_amount'];
$res = $this->changeOrderStatus($where,$trade_no,3,'pc支付宝支付');
if(!$res) {
echo 'fail';exit;
}
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
}
else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
$where['order_sn'] = $out_trade_no;
// $where['money'] = $data['total_amount'];
$res = $this->changeOrderStatus($where,$trade_no,3,'pc支付宝支付');
if(!$res) {
echo 'fail';exit;
}
//注意:
//付款完成后,支付宝系统发送该交易状态通知
}
echo "success"; //请不要修改或删除
}else {
//验证失败
echo "fail"; //请不要修改或删除
}
}
//
public function return_url() {
$config = $this->config;
$arr = $_GET;
$alipaySevice = new \AlipayTradeService($config);
$result = $alipaySevice->check($arr);
// var_dump($_GET);
// echo '<br/>';
if ($result) {
//商户订单号
$out_trade_no = htmlspecialchars($_GET['out_trade_no']);
//支付宝交易号
$trade_no = htmlspecialchars($_GET['trade_no']);
$data = $_GET;
$where['order_sn'] = $data['out_trade_no'];
// $where['money'] = $data['total_amount'];
$res = $this->changeOrderStatus($where,$data['trade_no'],3,'pc支付宝支付');
if($res) {
$this->redirect('http://bifangjia.cn/Compare_Price/pay/PayResult.html');
} else {
$this->redirect('http://bifangjia.cn');
}
}
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/7/23
* Time: 15:19
*/
namespace api\portal\controller;
use api\portal\model\AgencyModel;
use api\portal\model\MemberModel;
use api\portal\model\MoneyDetailModel;
use api\portal\model\OrderModel;
use api\portal\model\UserNeedModel;
use cmf\controller\HomeBaseController;
use think\Db;
class NotifyCommonController extends HomeBaseController
{
/**
* 支付回调处理方法(通用)
* @param where 回调订单的where条件(含订单号,支付金额)
* @param transaction_id 支付业务参数
* @param pay_type 支付类型
* @param title 支付原因
*/
protected function changeOrderStatus($where, $transaction_id, $pay_type, $title) {
$order_model = new OrderModel();
$orderInfo = $order_model->where($where)->find();
if($orderInfo['status'] == 1) {
// $order_model->startTrans();
$update = [
'status' => 2,
'pay_type' => $pay_type,
'pay_time' => time(),
'transaction_id'=>$transaction_id
];
Db::name('Test')->insertGetId(['data'=>0]);
// 根据支付类型修改不同表的数据
if($orderInfo['type'] == 1) {
$user_need_model = new UserNeedModel();
$pay = $order_model->where($where)->update($update);
$status = $user_need_model->where(['id'=>$orderInfo['need_id']])->update(['status'=>2,'pay_time'=>time()]);
}
if($orderInfo['type'] == 2) {
$agency_model = new AgencyModel();
$pay = $order_model->where($where)->update($update);
$status = $agency_model->where(['id'=>$orderInfo['need_id']])->update(['status'=>2,'pay_time'=>time()]);
}
if($orderInfo['type'] == 3) {
$Intermediary = controller('Intermediary','controller');
$agencyOffer = $Intermediary->agencyOffer($orderInfo['user_id'],$orderInfo['need_id']);
$update['lower_price'] = $agencyOffer['money'];
$pay = $order_model->where($where)->update($update);
$status = 1;
}
if(!$pay || !$status) {
$order_model->rollback();
return false;
}
// 记录用户金额记录
$member_model = new MemberModel();
$user = $member_model->field(['id,type'])->where(['id'=>$orderInfo['user_id']])->find();
if(!$user) {
$order_model->rollback();
return false;
}
$money_detail_model = new MoneyDetailModel();
if($orderInfo['type'] == 1) {
$order_type = 1;
}
$insert = [
'order_id' => $orderInfo['id'],
'user_id' => $user['id'],
'user_type' => $user['type'],
'title' => $title,
'money' => $orderInfo['money'],
'order_type' => $order_type,
'type' => 2,
'pay_type' => $pay_type,
'create_time' => time()
];
$money_detail_insert = $money_detail_model->insertGetId($insert);
if(!$money_detail_insert) {
$order_model->rollback();
return false;
}
$order_model->commit();
return $orderInfo['type'];
}
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/6/13
* Time: 17:53
*/
namespace api\portal\controller;
use api\portal\controller\NotifyCommonController;
class NotifyController extends NotifyCommonController
{
// 微信支付回调
public function notify() {
require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Api.php";
require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Notify.php";
require_once VENDOR_PATH.'WxpayAPI/example/log.php';
$post = $this->request->param();
if($post==null){
$post = file_get_contents("php://input");
if($post == null){
$post = $GLOBALS['HTTP_RAW_POST_DATA'];
}
}
if(!empty($post)) {
$xml = $post; //微信的回调数据
$base = new \WxPayResults(); //实例化数据对象结果类
$data = $base->FromXml($xml);
if($base->CheckSign() == true){
if ($data["return_code"] == "SUCCESS") {
$where['order_sn'] = $data['attach'];
if (!$where['order_sn']) {
$where['order_sn'] = $data['out_trade_no'];
}
// $where['money'] = $data['total_fee']/100;
$result = $this->changeOrderStatus($where,$data['transaction_id'],1,'pc微信扫码支付');
if($result) {
$this->return_success();
}
}
}
}
}
/*
* 给微信发送确认订单金额和签名正确,SUCCESS信息
*/
private function return_success(){
$return['return_code'] = 'SUCCESS';
$return['return_msg'] = 'OK';
$xml_post = '<xml>
<return_code>'.$return['return_code'].'</return_code>
<return_msg>'.$return['return_msg'].'</return_msg>
</xml>';
echo $xml_post;exit;
}
}
\ No newline at end of file
... ...