123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace app\common\library;
- use think\Controller;
- //汇聚银联h5、支付宝h5
- class HuijuH5Pay extends Controller
- {
- public function __construct(){
- $this->mch_no_trade = "888117500004672";
- //报备商户号
- $this->TradeMerchantNo = "777171600367612";
- //商户私钥
- $this->mchPrivateKey = "40c310e1a575470cb660f08937bf6313";
- }
- //银联h5
- public function ylh5Pay($order_no,$ReturnUrl,$notifyurl,$good_name,$amount,$m_id,$type='1'){
- if ($type==1){
- $q1_fr = "ALIPAY_H5";
- }elseif ($type==2){
- $q1_fr = "UNIONPAY_H5";
- }
- //1.版本号
- $params["p0_Version"] = "2.1";
- //2.商户编号
- $params["p1_MerchantNo"] = $this->mch_no_trade;
- //3.商户订单号
- $params["p2_OrderNo"] = $order_no;
- //4.订单金额
- $params["p3_Amount"] = $amount;
- //5.交易币种
- $params["p4_Cur"] = "1";
- //6.商品名称
- $params["p5_ProductName"] = $good_name;
- //7.商品描述
- $params["p6_ProductDesc"] = "";
- //8.公用回传参数
- $params["p7_Mp"] = $m_id;
- //9.商户页面通知地址
- $params["p8_ReturnUrl"] = $ReturnUrl;
- //10.服务器异步通知地址
- $params["p9_NotifyUrl"] = $notifyurl;
- //11.交易类型
- $params["q1_FrpCode"] = $q1_fr;
- //18.微信 H5 模 式
- //$params["q9_TransactionModel"] = "MODEL1"; //新增
- //19.报备商户号
- $params["qa_TradeMerchantNo"] = $this->TradeMerchantNo; //新增
- $hmacVal = urlencode($this->hmacRequest($params,$this->mchPrivateKey));
- $params['hmac'] = $hmacVal;
- $url = 'https://www.joinpay.com/trade/uniPayApi.action';
- $result = http_post($url,$params);
- $result = json_decode($result,true);
- //dump($result);die;
- return $result;
- }
- /**
- * 生成签名
- * @param $params
- * @param $key
- * @param string $encryptType
- * @return string
- */
- function hmacRequest($params, $key, $encryptType = "1")
- {
- if ("1" == $encryptType) {
- return md5(implode("", $params) . $key);
- } else {
- $private_key = openssl_pkey_get_private($key);
- $params = implode("", $params);
- openssl_sign($params, $sign, $private_key, OPENSSL_ALGO_MD5);
- openssl_free_key($private_key);
- $sign = base64_encode($sign);
- return $sign;
- }
- }
- /**
- * 批量代付生成签名,由于批量代付包含details的字段,需要特殊处理
- * @param $params
- * @param $key
- * @param string $encryptType
- * @return string
- */
- function batchHmacRequest($params, $key, $encryptType = "1")
- {
- $details=$params['details'];
- unset($params['details']);
- $detailsStr='';
- foreach ($details as $k=>$value){
- $detailsStr.= implode("", $value);
- }
- if ("1" == $encryptType) {
- return md5(implode("", $params) . $detailsStr . $key);
- } else {
- $private_key = openssl_pkey_get_private($key);
- $params = implode("", $params).$detailsStr;
- openssl_sign($params, $sign, $private_key, OPENSSL_ALGO_MD5);
- openssl_free_key($private_key);
- $sign = base64_encode($sign);
- return $sign;
- }
- }
- //生成订单号
- function getOrderId()
- {
- return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
- }
- /**
- * 生成批量代付detail
- *
- *$detail=array(
- * 0=>array(
- * 1, //detailId
- * '张三',//payee
- * '6228480402564890018',//'bankacount'
- * '0.1',// 'money'
- * '代付接口测试1',//'desc'
- * '广州市',// 'city'
- * 0,//'isVerify'
- * 1,//'PublicOrPrivate'
- * 102902500019,// 'num'
- * )
- *
- * )
- * return str
- *
- * example
- * 代付明细序号(必填)|收款人姓名(必填)|收款银行帐号(必填)|代付金额(必填)|代付说明|收款银行所在城市(必填)|是否审核(1:审核 0:不审核)|对公对私(1:对公 2:对私)|联行号(对公必填)
- * 1001|张三|6228480402564890018|10.01|代付接口测试1|广州市|1|1|102902500019^1001|李四|6228480402564890018|10.01|代付接口测试2|广州市|1|1|102902500019
- **/
- function getProxyPayDetail(Array $detail)
- {
- $result = '';
- foreach ($detail as $key => $item) {
- if ($key != 0) {
- $result .= '^';
- }
- $result .= implode('|', array_values($item));
- }
- return $result;
- }
- function decodeUnicode($str)
- {
- return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
- create_function(
- '$matches',
- 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
- ),
- $str);
- }
- function output($str){
- echo '<pre>'.decodeUnicode(json_encode(json_decode($str), JSON_PRETTY_PRINT)).'</pre>';
- }
- /**
- * 判断文件是是否是图片
- * @param $fileName
- * @param $fileTmpName
- * @return bool true|false
- */
- function checkIsImage($fileName,$fileTmpName){
- if( !in_array(substr(strrchr ( $fileName , "." ),1), array('jpg','png','bmp','jpeg')) || !getimagesize($fileTmpName)){
- return false;
- }
- return true;
- }
- /**
- * 获取图片的base64
- * @param $file $_FILES["file"]["tmp_name"]
- * @param $type png/
- * @return string
- */
- function getImageBase64($file,$type){
- $bin_string = file_get_contents($file);
- return 'data:' . $type . ';base64,' . base64_encode($bin_string);
- }
- /**
- * 生成批次id
- * @param $length
- * @return string
- */
- function getBatchId()
- {
- return "batch".getOrderId();
- }
- }
|