📖 File Reader
<?php
namespace app\common;
use app\api\common\Common_config;
use app\BaseController;
/**
* 汇付第三方支付
* 地址:https://paas.huifu.com/partners/api#/
*/
class Common_huifu extends BaseController
{
protected $product_id;//产品号
protected $huifu_id;//商户号
protected $sys_id;//系统号
protected $rsaPublicKey;//公钥
protected $rsaPrivateKey;//私钥
protected function initialize()
{
$config = Common_config::config("'huifu_product_id','huifu_upper_huifu_id','huifu_huifu_id','huifu_sys_id','huifu_rsaPublicKey','huifu_rsaPrivateKey'");
$this->product_id = $config["huifu_product_id"];
$this->huifu_id = $config["huifu_huifu_id"];
$this->sys_id = $config["huifu_sys_id"];
$this->rsaPublicKey = $config["huifu_rsaPublicKey"];
$this->rsaPrivateKey = $config["huifu_rsaPrivateKey"];
}
/**
* 汇付支付宝支付
* @param $user_miyao 会员秘钥
* @param $order_sn 订单编号
* @param $order_price 订单金额
* @param $order_type 订单类型
* @return void
*/
public function pay_alipay($user_miyao='',$order_sn='',$order_price=0,$order_type=0)
{
$url = 'https://api.huifu.com/v2/trade/payment/jspay';
$post['req_date'] = date('Ymd');
$post['req_seq_id'] = $order_sn;//请求流水号
$post['huifu_id'] = $this->huifu_id;//商户号
$post['trade_type'] = 'A_NATIVE';//支付方式 T_MINIAPP:app微信小程序 A_NATIVE: 支付宝正扫 U_NATIVE: 银联正扫
$post['trans_amt'] = sprintf("%.2f",$order_price);//交易金额
//$post['delay_acct_flag'] = 'Y'; //延时到账
$post['goods_desc'] = '订单支付';//商品描述
$post['notify_url'] = 'http://'.$_SERVER["SERVER_NAME"].'/api/Order/notify_url_huifu/user_miyao/'.$user_miyao.'/order_sn/'.$order_sn.'/order_type/'.$order_type;
$request_data = $this->request_post($post,$url);
$request_data = json_decode($request_data,true);
if ($request_data['data']['resp_code'] == "00000100"){
return $this->succeed_json("ok",$request_data,202);
}else{
return $this->error_json($request_data['data']['resp_desc']);
}
}
//请求
public function request_post($post,$url){
header('Content-Type: text/html; charset=utf-8'); //网页编码
$rsaPrivateKey = $this->rsaPrivateKey;
//请求头部
$headers = array('Content-Type:application/json');
//公共参数
$body = array();
$body['sys_id'] = $this->sys_id;
$body['product_id'] = $this->product_id;
ksort($post); // 根据key排序
$body['data'] = $post;
//加密
$data_post = json_encode($post, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
$key = "-----BEGIN PRIVATE KEY-----\n".wordwrap($rsaPrivateKey, 64, "\n", true)."\n-----END PRIVATE KEY-----";
$signature= '';
openssl_sign($data_post, $signature, $key, 'SHA256');
# 执行签名
$sign = base64_encode($signature);
$body['sign'] = $sign;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($body));
# 拼装请求头
$httpHeaders = $this->createHeaders($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
# 执行网络请求
$result = curl_exec($ch);
return $result ;
}
//头部信息
public function createHeaders($header_data = array()){
$headers = $header_data;
if (empty($header_data)){
$headers = array('Content-type: application/x-www-form-urlencoded');
}
array_push($headers, 'sdk_version:php#v2.0.1');
array_push($headers, 'charset:UTF-8');
return $headers;
}
//加密
function jiami($post, $padding=OPENSSL_PKCS1_PADDING){
//app微信小程序
$rsaPublicKey = $this->rsaPublicKey;
$key = "-----BEGIN PUBLIC KEY-----\n".wordwrap($rsaPublicKey, 64, "\n", true)."\n-----END PUBLIC KEY-----";
$encryptResult= '';
try {
openssl_public_encrypt($post, $encryptResult, $key, $padding);
} catch (\Exception $e) {
echo $e->getMessage();
}
return base64_encode($encryptResult);
}
}