/**
* 预支付请求接口(POST)
* @param string $openid openid
* @param string $body 商品简单描述
* @param string $order_sn 订单编号
* @param string $total_fee 金额
* @return json的数据
*/
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 , //$order_no, //订单号 商户订单号
'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' ; //签名算法,暂支持 MD5
$data [ 'package' ] = 'prepay_id=' . $xml_to_arr [ 'prepay_id' ]; //统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
$data [ 'paySign' ] = $this ->makeSign( $tmp ); //签名,具体签名方案参见微信公众号支付帮助文档;$data['out_trade_no'] = $out_trade_no;
$return [ 'status' ] = 1;
$return [ 'info' ] = $data ;
} else {
//失败
$return [ 'status' ] = -1;
$return [ 'info' ] = $xml_to_arr ;
}
exit (json_encode( $return ));
}
//curl请求
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 ;
}
|
请发表评论