作者 郭鑫

支付

  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: 29925
  5 + * Date: 2018/6/14
  6 + * Time: 16:05
  7 + */
  8 +
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\controller\NotifyCommonController;
  12 +use think\Db;
  13 +use traits\controller\Jump;
  14 +use api\portal\model\OrderModel;
  15 +
  16 +class AlipayController extends NotifyCommonController
  17 +{
  18 + public function _initialize()
  19 + {
  20 + require_once VENDOR_PATH.'alipay/config.php';
  21 + require_once VENDOR_PATH.'alipay/pagepay/service/AlipayTradeService.php';
  22 + require_once VENDOR_PATH.'alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';
  23 + $this->config = $config;
  24 + }
  25 +
  26 +
  27 + public function alipay() {
  28 + $data = $this->request->param();
  29 + $config = $this->config;
  30 + //商户订单号,商户网站订单系统中唯一订单号,必填
  31 + $out_trade_no = trim($data['order_sn']);
  32 +
  33 + //订单名称,必填
  34 + $subject = trim($data['name']);
  35 +
  36 + //付款金额,必填
  37 +// $total_amount = trim(I('get.price'));
  38 + $total_amount = trim(0.01);
  39 +
  40 + //商品描述,可空
  41 + $body = '';
  42 +
  43 + //构造参数
  44 + $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
  45 + $payRequestBuilder->setBody($body);
  46 + $payRequestBuilder->setSubject($subject);
  47 + $payRequestBuilder->setTotalAmount($total_amount);
  48 + $payRequestBuilder->setOutTradeNo($out_trade_no);
  49 +
  50 + $aop = new \AlipayTradeService($config);
  51 +
  52 + /**
  53 + * pagePay 电脑网站支付请求
  54 + * @param $builder 业务参数,使用buildmodel中的对象生成。
  55 + * @param $return_url 同步跳转地址,公网可以访问
  56 + * @param $notify_url 异步通知地址,公网可以访问
  57 + * @return $response 支付宝返回的信息
  58 + */
  59 + $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);
  60 +
  61 + //输出表单
  62 + var_dump($response);
  63 + }
  64 +
  65 + // 回调
  66 + public function notify_url() {
  67 + $config = $this->config;
  68 + $arr=$_POST;
  69 + $alipaySevice = new \AlipayTradeService($config);
  70 + $alipaySevice->writeLog(var_export($_POST,true));
  71 + $result = $alipaySevice->check($arr);
  72 + if($result) {
  73 + //商户订单号
  74 +
  75 + $out_trade_no = $_POST['out_trade_no'];
  76 +
  77 + //支付宝交易号
  78 +
  79 + $trade_no = $_POST['trade_no'];
  80 +
  81 + //交易状态
  82 + $trade_status = $_POST['trade_status'];
  83 +
  84 +
  85 + if($_POST['trade_status'] == 'TRADE_FINISHED') {
  86 +
  87 + //判断该笔订单是否在商户网站中已经做过处理
  88 + //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
  89 + //请务必判断请求时的total_amount与通知时获取的total_fee为一致的
  90 + //如果有做过处理,不执行商户的业务程序
  91 + $where['order_sn'] = $out_trade_no;
  92 +// $where['money'] = $data['total_amount'];
  93 + $res = $this->changeOrderStatus($where,$trade_no,3,'pc支付宝支付');
  94 + if(!$res) {
  95 + echo 'fail';exit;
  96 + }
  97 + //注意:
  98 + //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
  99 + }
  100 + else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
  101 + //判断该笔订单是否在商户网站中已经做过处理
  102 + //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
  103 + //请务必判断请求时的total_amount与通知时获取的total_fee为一致的
  104 + //如果有做过处理,不执行商户的业务程序
  105 + $where['order_sn'] = $out_trade_no;
  106 +// $where['money'] = $data['total_amount'];
  107 + $res = $this->changeOrderStatus($where,$trade_no,3,'pc支付宝支付');
  108 + if(!$res) {
  109 + echo 'fail';exit;
  110 + }
  111 + //注意:
  112 + //付款完成后,支付宝系统发送该交易状态通知
  113 + }
  114 + echo "success"; //请不要修改或删除
  115 + }else {
  116 + //验证失败
  117 + echo "fail"; //请不要修改或删除
  118 + }
  119 + }
  120 +
  121 + //
  122 + public function return_url() {
  123 + $config = $this->config;
  124 + $arr = $_GET;
  125 + $alipaySevice = new \AlipayTradeService($config);
  126 + $result = $alipaySevice->check($arr);
  127 +// var_dump($_GET);
  128 +// echo '<br/>';
  129 + if ($result) {
  130 + //商户订单号
  131 + $out_trade_no = htmlspecialchars($_GET['out_trade_no']);
  132 +
  133 + //支付宝交易号
  134 + $trade_no = htmlspecialchars($_GET['trade_no']);
  135 + $data = $_GET;
  136 + $where['order_sn'] = $data['out_trade_no'];
  137 +// $where['money'] = $data['total_amount'];
  138 + $res = $this->changeOrderStatus($where,$data['trade_no'],3,'pc支付宝支付');
  139 + if($res) {
  140 + $this->redirect('http://bifangjia.cn/Compare_Price/pay/PayResult.html');
  141 + } else {
  142 + $this->redirect('http://bifangjia.cn');
  143 + }
  144 + }
  145 + }
  146 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: 29925
  5 + * Date: 2018/7/23
  6 + * Time: 15:19
  7 + */
  8 +
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\model\AgencyModel;
  12 +use api\portal\model\MemberModel;
  13 +use api\portal\model\MoneyDetailModel;
  14 +use api\portal\model\OrderModel;
  15 +use api\portal\model\UserNeedModel;
  16 +use cmf\controller\HomeBaseController;
  17 +use think\Db;
  18 +
  19 +class NotifyCommonController extends HomeBaseController
  20 +{
  21 +
  22 + /**
  23 + * 支付回调处理方法(通用)
  24 + * @param where 回调订单的where条件(含订单号,支付金额)
  25 + * @param transaction_id 支付业务参数
  26 + * @param pay_type 支付类型
  27 + * @param title 支付原因
  28 + */
  29 + protected function changeOrderStatus($where, $transaction_id, $pay_type, $title) {
  30 + $order_model = new OrderModel();
  31 + $orderInfo = $order_model->where($where)->find();
  32 + if($orderInfo['status'] == 1) {
  33 +// $order_model->startTrans();
  34 + $update = [
  35 + 'status' => 2,
  36 + 'pay_type' => $pay_type,
  37 + 'pay_time' => time(),
  38 + 'transaction_id'=>$transaction_id
  39 + ];
  40 + Db::name('Test')->insertGetId(['data'=>0]);
  41 + // 根据支付类型修改不同表的数据
  42 + if($orderInfo['type'] == 1) {
  43 + $user_need_model = new UserNeedModel();
  44 + $pay = $order_model->where($where)->update($update);
  45 + $status = $user_need_model->where(['id'=>$orderInfo['need_id']])->update(['status'=>2,'pay_time'=>time()]);
  46 + }
  47 + if($orderInfo['type'] == 2) {
  48 + $agency_model = new AgencyModel();
  49 + $pay = $order_model->where($where)->update($update);
  50 + $status = $agency_model->where(['id'=>$orderInfo['need_id']])->update(['status'=>2,'pay_time'=>time()]);
  51 + }
  52 + if($orderInfo['type'] == 3) {
  53 + $Intermediary = controller('Intermediary','controller');
  54 + $agencyOffer = $Intermediary->agencyOffer($orderInfo['user_id'],$orderInfo['need_id']);
  55 + $update['lower_price'] = $agencyOffer['money'];
  56 + $pay = $order_model->where($where)->update($update);
  57 + $status = 1;
  58 + }
  59 + if(!$pay || !$status) {
  60 + $order_model->rollback();
  61 + return false;
  62 + }
  63 + // 记录用户金额记录
  64 + $member_model = new MemberModel();
  65 + $user = $member_model->field(['id,type'])->where(['id'=>$orderInfo['user_id']])->find();
  66 + if(!$user) {
  67 + $order_model->rollback();
  68 + return false;
  69 + }
  70 + $money_detail_model = new MoneyDetailModel();
  71 + if($orderInfo['type'] == 1) {
  72 + $order_type = 1;
  73 + }
  74 + $insert = [
  75 + 'order_id' => $orderInfo['id'],
  76 + 'user_id' => $user['id'],
  77 + 'user_type' => $user['type'],
  78 + 'title' => $title,
  79 + 'money' => $orderInfo['money'],
  80 + 'order_type' => $order_type,
  81 + 'type' => 2,
  82 + 'pay_type' => $pay_type,
  83 + 'create_time' => time()
  84 + ];
  85 + $money_detail_insert = $money_detail_model->insertGetId($insert);
  86 + if(!$money_detail_insert) {
  87 + $order_model->rollback();
  88 + return false;
  89 + }
  90 + $order_model->commit();
  91 + return $orderInfo['type'];
  92 + }
  93 + }
  94 +
  95 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: 29925
  5 + * Date: 2018/6/13
  6 + * Time: 17:53
  7 + */
  8 +
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\controller\NotifyCommonController;
  12 +
  13 +class NotifyController extends NotifyCommonController
  14 +{
  15 +
  16 + // 微信支付回调
  17 + public function notify() {
  18 + require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Api.php";
  19 + require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Notify.php";
  20 + require_once VENDOR_PATH.'WxpayAPI/example/log.php';
  21 +
  22 + $post = $this->request->param();
  23 + if($post==null){
  24 + $post = file_get_contents("php://input");
  25 + if($post == null){
  26 + $post = $GLOBALS['HTTP_RAW_POST_DATA'];
  27 + }
  28 + }
  29 + if(!empty($post)) {
  30 + $xml = $post; //微信的回调数据
  31 + $base = new \WxPayResults(); //实例化数据对象结果类
  32 + $data = $base->FromXml($xml);
  33 + if($base->CheckSign() == true){
  34 + if ($data["return_code"] == "SUCCESS") {
  35 + $where['order_sn'] = $data['attach'];
  36 + if (!$where['order_sn']) {
  37 + $where['order_sn'] = $data['out_trade_no'];
  38 + }
  39 +// $where['money'] = $data['total_fee']/100;
  40 + $result = $this->changeOrderStatus($where,$data['transaction_id'],1,'pc微信扫码支付');
  41 + if($result) {
  42 + $this->return_success();
  43 + }
  44 + }
  45 + }
  46 + }
  47 + }
  48 +
  49 + /*
  50 + * 给微信发送确认订单金额和签名正确,SUCCESS信息
  51 + */
  52 + private function return_success(){
  53 + $return['return_code'] = 'SUCCESS';
  54 + $return['return_msg'] = 'OK';
  55 + $xml_post = '<xml>
  56 + <return_code>'.$return['return_code'].'</return_code>
  57 + <return_msg>'.$return['return_msg'].'</return_msg>
  58 + </xml>';
  59 + echo $xml_post;exit;
  60 + }
  61 +}