<?php
/**
* 微信支付類
*
*/
class WechatCN extends Action{
public function getBill($params) {
$request = new Request('/download_bill', $params);
$request->get();
}
public function getPayment($params) {
$request = new Request('/payment', $params);
$request->get();
}
public function refund($params) {
$request = new Request('/payment_refund', $params);
$request->post();
}
public function getRefund($params) {
$request = new Request('/payment_refund', $params);
$request->get();
}
public function appPay($params) {
// $params = $this->__isCNY($params);
$request = new Request('/wechatpay/app/payment', $params);
$request->post();
}
public function mpPay($params) {
// $params = $this->__isCNY($params);
$request = new Request('/mp_pay', $params);
$request->post();
}
public function miniProgramPay($params) {
// $params = $this->__isCNY($params);
$request = new Request('/wechatpay/mini_program/payment', $params);
$request->post();
}
public function consumerScanWeb($params) {
// $params = $this->__isCNY($params);
$request = new Request('/wechatpay/web/payment', $params);
// unset( $request->pay_config);
$request->post();
}
public function consumerScanDevice($params) {
// $params = $this->__isCNY($params);
$request = new Request('/wechatpay/qrcode/payment', $params);
$request->post();
}
public function merchantScanConsumer($params) {
// $params = $this->__isCNY($params);
$request = new Request('/wechatpay/barcode/payment', $params);
$request->post();
}
public function getRate($params) {
$request = new Request('/wechatpay/forex_rate', $params);
$request->get();
}
public function declaration($params) {
$request = new Request('/wechatpay/declaration', $params);
$request->post();
}
public function redeclaration($params) {
$request = new Request('/wechatpay/redeclaration', $params);
$request->post();
}
public function getDeclaration($params) {
$request = new Request('/wechatpay/declaration', $params);
$request->get();
}
}
/*
*阿里支付**/
class Alipay{
public function getBill($params) {
$request = new Request('/download_bill', $params);
$request->get();
}
public function getPayment($params) {
$request = new Request('/payment', $params);
$request->get();
}
public function refund($params) {
$request = new Request('/payment_refund', $params);
$request->post();
}
public function getRefund($params) {
$request = new Request('/payment_refund', $params);
$request->get();
}
public function appPay($params) {
// $params = $this->__isCNY($params);
$request = new Request('/alipay/app/payment', $params);
$request->post();
}
public function wapPay($params) {
// $params = $this->__isHK($params);
// $params = $this->__isCNY($params);
$request = new Request('/alipay/wap/payment', $params);
$request->post();
}
public function consumerScanWeb($params) {
// $params = $this->__isHK($params);
$request = new Request('/alipay/web/payment', $params);
$request->post();
}
public function consumerScanMerchant($params) {
// $params = $this->__isHK($params);
$request = new Request('/alipay/qrcode/payment', $params);
$request->post();
}
public function merchantScanConsumer($params) {
// $params = $this->__isHK($params);
$request = new Request('/alipay/barcode/payment', $params);
$request->post();
}
public function getRate($params) {
$request = new Request('/alipay/forex_rate', $params);
$request->get();
}
public function declaration($params) {
$request = new Request('/alipay/declaration', $params);
$request->post();
}
public function redeclaration($params) {
$request = new Request('/alipay/redeclaration', $params);
$request->post();
}
public function getDeclaration($params) {
$request = new Request('/alipay/declaration', $params);
$request->get();
}
//支付寶二維碼收款
public function codePay($params){
$request = new Request('/alipay/scanned_pay', $params);
$request->post();
}
}
class Request
{
protected $_config = [];
const REQUEST_TIME = 10;
public function __construct($api_url, $params) {
$this->pay_config = array(
'HP_HOST' => 'https://testapi.wisecashier.com',
'MERCHANT_NO' => '商戶號',
'VERSION' => '1.0',
'PRIVATE_KEY_PATH' => '所在路徑/private.key',
'MERCHANT_APPID' => '',//微信APPID
// 'MERCHANT_MINI_APPID' => '去微信公衆平臺, 登陸小程序帳號, 進入開發設置獲取'
);
$this->_config['url'] = sprintf("%s%s", $this->pay_config['HP_HOST'], $api_url);
$this->_config['params'] = $params;
$this->_config['timestamp'] = time();
$this->_config['signature'] = $this->sign();
// $this->_config['signature'] = sign($params, $this->_config['timestamp'], $this->pay_config['PRIVATE_KEY_PATH']);
$this->_config['header'] = [
"Version:" . $this->pay_config['VERSION'] ,
"MerchantNo:" . $this->pay_config['MERCHANT_NO'],
"Signature:" . $this->_config['signature'],
"Timestamp:" . $this->_config['timestamp']
];
if($api_url == '/download_bill') {
$this->_config['header'] = [
"Version:3.0" ,
"MerchantNo:" . $this->pay_config['MERCHANT_NO'],
"Signature:" . $this->_config['signature'],
"Timestamp:" . $this->_config['timestamp']
];
}
// echo json_encode($this->_config, JSON_PRETTY_PRINT);
// exit;
}
/**
* 內部調用get請求
* @return object | bool
*/
public function get() {
$data = $this->_config['params'];
$url = $this->_config['url'];
if (is_array($data) && count($data) > 0) {
$param = http_build_query($data);
$url = (!strpos($url, '?')) ? $url . "?" . $param : $url . "&" . $param;
}
$rs = $this->_httpGet($url);
// echo $rs;
return $rs;
}
/**
* 內部調用post請求
* @param $withFiles
* @return object | bool
*/
public function post($withFiles = false) {
header("Content-type:text/html;charset=utf-8");
$rs = $this->_httpPost('POST', $withFiles);
// echo $rs;
print_r(json_decode($rs,true));
return $rs;
}
/**
* 內部調用put請求
* @param $withFiles
* @return object | bool
*/
public function put($withFiles = false) {
$rs = $this->_httpPost('PUT', $withFiles);
// echo $rs;
return $rs;
}
/**
* 內部調用delete請求
* @return object | bool
*/
public function del() {
$rs = $this->_httpPost('DELETE');
// echo $rs;
return $rs;
}
/**
* curl-get請求
* @param $url
* @return object | bool
*/
protected function _httpGet($url = "") {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->_config['header']);
curl_setopt($curl, CURLOPT_TIMEOUT, self::REQUEST_TIME);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
return $this->returnData($res, $info);
}
/**
* curl-post請求
* @param $request_type
* @param $withFiles
* @return object | bool
*/
protected function _httpPost($request_type = 'POST', $withFiles = false) {
// echo "\nAPI--->" . $this->_config['url'] . "\n";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->_config['url']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, self::REQUEST_TIME);
if ($withFiles) {
$boundary = '---------------'.time().rand(1000, 99999);
$this->_config['header']['Content-Type'] = "multipart/form-data;boundary=" . $boundary;
}
// 判斷提交類型
switch ($request_type) {
case 'POST':
curl_setopt($curl, CURLOPT_POST, true);
break;
case 'PUT':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
break;
case 'DELETE':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default:
return Message::msg(904);
break;
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->_config['params']);
if ($this->_config['header']) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->_config['header']);
}
$res = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
return $this->returnData($res, $info);
}
/**
* 返回數據
* @param $res
* @param $info
* @return object
*/
protected function returnData($res, $info) {
if ($info['http_code'] == 200) {
return $res;
} elseif ($info['http_code'] == 500 || $info['http_code'] == 0 ) {
return Message::msg(500);
} else {
return Message::msg(903);
}
}
public function sign() {
$signature_str = '';
$params = $this->_config['params'];
$prikey = openssl_pkey_get_private(file_get_contents($this->pay_config['PRIVATE_KEY_PATH']));
ksort($params);
foreach ($params as $key => $item) {
if (strlen($signature_str) == 0) {
$signature_str .= $key . '=' . rawurlencode($item);
} else {
$signature_str .= '&' . $key . '=' . rawurlencode($item);
}
}
$signature_str = utf8_encode($signature_str);
$signature_str .= ',' . strval($this->_config['timestamp']);
openssl_sign($signature_str, $sign, $prikey, OPENSSL_ALGO_SHA256);
$sign = base64_encode($sign);
return $sign;
}
}
class Message
{
const DEFINE_MSG_NUM = '000';
public static function msg($msg_num = null) {
if(is_null($msg_num) || !is_numeric($msg_num))
$msg_num = self::DEFINE_MSG_NUM;
return json_encode(self::_matchMsg((string)$msg_num));
}
protected static function _matchMsg($msg_num) {
$msg_list = self::_msgList();
$msg_num = array_key_exists($msg_num, $msg_list) ? $msg_num : self::DEFINE_MSG_NUM;
return $msg_list[$msg_num];
}
protected static function _msgList() {
return [
'000' => ['meta' => ['status_code' => 0, 'message' => '提示碼錯誤,請從新設置', 'success' => false]],
'200' => ['meta' => ['status_code' => 200, 'message' => '成功', 'success' => true]],
'404' => ['meta' => ['status_code' => 404, 'message' => '未找到該頁面', 'success' => false]],
'500' => ['meta' => ['status_code' => 500, 'message' => '服務器響應失敗', 'success' => false]],
'304' => ['meta' => ['status_code' => 304, 'message' => '頁面重定向', 'success' => true]],
'901' => ['meta' => ['status_code' => 901, 'message' => '祕鑰路徑錯誤', 'success' => false]],
'902' => ['meta' => ['status_code' => 902, 'message' => '請求超時', 'success' => false]],
'903' => ['meta' => ['status_code' => 903, 'message' => '請求錯誤,請重試', 'success' => false]],
'904' => ['meta' => ['status_code' => 904, 'message' => '請求類型錯誤', 'success' => false]],
];
}
public static function ifMsg($message, $contents) {}
}
function sign($data, $timestamp, $private_key) {
$prikey = openssl_pkey_get_private(file_get_contents($private_key));
ksort($data);
$signature_str = '';
foreach ($data as $key => $item) {
if (strlen($signature_str) == 0) {
$signature_str .= $key . '=' . rawurlencode($item);
} else {
$signature_str .= '&' . $key . '=' . rawurlencode($item);
}
}
$signature_str = utf8_encode($signature_str);
$signature_str .= ',' . strval($timestamp);
$alg = OPENSSL_ALGO_SHA256;
openssl_sign($signature_str, $sign, $prikey, $alg);
$sign = base64_encode($sign);
return $sign;
}
?>