支付
public function prepay()
{
tp_log('预支付请求数据===>' . json_encode($_POST), 'prepay', request()->controller());
$goods_user = db('tf_goods_user')->where(array('order_no' => $_POST['order_no']))->find();
$goods = db('tf_goods')->where(array('id' => $goods_user['goods_id']))->find();
if (($goods['sales_initial'] - $goods['sales_actual']) <= 0) {
$return['status'] = 0;
$return['info'] = '此产品已售完';
exit(json_encode($return));
}
$order_no = $_POST['order_no'];
$is_sale = $_POST['is_sale'];
$openid = $_POST['openid'];
$goods_name = $_POST['goods_name'];
$pay_price = $_POST['price'];
$attach['is_sale'] = $_POST['is_sale'];
$attach['sale_id'] = $_POST['sale_id'];
$nonce_str = $this->nonce_str();
$order_no_ssh = $this->get_orderssh();
$data = [
'appid' => config('pay.APPID'),
'mch_id' => config('pay.MCHID'),
'nonce_str' => $nonce_str,
'body' => $goods_name,
'attach' => json_encode($attach),
'out_trade_no' => $order_no_ssh,
'total_fee' => intval($pay_price * 100),
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => config('pay.NOTIFY_URL'),
'trade_type' => 'JSAPI',
'openid' => $openid
];
$p_o_data['createtime'] = time();
$p_o_data['order_no'] = $order_no;
$p_o_data['order_no_ssh'] = $order_no_ssh;
$p_o_data['ready'] = json_encode($data);
$p_o_return = db('tf_pay_order')->insert($p_o_data);
if(!$p_o_return){
$return['status'] = -1;
$return['info'] = $p_o_data;
exit(json_encode($return));
}
$sign = $this->makeSign($data);
$data['sign'] = $sign;
$xml = $this->toXml($data);
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$prepay_return_reslut_xml = $this->http_request($url, $xml);
$xml_to_arr = $this->fromXml($prepay_return_reslut_xml);
$return_result = json_encode($xml_to_arr, true);
tp_log('预支付请求返回数据array===>' .$return_result , 'prepay', request()->controller());
db('tf_goods_order')->where(array('order_no' => $order_no))
->update(array(
'go_pay' => $return_result,
'updatetime' => time(),
'updateuser' => $openid
));
if($xml_to_arr['return_code'] == 'SUCCESS' && $xml_to_arr['result_code'] == 'SUCCESS'){
$time = time();
$tmp = [
'appId' => config('pay.APPID'),
'nonceStr' => $nonce_str,
'package' => 'prepay_id='.$xml_to_arr['prepay_id'],
'signType' => 'MD5',
'timeStamp' => "$time",
];
$data['timeStamp'] = "$time";
$data['nonceStr'] = $nonce_str;
$data['signType'] = 'MD5';
$data['package'] = 'prepay_id='.$xml_to_arr['prepay_id'];
$data['paySign'] = $this->makeSign($tmp);
$return['status'] = 1;
$return['info'] = $data;
}else{
$return['status'] = -1;
$return['info'] = $xml_to_arr;
}
exit(json_encode($return));
}
public function http_request($url, $data = null, $headers = array())
{
$curl = curl_init();
if (count($headers) >= 1) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
退款
public function refund()
{
$transaction_id = '4200000712202007274705432240';
$total_fee = '0.01';
$refund_fee = '0.01';
$time = time();
$nonceStr = md5($time . $transaction_id . $total_fee . $refund_fee);
$params = [
'appid' => config('pay.APPID'),
'mch_id' => config('pay.MCHID'),
'nonce_str' => $nonceStr,
'transaction_id' => $transaction_id,
'out_refund_no' => $time,
'total_fee' => $total_fee * 100,
'refund_fee' => $refund_fee * 100,
'notify_url' => config('pay.NOTIFY_URL_REFUND'),
];
$params['sign'] = $this->makeSign($params);
tp_log('退款请求数据===>' . json_encode($params), 'refund', request()->controller());
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$result = $this->post($url, $this->toXml($params), true, $this->getCertPem());
$prepay = $this->fromXml($result);
if (empty($result)) {
throw new BaseException(['msg' => '微信退款api请求失败']);
}
$prepay = $this->fromXml($result);
tp_log('退款返回数据===>' . json_encode($prepay), 'refund', request()->controller());
return true;
}
public function post($url, $data = [], $useCert = false, $sslCert = [])
{
$header = [
'Content-type: application/json;'
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
if ($useCert == true) {
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem']);
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem']);
}
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
订单查询
public function orderquery()
{
$transaction_id = '4200000712202007274705432240';
$time = time();
$nonce_str = md5($time . mt_rand(00000,99999));
$params = [
'appid' => config('pay.APPID'),
'mch_id' => config('pay.MCHID'),
'transaction_id' => $transaction_id,
'nonce_str' => $nonce_str,
];
$params['sign'] = $this->makeSign($params);
$url = 'https://api.mch.weixin.qq.com/pay/orderquery';
$result = $this->post($url, $this->toXml($params));
$prepay = $this->fromXml($result);
if ($prepay['return_code'] === 'FAIL') {
throw new BaseException(['msg' => $prepay['return_msg'], 'code' => 0]);
}
if ($prepay['result_code'] === 'FAIL') {
throw new BaseException(['msg' => $prepay['err_code_des'], 'code' => 0]);
}
return $prepay;
}
退款查询
public function refundquery()
{
$transaction_id = '4200000712202007274705432240';
$time = time();
$nonce_str = md5($time . mt_rand(00000,99999));
$params = [
'appid' => config('pay.APPID'),
'mch_id' => config('pay.MCHID'),
'transaction_id' => $transaction_id,
'nonce_str' => $nonce_str,
];
$params['sign'] = $this->makeSign($params);
$url = 'https://api.mch.weixin.qq.com/pay/refundquery';
$result = $this->post($url, $this->toXml($params));
$prepay = $this->fromXml($result);
dump($prepay);die;
if ($prepay['return_code'] === 'FAIL') {
throw new BaseException(['msg' => $prepay['return_msg'], 'code' => 0]);
}
if ($prepay['result_code'] === 'FAIL') {
throw new BaseException(['msg' => $prepay['err_code_des'], 'code' => 0]);
}
return $prepay;
}
支付成功回调
public function index()
{
$data = file_get_contents('php://input');
$data = $this->FromXml($data);
tp_log('支付回调数据===>' . json_encode($data), 'index', request()->controller());
$data_sign = $data['sign'];
unset($data['sign']);
$sign = $this->makeSign($data);
$str_success = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
$str_error = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
if (($sign === $data_sign) && ($data['return_code'] == 'SUCCESS') && ($data['result_code'] == 'SUCCESS')) {
}
echo $str_success;
}
退款成功回调
public function refund(){
$data = file_get_contents('php://input');
$data = $this->FromXml($data);
tp_log('退款回调数据===>' . json_encode($data), 'refund', request()->controller());
$req_info_xml = openssl_decrypt(base64_decode($data['req_info']), 'aes-256-ecb', md5(config('pay.KEY')),OPENSSL_RAW_DATA);
$req_info = $this->FromXml($req_info_xml);
}
用到的方法
private function makeSign($values)
{
ksort($values);
$string = $this->toUrlParams($values);
$string = $string . '&key=' . config('pay.KEY');
$string = md5($string);
$result = strtoupper($string);
return $result;
}
private function ToUrlParams($array)
{
$buff = "";
foreach ($array as $k => $v) {
if ($k != "sign" && $v != "" && !is_array($v)) {
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
private function toXml($values)
{
if (!is_array($values)
|| count($values) <= 0
) {
return false;
}
$xml = "<xml>";
foreach ($values as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
private function fromXml($xml)
{
libxml_disable_entity_loader(true);
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
private function getCertPem()
{
$filePath = EXTEND_PATH.'wxpay/cert/';
return [
'certPem' => $filePath . 'apiclient_cert.pem',
'keyPem' => $filePath . 'apiclient_key.pem'
];
}
public function get_orderssh()
{
return date("YmdHis") . mt_rand(10000000, 99999999);
}