寫在最前面的話:
最近由于業(yè)務(wù)需求,每日需要對接各種各樣的支付通道(掃碼支付,H5支付,網(wǎng)關(guān)支付)(又分為微信支付,支付寶支付,銀聯(lián)支付,京東支付等等),雖然每個公司的文檔或業(yè)務(wù)邏輯都不一樣,但是都大同小異,所以整理了一份通用的支付對接類,愿可以幫助到需要的朋友,本人能力有限,也有可能有考慮不周的地方,或者需要改進的,如果你有更好的想法或者建議,可以在下方留言,或者聯(lián)系本人!
private $payType = 21;
private $payMethod = 2;
private $prefix = 'HH_';
private $channel = "某某支付";
private $scanApi = 'scanPayApi'; //掃碼支付
private $h5Api = 'h5PayApi'; //H5支付
private $getwayApi = 'gatewayPayApi'; //網(wǎng)關(guān)支付
/**------------------------以下更換為自己的邏輯------------------------------*/
public function index( Request $request )
{
if ( $request -> has( [ 'amount' , 'uid' , 'goods_id' ] ) )
{
$amount = $request -> input( 'amount' );
$uid = $request -> input( 'uid' );
$goods_id = $request -> input( 'goods_id' );
/**
*
此處編寫自己的邏輯
$url = env( 'JYPAY_GETWAY' );
$u[ "id" ] = $data[ 'orderId' ];
$u[ "uid" ] = $uid;
$u[ "name" ] = XMAgentUserData ::getPersonalDetails( $uid )[ 0 ][ 'name' ];
$u[ "goods_id" ] = $goods_id;
$u[ "pay_amount" ] = $amount;
$u[ "ip_address" ] = $this->getIp();
$u[ "pay_name" ] = "華悅支付";
OrderForm ::insertData( $u );
return redirect() -> away( $url );
*
*/
}
return response() -> json( [
'code' => 1001 ,
'msg' => 'no' ,
] );
}
public function notify( Request $request )
{
$data = $this->dataReturn($request);
if ($data['tradeStatus'] == 1 && $sign == $data['sign'])
{
$url = env( 'RESPONSE_PAY' ) . $data[ 'fxddh' ] . '&uid=' . $u[ 0 ][ 'uid' ];
$this->requestCurl($url);
return 'success';
}
return response() -> json( [
'code' => 1001 ,
'msg' => 'no' ,
] );
}
public function callback()
{
return "This is a lonely page";
}
/**處理支付前要統(tǒng)一或必備的參數(shù),建議放簽名的數(shù)據(jù)
*
* @param $amount
*
* @return array
*/
public function dataPooled( $amount )
{
$data = [
"fxid" => env( 'JYPAY_MERCHANT_NUM' ) , //商戶號
"fxddh" => $this -> prefix . $this -> payType . time() , //商戶訂單號
"fxdesc" => "test" , //商品名
"fxfee" => $amount , //支付金額 單位元
"fxnotifyurl" => env( 'JYPAY_NOTIFY' ) , //異步回調(diào) , 支付結(jié)果以異步為準(zhǔn)
"fxbackurl" => env( 'JYPAY_RETURN' ) , //同步回調(diào) 不作為最終支付結(jié)果為準(zhǔn),請以異步回調(diào)為準(zhǔn)
"fxpay" => $this -> payType , //支付類型 此處可選項以網(wǎng)站對接文檔為準(zhǔn)
"fxip" => $this -> getIp() //支付端ip地址
];
return $data;
}
/** 處理異步回調(diào)后的數(shù)據(jù)
*
* @param Request $request
*
* @return array
*/
public function dataReturn( Request $request )
{
$data = [
'fxid' => $request -> input( 'fxid' ) ,
'fxddh' => $request -> input( 'fxddh' ) ,
'fxorder' => $request -> input( 'fxorder' ) ,
'fxdesc' => $request -> input( 'fxdesc' ) ,
'fxfee' => $request -> input( 'fxfee' ) ,
'fxstatus' => $request -> input( 'fxstatus' ) ,
'fxtime' => $request -> input( 'fxtime' ) ,
'fxsign' => $request -> input( 'fxsign' ) ,
];
return $data;
}
/**------------------------以下是常用的。有需求更改,無需求可不動------------------------------*/
/**切換支付API通道
* 默認(rèn)為H5支付
*
* @param string $type
*
* @return mixed
*/
public function apiSwitch( $type = '' )
{
switch ( $type ) {
case 1:
return $this -> scanApi;
case 2:
return $this -> getwayApi;
default:
return $this -> h5Api;
}
}
/**------------------------以下是公共的,可直接使用------------------------------*/
/** url編碼和解碼
* 默認(rèn)為解碼,
*
* @param $strOrUrl
* @param string $type
*
* @return string
*/
public function urlCode( $strOrUrl ,$type = '' )
{
return empty($type) ? urldecode($strOrUrl): urlencode($strOrUrl);
}
/** 解碼
*
* @param $json
* @param bool $assoc
*
* @return mixed
*/
public function jsonDecode( $json ,$assoc = false)
{
return json_decode($json,$assoc);
}
/**編碼
*
* @param $str
*
* @return false|string
*/
public function jsonEncode($str)
{
return json_encode($str);
}
/**數(shù)組拼接字符串
*
* @param $array
*
* @return bool|string
*/
public function arrayJoinStr( $array )
{
$md5str = "";
foreach ( $array as $key => $val ) {
$md5str .= $key . "=" . $val . "&";
}
return $md5str;
}
/**拼接后字符串去除最后的&符號
*
* @param $str
*
* @return bool|string
*/
public function strTrim( $str )
{
return substr( $str , 0 , -1 );
}
/**大小寫轉(zhuǎn)換,默認(rèn)為大寫,有值則小寫
*
* @param $str
* @param string $type
*
* @return string
*/
public function strCase( $str ,$type = '')
{
return empty($type) ? strtoupper($str) : strtolower($str);
}
/**數(shù)組按照鍵名排序
*
* @param $array
*
* @return bool
*/
public function dataSort( &$array )
{
return ksort($array);
}
/**加密
*
* @param $str
*
* @return string
*/
public function dataMd5( $str )
{
return md5( $str );
}
/**16進制編碼轉(zhuǎn)字符串
*
* @param $hex
*
* @return string
*/
public function hexToString( $hex )
{
$string = '';
for ( $i = 0 ; $i < strlen( $hex ) - 1 ; $i += 2 ) {
$string .= chr( hexdec( $hex[ $i ] . $hex[ $i + 1 ] ) );
}
return $string;
}
/**字符串轉(zhuǎn)16進制編碼
*
* @param $string
*
* @return string
*/
public function stringToHex( $string )
{
$hex = '';
for ( $i = 0 ; $i < strlen( $string ) ; $i ++ ) {
$hex .= dechex( ord( $string[ $i ] ) );
}
return $hex;
}
/**獲取真實IP
* @return array|false|string
*/
public function getIp()
{
if ( getenv( "HTTP_CLIENT_IP" ) && strcasecmp( getenv( "HTTP_CLIENT_IP" ) , "unknown" ) )
$ip = getenv( "HTTP_CLIENT_IP" );
else if ( getenv( "HTTP_X_FORWARDED_FOR" ) && strcasecmp( getenv( "HTTP_X_FORWARDED_FOR" ) , "unknown" ) )
$ip = getenv( "HTTP_X_FORWARDED_FOR" );
else if ( getenv( "REMOTE_ADDR" ) && strcasecmp( getenv( "REMOTE_ADDR" ) , "unknown" ) )
$ip = getenv( "REMOTE_ADDR" );
else if ( isset( $_SERVER[ 'REMOTE_ADDR' ] ) && $_SERVER[ 'REMOTE_ADDR' ] && strcasecmp( $_SERVER[ 'REMOTE_ADDR' ] , "unknown" ) )
$ip = $_SERVER[ 'REMOTE_ADDR' ];
else
$ip = "unknown";
return ( $ip );
}
/** @模擬請求
*
* @param $url
* @param array $postArray
* @param string $method
* @param string $content_type
*
* @return bool|string
*
*/
public function requestCurl( $url , $postArray = [] , $method = 'GET' , $content_type = '' )
{
$str = '';
if ( !empty( $url ) ) {
try {
$ch = curl_init();
curl_setopt( $ch , CURLOPT_URL , $url );
curl_setopt( $ch , CURLOPT_HEADER , 0 );
curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt( $ch , CURLOPT_TIMEOUT , 30 ); //30秒超時
curl_setopt( $ch , CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt( $ch , CURLOPT_HTTPHEADER , $this -> getHeaderContentType( $content_type ) );
if ( strtoupper( $method ) == 'POST' ) {
curl_setopt( $ch , CURLOPT_POST , 1 );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $this -> dataBuild( $postArray ) );
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //是否驗證對等證書,1則驗證,0則不驗證
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$str = curl_exec( $ch );
curl_close( $ch );
} catch ( Exception $e ) {
$str = '';
}
}
return $str;
}
/**
* 數(shù)組生成一個經(jīng)過 URL-encode 的請求字符串
* ["name"=>"callback" , "value"=>"test"] => name=callback&value=test
*
* @param $array
*
* @return string
*/
public function dataBuild( $array )
{
return is_array($array) ? http_build_query($array) : $array;
}
/**
* @XML數(shù)據(jù)格式
* @XJSON數(shù)據(jù)格式
* @Xform表單數(shù)據(jù)編碼 k=>v
*
* @param $type
*
* @return array
*/
public function getHeaderContentType( $type )
{
switch ($type){
case 'xml':
$type = "application/xml; charset=utf-8";
case 'json':
$type = "application/json; charset=utf-8";
case 'html':
$type = "text/html; Charset=utf-8";
default:
$type = "application/x-www-form-urlencoded; charset=utf-8";
}
return [ "Content-Type:$type" ];
}
public function generateStr( $length = 4 )
{
if ( $length < 1 || $length > 30 ) {
return false;
}
$chars = [
'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'k' , 'm' , 'n' , 'p' , 'x' , 'y' , 'z' ,
'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'K' , 'M' , 'N' , 'P' , 'X' , 'Y' , 'Z' ,
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
];
$str = join( '' , array_rand( array_flip( $chars ) , $length ) );
return $str;
}