支付demo2

  1 <?php
  2 
  3 namespace App\Http\Controllers;
  4 use Illuminate\Http\Request;
  5 
  6 /**
  7  *pc微信支付宝第三方支付类
  8  */
  9 class PayController extends Controller
 10 {
 11     /**
 12      *支付宝第三方支付
 13      * $no        订单号
 14      * $money     订单金额
 15      * $title     订单标题
 16      * $info      订单详情
 17      */
 18     public function alipayPay($no, $money, $title, $info){
 19         //引入sdk
 20         require_once "pay/alipaysdk/AopSdk.php";
 21         $aop = new \AopClient();
 22         $aop->gatewayUrl = self::$GATEWAY_URL;
 23         $aop->appId = self::$APP_ID;
 24         $aop->rsaPrivateKey = self::$RSA_PRIVATE_KEY;
 25         $aop->apiVersion = self::$API_VERSION;
 26         $aop->signType = self::$SIGN_TYPE;
 27         $aop->postCharset= self::$POST_CHARSET;
 28         $aop->format= self::$FORMAT;
 29         $aop->alipayrsaPublicKey = self::$ALIPAY_RSA_PUBLIC_KEY;
 30         $request = new \AlipayTradePagePayRequest();
 31         $request->setReturnUrl(route('web.alipay.return'));
 32         $request->setNotifyUrl(route('web.alipay.notify'));
 33         $request->setBizContent('{"product_code":"FAST_INSTANT_TRADE_PAY","out_trade_no":"这是订单号","subject":"这是商品名称","total_amount":"订单价格","body":"订单详情"}');
 34         //请求
 35         $result = $aop->pageExecute ($request);
 36         //输出
 37         echo $result;
 38     }
 39     //支付宝同步通知
 40     public function aliReturn(){
 41         //引入sdk
 42         require_once "pay/alipaysdk/AopSdk.php";
 43 
 44         $aop = new \AopClient();
 45         $aop->alipayrsaPublicKey = self::$ALIPAY_RSA_PUBLIC_KEY;
 46         //此处验签方式必须与下单时的签名方式一致
 47         $flag = $aop->rsaCheckV1($_GET, NULL, "RSA2");
 48         //验签通过后再实现业务逻辑,比如修改订单表中的支付状态。
 49         if ($flag){
 50             //处理业务逻辑
 51 
 52             echo '商家页面';
 53         }else{
 54             echo '支付错误';
 55         }
 56     }
 57     //支付宝异步通知
 58     public function aliNotify(){
 59         //引入sdk
 60         require_once "pay/alipaysdk/AopSdk.php";
 61         $aop = new \AopClient();
 62         $aop->alipayrsaPublicKey = self::$ALIPAY_RSA_PUBLIC_KEY;
 63         //此处验签方式必须与下单时的签名方式一致
 64         $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
 65         //验签通过后再实现业务逻辑,比如修改订单表中的支付状态。
 66         /**
 67          * ①验签通过后核实如下参数out_trade_no、total_amount、seller_id
 68          * ②修改订单表
 69          **/
 70         //打印success,应答支付宝。必须保证本界面无错误。只打印了success,否则支付宝将重复请求回调地址。
 71         //echo 'success';
 72         if ($flag){
 73             //处理业务逻辑
 74 
 75             echo "success";
 76         }else{
 77             echo "fail";
 78         }
 79     }
 80     /**
 81      * 第三方微信支付
 82      * $no      订单号
 83      * $money   订单金额
 84      * $title   订单标题
 85      * $info    订单详情
 86      */
 87     public function wechatPay($no, $money, $title, $info){
 88 //引入sdk
 89         require_once "pay/Wxpay/example/WxPay.Api.php";
 90         $notify = new \NativePay();
 91         $input = new \WxPayUnifiedOrder();
 92         $input->SetBody("test");//商品描述
 93         $input->SetAttach("test");//附加数据
 94         $input->SetOut_trade_no('123456789');//订单号唯一
 95         $input->SetTotal_fee("1");//商品价格
 96         $input->SetTime_start(date("YmdHis"));//开始时间
 97         $input->SetTime_expire(date("YmdHis", time() + 600)); //交易结束时间
 98         $input->SetNotify_url(route('web.wxpay.notify'));//通知地址
 99         $input->SetTrade_type("NATIVE");//交易类型
100         $input->SetProduct_id("123456789");//商品id
101         $result = $notify->GetPayUrl($input);
102         $url = $result["code_url"];
103         return "http://paysdk.weixin.qq.com/example/qrcode.php?data=" . $url;
104     }
105 
106     //回调函数
107     public function wxNotify() {
108         require_once "pay/Wxpay/example/WxPay.Api.php";
109         //file_put_contents("/tmp/wechat.log",$GLOBALS['HTTP_RAW_POST_DATA']);
110         try{
111             $notify = new \NativeNotifyCallBack();
112             $notify->Handle(true);
113 
114         }catch(\Exception $e){
115             throw $e;
116         }
117     }
118     /**
119      * 支付宝提现
120      */
121     public function alipayTx($money,$no, $ali_no, $ali_name){
122         //引入sdk
123         require_once "pay/alipaysdk/AopSdk.php";
124         $aop = new \AopClient ();
125         $aop->gatewayUrl = self::$GATEWAY_URL;
126         $aop->appId = self::$APP_ID;
127         $aop->rsaPrivateKey = self::$RSA_PRIVATE_KEY;
128         $aop->alipayrsaPublicKey= self::$ALIPAY_RSA_PUBLIC_KEY;
129         $aop->apiVersion = self::$API_VERSION;
130         $aop->signType = self::$SIGN_TYPE;
131         $aop->postCharset= self::$POST_CHARSET;
132         $aop->format= self::$FORMAT;
133         $request = new \AlipayFundTransToaccountTransferRequest ();
134 
135         $request->setBizContent("{" .
136             "\"out_biz_no\":\"$no\"," .
137             "\"payee_type\":\"ALIPAY_LOGONID\"," .
138             "\"payee_account\":\"$ali_no\"," .
139             "\"amount\":\"$money\"," .
140             "\"payer_show_name\":\"弘商城提现\"," .
141             "\"payee_real_name\":\"$ali_name\"," .
142             "\"remark\":\"弘商城支付宝提现\"" .
143             "  }");
144         $result = $aop->execute( $request);
145         $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
146         $resultCode = $result->$responseNode->code;
147         $out_no = $result->$responseNode->out_biz_no;
148         if(!empty($resultCode)&&$resultCode == 10000){
149             //处理逻辑
150 
151             $this->data = true;
152         } else {
153             $this->data = false;
154         }
155         return $this->data;
156     }
157 
158     /**
159      * 微信提现
160      */
161     public function wechatTx($openid,$money,$realname,$no){
162         header('content-type:text/html;charset=utf-8');
163         $data['mch_appid']= self::$WX_APP_ID;//商户的应用appid
164         $data['mchid']= self::$WX_MCH_ID;//商户ID
165         $data['nonce_str']=$this->unicode();//unicode();//
166         $data['partner_trade_no']="$no";//.time();//
167         $data['openid']=$openid;//
168         $data['check_name']='NO_CHECK';//
169         $data['re_user_name']=$realname;//
170         $data['amount']=floatval($money*100);//
171         $data['desc']='用户提现';//
172         $data['spbill_create_ip']=$_SERVER['SERVER_ADDR'];//
173         $secrect_key= self::$WX_SECRET_KEY;///
174         $data=array_filter($data);
175         ksort($data);
176         $str='';
177         foreach($data as $k=>$v) {
178             $str.=$k.'='.$v.'&';
179         }
180         $str.='key='.$secrect_key;
181         $data['sign']=md5($str);
182         $xml=$this->arraytoxml($data);
183         $url= self::$WX_URL;
184         $res=$this->pem_curl($xml,$url);
185         $return = $this->xmltoarray($res);
186         if ($return['return_code'] == 'SUCCESS' && $return['result_code'] == 'SUCCESS'){
187             //处理逻辑
188             return true;
189         }else{
190             return false;
191         }
192     }
193     //需要证书的curl
194     function pem_curl($param="",$url) {
195         $postUrl = $url;
196         $curlPost = $param;
197         $ch = curl_init();                                      //初始化curl
198         curl_setopt($ch, CURLOPT_URL,$postUrl);                 //抓取指定网页
199         curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
200         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
201         curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
202         curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);           // 增加 HTTP Header(头)里的字段
203         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
204         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
205         curl_setopt($ch,CURLOPT_SSLCERT,app_path().'/Utils/Wxpay/apiclient_cert.pem'); //这个是证书的位置
206         curl_setopt($ch,CURLOPT_SSLKEY,app_path().'/Utils/Wxpay/apiclient_key.pem'); //这个也是证书的位置
207         $data = curl_exec($ch);                                 //运行curl
208         //返回结果
209         if($data){
210             curl_close($ch);
211             return $data;
212         }
213         else {
214             $error = curl_errno($ch);
215             echo "curl出错,错误码:$error"."<br>";
216             curl_close($ch);
217             return false;
218         }
219     }
220     //微信辅助函数
221     function unicode() {
222         $str = uniqid(mt_rand(),1);
223         $str=sha1($str);
224         return md5($str);
225     }
226     function arraytoxml($data){
227         $str='<xml>';
228         foreach($data as $k=>$v) {
229             $str.='<'.$k.'>'.$v.'</'.$k.'>';
230         }
231         $str.='</xml>';
232         return $str;
233     }
234     function xmltoarray($xml) {
235         //禁止引用外部xml实体
236         libxml_disable_entity_loader(true);
237         $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
238         $val = json_decode(json_encode($xmlstring),true);
239         return $val;
240     }
241 }

猜你喜欢

转载自www.cnblogs.com/liyanxi/p/12059377.html