作者 杨育虎

支付

  1 +<?php
  2 +
  3 +namespace app\portal\controller;
  4 +
  5 +use cmf\controller\HomeBaseController;
  6 +use think\Db;
  7 +use think\Log;
  8 +use think\Validate;
  9 +
  10 +/**
  11 + * 微信支付,退款,提现DEMO
  12 + * Class PayController
  13 + * @package app\portal\controller
  14 + */
  15 +class PayController extends HomeBaseController
  16 +{
  17 +
  18 + /**
  19 + * 微信支付
  20 + */
  21 + public function index(){
  22 +
  23 + }
  24 +
  25 + /**
  26 + * 支付回调
  27 + */
  28 + public function notify(){
  29 +
  30 + }
  31 +
  32 + /**
  33 + * 退款
  34 + * @throws \WxPayException
  35 + */
  36 + public function refund(){
  37 + $param = $this->request->param();
  38 + $signature = $param['s'];
  39 + $arithmetic['timeStamp']= $param['t'];
  40 + $arithmetic['randomStr']= $param['r'];
  41 + $arithmetic['orderSn']= $param['o'];
  42 + $str = arithmetic($arithmetic);
  43 + if($str != $signature){
  44 + $this->error('签名验证失败');
  45 + }
  46 +
  47 + /*微信退款*/
  48 + require_once EXTEND_PATH."WxpayAPI/lib/WxPay.Api.php";
  49 + require_once EXTEND_PATH."WxpayAPI/example/log.php";
  50 + if(isset($info["transaction_id"]) && empty($info["transaction_id"])){
  51 + $transaction_id = $info["transaction_id"];
  52 + $total_fee = $info["real_price"]*100;
  53 + $refund_fee = $info["real_price"]*100;
  54 + $input = new \WxPayRefund();
  55 + $input->SetTransaction_id($transaction_id);
  56 + $input->SetTotal_fee($total_fee);
  57 + $input->SetRefund_fee($refund_fee);
  58 + $input->SetOut_refund_no(\WxPayConfig::MCHID.date("YmdHis"));
  59 + $input->SetOp_user_id(\WxPayConfig::MCHID);
  60 + $ret=\WxPayApi::refund($input);
  61 + if($ret['result_code']=='SUCCESS'){
  62 + //todo 退款成功处理
  63 + $this->success('退款成功');
  64 + }else{
  65 + $this->error("退款失败:".$ret['err_code_des']);
  66 + }
  67 + }else{
  68 + $this->error('缺少退款标识');
  69 + }
  70 +
  71 + }
  72 +
  73 + /**
  74 + * 提现
  75 + */
  76 + public function withdraw_cash(){
  77 + $param = $this->request->param();
  78 + $signature = $param['s'];
  79 + $arithmetic['timeStamp']= $param['t'];
  80 + $arithmetic['randomStr']= $param['r'];
  81 + $arithmetic['orderSn']= $param['o'];
  82 + $str = arithmetic($arithmetic);
  83 + if($str != $signature){
  84 + $this->error('签名验证失败');
  85 + }
  86 + $price=1;
  87 + $openid='';
  88 +
  89 + $merch=new \MerchPay();
  90 + $trade_no = cmf_get_order_sn();
  91 + $res=$merch->pay($openid,$trade_no,$price,'提现');
  92 + if($res['result_code']=='SUCCESS'){
  93 + //todo 提现成功处理
  94 + }else{
  95 + $this->error('操作失败:'.$res['return_msg']);
  96 + }
  97 +
  98 + }
  99 +
  100 +}