1. 程式人生 > 實用技巧 >支付寶支付封裝

支付寶支付封裝

  1 <?php
  2 namespace pay;
  3 
  4 /**
  5  * 支付寶支付
  6  */
  7 class  Alipay
  8 {
  9     // 公私鑰 2048 位
 10     private $config = [
 11         "app_id"=>"*****",
 12         "rsaPrivateKey"=>"*****",//私鑰
 13     ];
 14 
 15     private $charset = 'utf-8';
 16     private $sign_type = 'RSA2';
17 private $version = '1.0'; 18 19 20 private $calltype = 'app';//呼叫埠 app mobile pc 21 private $submitUrl = "https://openapi.alipay.com/gateway.do" ;//提交地址 22 // private $submitUrl = "https://openapi.alipaydev.com/gateway.do";//測試地址 23 private $submit_auto = true; 24 25 public function
__construct($calltype='',$config=[]){ 26 $this->calltype=$calltype!=''?$calltype:$this->calltype; 27 if($config) 28 { 29 $this->config=$config; 30 } 31 } 32 public function index($data) 33 { 34 // 埠檢測 35 $res=$this
->calltypeCheck($data); 36 if($res['code']==0){ return $res; } 37 38 $calltype=$this->calltype; 39 return $this->$calltype($data); 40 } 41 42 public function app($data) 43 { 44 $str=$this->keyandvalStr($this->postdata($data),1); 45 Flog($str); 46 // $str=$this->submitUrl.'?'.$str; 47 // //返回連結不能帶官方https連結 48 return ['code'=>1,'data'=>['type'=>'app','str'=>$str]]; 49 } 50 51 public function pc($data) 52 { 53 $html=$this->formsubmithtml($this->submitUrl,$this->postdata($data)); 54 // var_dump($html); 55 return ['code'=>1,'data'=>['type'=>'pc','html'=>$html]]; 56 } 57 58 public function mobile($data) 59 { 60 // dump($data); 61 $str=$this->keyandvalStr($this->postdata($data),1); 62 $url="https://openapi.alipay.com/gateway.do?".$str; 63 return ['code'=>1,'data'=>['type'=>'mobile','url'=>$url]]; 64 } 65 66 67 // 獲取資料 68 public function postdata($data) 69 { 70 $calltypeD=$this->calltypeD(); 71 72 $return_url=$data['return_url']; 73 $notify_url=$data['notify_url']; 74 75 // 對subiect進行 & 刪除 76 $subject=str_replace("&","",$data['subject']); 77 78 $biz_content=[ 79 'body'=>'', 80 'subject'=>$subject, 81 'out_trade_no'=>$data['pay_sn'], 82 'timeout_express'=>'30m', 83 'total_amount'=>$data['total'], 84 'product_code'=>$calltypeD['product_code'], 85 "goods_type"=>1,//商品型別 0虛擬商品 1實物商品 86 ]; 87 $post=[ 88 'app_id'=>$this->config['app_id'], 89 'method'=>$calltypeD['method'], 90 'format'=>'json', 91 'charset'=>$this->charset, 92 'sign_type'=>$this->sign_type, 93 'return_url'=>$return_url, 94 'sign'=>'', 95 'timestamp'=>date('Y-m-d H:i:s'), 96 'version'=>$this->version, 97 'notify_url'=>$notify_url, 98 'biz_content'=>json_encode($biz_content), 99 ]; 100 101 $signdata=$this->alisign($post); 102 103 return $signdata; 104 } 105 106 // 根據型別獲取想要的資訊 107 public function calltypeD($field='') 108 { 109 $D=[ 110 'app'=>['product_code'=>'QUICK_MSECURITY_PAY','method'=>'alipay.trade.app.pay'], 111 'pc'=>['product_code'=>'FAST_INSTANT_TRADE_PAY','method'=>'alipay.trade.page.pay'], 112 'mobile'=>['product_code'=>'QUICK_WAP_WAY','method'=>'alipay.trade.wap.pay'], 113 ]; 114 if($field!='') 115 { 116 return isset($D[$this->calltype])?$D[$this->calltype][$field]:''; 117 }else{ 118 return isset($D[$this->calltype])?$D[$this->calltype]:[]; 119 } 120 121 122 } 123 124 // 埠檢測 125 public function calltypeCheck($data) 126 { 127 // 對配置資訊進行檢測 128 if($this->config['app_id']=='') { return ['code'=>0,'msg'=>'請先配置 app_id']; } 129 if($this->config['rsaPrivateKey']=='') { return ['code'=>0,'msg'=>'請先配置 rsaPrivateKey']; } 130 131 return ['code'=>1,'msg'=>'檢測通過']; 132 } 133 134 135 /************************** 支付寶公用方法 ************************************/ 136 137 public function alisign($data) 138 { 139 $rsaPrivateKey=$this->config['rsaPrivateKey']; 140 $buff = ""; 141 ksort($data); 142 $buff=$this->keyandvalStr($data); 143 $str = chunk_split($rsaPrivateKey, 64, "\n"); 144 $res = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n"; 145 146 $sign=$this->ras2Sign($buff,$res); 147 $data['sign']=$sign; 148 return $data; 149 } 150 public function keyandvalStr($data,$flg=0) 151 { 152 $buff=""; 153 foreach ($data as $k => $v) { 154 if($flg!=0 && $v != "" && !is_array($v)) 155 { 156 $v=urlencode($v); 157 $buff .= $k . "=" . $v . "&"; 158 }else{ 159 if($k != "sign" && $v != "" && !is_array($v)){ 160 $buff .= $k . "=" . $v . "&"; 161 } 162 } 163 } 164 $buff = trim($buff, "&"); 165 return $buff; 166 } 167 168 public function ras2Sign($buff,$res) 169 { 170 $sign=''; 171 openssl_sign($buff, $sign, $res, OPENSSL_ALGO_SHA256); 172 $sign = base64_encode($sign); 173 return $sign; 174 } 175 public function formsubmithtml($action,$data) 176 { 177 $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$action."' method='POST'>"; 178 foreach ($data as $key => $value) { 179 $val = str_replace("'","&apos;",$value); 180 $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; 181 } 182 $sHtml = $sHtml."<input type='submit' class='paysubmit' value='ok' ></form>"; 183 if($this->submit_auto==true) 184 { 185 $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>"; 186 } 187 188 return $sHtml; 189 } 190 191 192 193 194 195 196 197 198 199 200 }

之後會持續修改更新。。。。。