作者 潘浩文
1 个管道 的构建 通过 耗费 0 秒

个人中心接口设计

  1 +<?php
  2 +
  3 +namespace app\portal\controller;
  4 +
  5 +use cmf\controller\HomeBaseController;
  6 +use EasyWeChat\Foundation\Application;
  7 +use EasyWeChat\Payment\Order;
  8 +use think\Db;
  9 +
  10 +/**
  11 + * 微信支付,退款,提现DEMO
  12 + * Class PayController
  13 + * @package app\portal\controller
  14 + */
  15 +class PayController extends HomeBaseController
  16 +{
  17 + protected $options;
  18 + function _initialize()
  19 + {
  20 + parent::_initialize();
  21 + $this->options = [
  22 + 'app_id' => config('app_id'),
  23 + 'secret' => config('app_secret'),
  24 + 'wx_mch_id' => config('wx_mch_id'),
  25 + 'wx_pay_key' => config('wx_pay_key'),
  26 + ];
  27 + }
  28 +
  29 + /**
  30 + * 微信支付
  31 + */
  32 + public function index(){
  33 + $attributes = [
  34 + 'trade_type' => 'JSAPI',
  35 + 'body' => '百荣科技',
  36 + 'detail' => '以客户为中心 以奋斗者文本',
  37 + 'out_trade_no' => cmf_get_order_sn(),
  38 + 'total_fee' => 1, // 单位:分
  39 + 'notify_url' => url('portal/pay/notify','','',true), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  40 + 'openid' => cmf_get_current_user_openid(), // trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识,
  41 + ];
  42 + $order = new Order($attributes);
  43 +
  44 + $app = new Application($this->options);
  45 + $payment = $app->payment;
  46 + $result = $payment->prepare($order);
  47 +
  48 + if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
  49 + $prepayId = $result->prepay_id;
  50 + $jsApiParameters=$payment->configForJSSDKPayment($prepayId);
  51 + $this->assign('jsApiParameters',json_encode($jsApiParameters));
  52 + return $this->fetch();
  53 + }else{
  54 + $this->error('支付参数错误','',$result);
  55 + }
  56 +
  57 + }
  58 +
  59 + /**
  60 + * 联系支付回调
  61 + * @throws \EasyWeChat\Core\Exceptions\FaultException
  62 + */
  63 + public function notify(){
  64 + cache('nnn',111);
  65 + $app = new Application($this->options);
  66 + $response = $app->payment->handleNotify(function($notify, $successful){
  67 + cache('notify',$notify);
  68 + cache('successful',$successful);
  69 + /*这里是支付回调逻辑处理,一下是DEMO*/
  70 +
  71 +// // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  72 + $out_trade_no=$notify->out_trade_no;
  73 + $percent= Db::name('config')->where('id',1)->find()['contact_percent'];
  74 + $order = Db::name('contact_order')->where('order_sn',$out_trade_no)->find();
  75 + $openid=Db::name('third_party_user')->where('user_id',$order['user_id'])->find()['openid'];
  76 + if (!$order) { // 如果订单不存在
  77 + return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  78 + }
  79 +// // 如果订单存在
  80 +// // 检查订单是否已经更新过支付状态
  81 + if ($order['status']==2) { // 假设订单字段“支付时间”不为空代表已经支付
  82 + return true; // 已经支付成功了就不再更新了
  83 + }
  84 +//
  85 +// // 用户是否支付成功
  86 + if ($successful) {
  87 + // 回填微信的订单号
  88 +// $update['transaction_id']=$notify->transaction_id;
  89 +// $update['pay_time']=time();
  90 + // 不是已经支付状态则修改为已经支付状态
  91 + $update['status'] = 2;
  92 +//
  93 +// $app = new Application($this->options);
  94 +// $merchantPay = $app->merchant_pay;
  95 +// $merchantPayData = [
  96 +// 'partner_trade_no' => cmf_get_order_sn(), //随机字符串作为订单号,跟红包和支付一个概念。
  97 +// 'openid' => $openid, //收款人的openid
  98 +// 'check_name' => 'NO_CHECK', //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
  99 +// 're_user_name'=>'张三', //OPTION_CHECK FORCE_CHECK 校验实名的时候必须提交
  100 +// 'amount' => $percent*$order['money'], //单位为分
  101 +// 'desc' => '企业付款',
  102 +// 'spbill_create_ip' => '192.168.0.1', //发起交易的IP地址
  103 +// ];
  104 +// $merchantPay->send($merchantPayData);
  105 +// Db::name('refund')->insert(['type'=>2,'order_sn'=>$merchantPayData['partner_trade_no'],'create_time'=>time(),'money'=>$percent*$order['money'],'user_id'=>$order['user_id']]);
  106 +
  107 + } else { // 用户支付失败
  108 + $update['status']=3;
  109 + }
  110 + Db::name('contact_order')->where('order_sn',$out_trade_no)->update($update);
  111 + return true; // 返回处理完成
  112 + });
  113 + }
  114 +
  115 +public function test11(){
  116 + var_dump(cache('successful'));
  117 + var_dump(cache('nnn'));
  118 +}
  119 +
  120 + /**
  121 + * 订单支付回调
  122 + * @throws \EasyWeChat\Core\Exceptions\FaultException
  123 + */
  124 + public function notify2(){
  125 + cache('nnn',111);
  126 + $app = new Application($this->options);
  127 + $response = $app->payment->handleNotify(function($notify, $successful){
  128 + cache('notify',$notify);
  129 + cache('successful',$successful);
  130 + /*这里是支付回调逻辑处理,一下是DEMO*/
  131 +
  132 + // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  133 + $out_trade_no=$notify->out_trade_no;
  134 + $order = Db::name('order')->where('order_sn',$out_trade_no)->find();
  135 + if (!$order) { // 如果订单不存在
  136 + return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  137 + }
  138 +// // 如果订单存在
  139 +// // 检查订单是否已经更新过支付状态
  140 + if ($order['status']==2&&$order['to_user_status']==2&&$order['to_post_status']==1) { // 假设订单字段“支付时间”不为空代表已经支付
  141 + return true; // 已经支付成功了就不再更新了
  142 + }
  143 +
  144 + // 用户是否支付成功
  145 + if ($successful) {
  146 + // 回填微信的订单号
  147 +// $update['transaction_id']=$notify->transaction_id;
  148 +// $update['pay_time']=time();
  149 + // 不是已经支付状态则修改为已经支付状态
  150 + $update['status'] = 2;
  151 + $update['to_user_status']=2;
  152 + $update['to_post_status']=1;
  153 + }
  154 + Db::name('order')->where('order_sn',$out_trade_no)->update($update);
  155 +
  156 + return true; // 返回处理完成
  157 + });
  158 +
  159 + $response->send();
  160 + }
  161 +
  162 + /**
  163 + * 查询订单
  164 + */
  165 + public function checkOrder(){
  166 + $app = new Application($this->options);
  167 + $payment = $app->payment;
  168 + $orderNo = "2018080397545210";//商户系统内部的订单号(out_trade_no)
  169 + $result=$payment->query($orderNo);
  170 + var_dump($result);
  171 + }
  172 +
  173 + /**
  174 + * 退款
  175 + */
  176 + public function refund(){
  177 + //todo 退款逻辑应该加入百荣签名验证规则,避免出现被盗用
  178 +// $param=$this->request->param();
  179 +// $signature = $param['s'];
  180 +// $arithmetic['timeStamp']= $param['t'];
  181 +// $arithmetic['randomStr']= $param['r'];
  182 +// $arithmetic['orderSn']= $param['o'];
  183 +// $str = arithmetic($arithmetic);
  184 +// if($str != $signature){
  185 +// $this->error('签名验证失败');
  186 +// }
  187 + $app = new Application($this->options);
  188 + $payment = $app->payment;
  189 + //使用商户订单号退款 PS.其他形式参考文档
  190 + $orderNo = "2018080397100101";//商户系统内部的订单号(out_trade_no)
  191 + $refundNo = cmf_get_order_sn();//退款单号
  192 + $result = $payment->refund($orderNo, $refundNo, 1, 1); // 总金额 100, 退款 80,refundFee可选(为空时全额退款)
  193 + var_dump($result);
  194 + }
  195 +
  196 + /**
  197 + * 查询退款
  198 + */
  199 + public function checkRefund(){
  200 + $app = new Application($this->options);
  201 + $payment = $app->payment;
  202 + $outTradeNo="2018080210097519";//商户系统内部的订单号(out_trade_no)
  203 + $result = $payment->queryRefund($outTradeNo);
  204 + var_dump($result);
  205 + }
  206 +
  207 + /**
  208 + * 退款结果回调
  209 + */
  210 + public function refundNotify() {
  211 + cache('test',123123);
  212 + $app = new Application($this->options);
  213 + $response = $app->payment->handleRefundNotify(function ($message, $reqInfo) {
  214 + cache('message',$message);
  215 + cache('reqInfo',$reqInfo);
  216 + // 其中 $message['req_info'] 获取到的是加密信息
  217 + // $reqInfo 为 message['req_info'] 解密后的信息
  218 + // 你的业务逻辑...
  219 + return true; // 返回 true 告诉微信“我已处理完成”
  220 + // 或返回错误原因 $fail('参数格式校验错误');
  221 + });
  222 +
  223 + $response->send();
  224 + }
  225 +
  226 + /**
  227 + * 红包
  228 + */
  229 + public function luckyMoney(){
  230 + //todo 退款逻辑应该加入百荣签名验证规则,避免出现被盗用
  231 + /* $param=$this->request->param();
  232 + $signature = $param['s'];
  233 + $arithmetic['timeStamp']= $param['t'];
  234 + $arithmetic['randomStr']= $param['r'];
  235 + $arithmetic['orderSn']= $param['o'];
  236 + $str = arithmetic($arithmetic);
  237 + if($str != $signature){
  238 + $this->error('签名验证失败');
  239 + }*/
  240 +
  241 + $app = new Application($this->options);
  242 + $luckyMoney = $app->lucky_money;
  243 + $luckyMoneyData = [
  244 + 'mch_billno' => 'xy123456',
  245 + 'send_name' => '测试红包',
  246 + 're_openid' => 'oxTWIuGaIt6gTKsQRLau2M0yL16E',
  247 + 'total_num' => 1, //普通红包固定为1,裂变红包不小于3
  248 + 'total_amount' => 100, //单位为分,普通红包不小于100,裂变红包不小于300
  249 + 'wishing' => '祝福语',
  250 + 'client_ip' => '192.168.0.1', //可不传,不传则由 SDK 取当前客户端 IP
  251 + 'act_name' => '测试活动',
  252 + 'remark' => '测试备注',
  253 + // ...
  254 + ];
  255 + //普通红包
  256 + $result = $luckyMoney->send($luckyMoneyData, \EasyWeChat\Payment\LuckyMoney\API::TYPE_NORMAL);
  257 + var_dump($result);
  258 + }
  259 +
  260 + /**
  261 + * 查询红包
  262 + */
  263 + public function checkLuckyMoney(){
  264 + $mchBillNo = "商户系统内部的订单号(mch_billno)";
  265 + $app = new Application($this->options);
  266 + $luckyMoney = $app->lucky_money;
  267 + $luckyMoney->query($mchBillNo);
  268 + }
  269 +
  270 + /**
  271 + * 生成签名DEMO
  272 + * @return string
  273 + */
  274 + public function getSignatureDemo(){
  275 + $timeStamp = time();
  276 + $randomStr = cmf_random_string(8);
  277 + $arithmetic['timeStamp']= $timeStamp;
  278 + $arithmetic['randomStr']= $randomStr;
  279 + $signature = arithmetic($arithmetic);
  280 + $notifyParam=array('t'=>$timeStamp,'r'=>$randomStr,'s'=>$signature);
  281 + $url=url('your url 表达式',$notifyParam,true,true);
  282 + return $url;
  283 + }
  284 +
  285 +
  286 +}