⼩程序实现⽀付功能
⼩程序⽀付,没有封装⽀付代码:直接上⼀段可⽤的流程代码吧:
⼩程序⽀付官⽹⽂档有详细的说明,这⾥我就不再赘述啦:
客户端js:
url:'/order/store',//改成你⾃⼰的链接
header:{
'Content-Type':'application/x-www-form-urlencoded'
},
method:'POST',
success:function(res){
console.log(res.data);
console.log('调起⽀付');
'timeStamp': res.data.timeStamp,
'nonceStr': Str,
'package': res.data.package,代码转换
'signType':'MD5',
'paySign': res.data.paySign,
'success':function(res){
console.log('success');
wx.showToast({
title:'⽀付成功',
icon:'success',
duration:3000
});
},
'fail':function(res){
console.log('fail');
},
'complete':function(res){
console.log('complete');
}
});
},
fail:function(res){
console.log(res.data)
}
});
后端部分:laravel5:<?php
namespace App\Http\Controllers\Api\Order;
use App\Models\Order;
use App\Models\OrderGoods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Member;
use App\Models\MemberAddress;
use App\Models\Product;
use App\Http\Requests;
use App\Http\Requests\Interfaces\MemberCheck;
use Carbon\Carbon;
class OrderController extends Controller
{
use MemberCheck;
public function __construct()
{
$config = array(
'appid'        => env('WECHAT_APPID'),//⼩程序appid
'pay_mchid'    => env('WECH_ID'),//商户号
'pay_apikey' =>env('WECHAT_KEY'),//可在商户后台⽣成⽀付秘钥
);
$this->config = $config;
}
/**
* ⽣成订单号
* @author      lxhui<772932587@qq>
* @since 1.0
* @return array
*/
private static function trade_no() {
list($usec, $sec) = explode(" ", microtime());
$usec = substr(str_replace('0.', '', $usec), 0 ,4);
$str  = rand(10,99);
return date("YmdHis").$usec.$str;
}
/**
* order
* @author      lxhui<772932587@qq>
* @since 1.0
* @return array
*/
public function store(Request $request )
{
\DB::beginTransaction();
try{
if( !$this->checkMember(['openid'=>$request->openid]))
return response()->json(['code'=>200,'status'=>0,'message'=>'该openid未注册']);
$config = $this->config;
$result =json_decode($request->apiParams,true);
if( !$result['addressInfo'])
return response()->json(['code'=>200,'status'=>0,'message'=>'收货地址不能为空']);
if( !$result['orderInfo'])
return response()->json(['code'=>200,'status'=>0,'message'=>'订单商品不能为空']);
$address_data = [
'openid'=>$request->openid,
'realname'=>$result['addressInfo']['userName'],
'mobile'=>$result['addressInfo']['telNumber'],
'province'=>$result['addressInfo']['provinceName'],
'city'=>$result['addressInfo']['cityName'],
'area'=>$result['addressInfo']['countyName'],
'address'=>$result['addressInfo']['detailInfo'],
'zipcode'=>$result['addressInfo']['postalCode'],
];
$model = MemberAddress::firstOrCreate($address_data);
$productArray =$result['orderInfo'];
$products = [];
$orderProducts = [];
$productsFee = 0.0; //⽀付商品总价
foreach ($productArray as$val) {
$product = Product::find($val['goodsid']);
$productsFee += $product->marketprice * $val['total'];
}
// 计算价格
$shippingFee = 10.0;
$totalFee = $productsFee + $shippingFee;
// 创建订单
$ordersn = self::trade_no();
$order = new Order();
$order->ordersn = $ordersn;
$order->openid = $request->openid;
$order->price = $totalFee;
$order->goodsprice = $totalFee;
$order->createtime =time();
$order->addressid = $model->id;
$order->storeid = $request->storeid;
$order->save();
$orderid = $order->id;
$order_goods = new OrderGoods();
foreach ($productArray as$val) {
$product = Product::find($val['goodsid']);
$product=['orderid'=>$orderid,'goodsid'=>$val['goodsid'],'price'=>$product->marketprice,'total'=>$val['total'],'openid'=>$request->openid];
array_push($products, $product);
}
/* ⽣产预订单参数 */
$openid = $request->openid;
$body = '商城订单';
$order_sn = $ordersn;
$total_fee = $totalFee;
//统⼀下单参数构造
$unifiedorder = array(
'appid'            => $config['appid'],
'mch_id'        => $config['pay_mchid'],
'nonce_str'        => self::getNonceStr(),
'body'            => $body,
'out_trade_no'    => $order_sn,
'total_fee'        => $total_fee * 100,
'spbill_create_ip'    => $request->getClientIp(),
'notify_url'    => ''.$_SERVER['HTTP_HOST'].'/Api/Wxpay/notify',// ⽀付回调的url,可⾃定义,⾮必须
'trade_type'    => 'JSAPI',
'openid'        => $request->openid,//'oIXoL0ZpfG3NdSE8Qa-S1GcEHJGY'//测试openid
);
$unifiedorder['sign'] = self::makeSign($unifiedorder);
//请求数据
$xmldata = self::array2xml($unifiedorder);
$url = 'h.weixin.qq/pay/unifiedorder';
$res = self::curl_post_ssl($url, $xmldata);
if(!$res){
return response()->json(['code'=>200,'status'=>0,'message'=>'⽆法连接服务器']);
//self::return_err("Can't connect the server");
}
//file_put_contents是⽤来查看服务器返回的结果测试完可以删除了,可以使⽤laravel⾃带⽇志功能
//file_put_contents('/',$res,FILE_APPEND);
        \Log::info(json_encode($res));
$content = self::xml2array($res);
$result_code= isset($content['result_code']) ? $content['result_code'] : '';
$return_code = isset($content['return_code']) ? $content['return_code'] : '';
if(strval($result_code) == 'FAIL'){
return self::return_err(strval($content['err_code_des']));
}
if(strval($return_code) == 'FAIL'){
return self::return_err(strval($content['return_msg']));
}
$data = $this->pay($content['prepay_id']);
\DB::table("eshop_order_goods")->insert($products);
\DB::commit();
return response()->json(['code'=>200,'status'=>1,'message'=>'提交成功','data'=>$data]);
} catch (\Exception$e){
\DB::rollback();//事务回滚
return response()->json(['code'=>200,'status'=>0,'message'=>$e->getMessage()]);
}
}
/**
* 进⾏⽀付接⼝签名
* @param string $prepay_id 预⽀付ID(调⽤prepay()⽅法之后的返回数据中获取)
* @return  json的数据
*/
public function pay($prepay_id){
$config = $this->config;
$data = array(
'appId'        => $config['appid'],
'timeStamp'    => time(),
'nonceStr'    => self::getNonceStr(),
'package'    => 'prepay_id='.$prepay_id,
'signType'    => 'MD5'
);
$data['paySign'] = self::makeSign($data);
return$data;
}
/
/⽀付回调验证
public function notify(){
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
// 这句file_put_contents是⽤来查看服务器返回的XML数据测试完可以删除了
//file_put_contents(APP_ROOT.'/',$res,FILE_APPEND);
//将服务器返回的XML数据转化为数组
$data = self::xml2array($xml);
// 保存服务器返回的签名sign
$data_sign = $data['sign'];
// sign不参与签名算法
unset($data['sign']);
$sign = self::makeSign($data);
// 判断签名是否正确判断⽀付状态
if ( ($sign===$data_sign) && ($data['return_code']=='SUCCESS') && ($data['result_code']=='SUCCESS') ) {
$result = $data;
//获取服务器返回的数据
$order_sn = $data['out_trade_no'];            //订单单号
$openid = $data['openid'];                    //付款⼈openID
$total_fee = $data['total_fee'];            //付款⾦额
$transaction_id = $data['transaction_id'];    //⽀付流⽔号
//更新数据库
$this->updateDB($order_sn,$openid,$total_fee,$transaction_id);
}else{
$result = false;
}
// 返回状态给服务器
if ($result) {
$str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';        }else{
$str='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';        }
return$result;
}
//---------------------------------------------------------------⽤到的函数-------------------------------------------------------
-----
/**
* 错误返回提⽰
* @param string $errMsg 错误信息
* @param string $status 错误码
* @return  json的数据
*/
protected function return_err($errMsg='error',$status=0){
return response()->json(['code'=>200,'result'=>'fail','status'=>$status,'errmsg'=>$errMsg]);
}
/**
* 正确返回
* @param    array $data 要返回的数组
* @return  json的数据
*/
protected function return_data($data=array()){
return response()->json(['code'=>200,'result'=>'success','status'=>1,'data'=>$data]);
}
/**
* 将⼀个数组转换为 XML 结构的字符串
* @param array $arr 要转换的数组
* @param int $level 节点层级, 1 为 Root.
* @return string XML 结构的字符串
*/
protected function array2xml($arr, $level = 1) {
$s = $level == 1 ? "<xml>" : '';
foreach($arr as$tagname => $value) {
if (is_numeric($tagname)) {
$tagname = $value['TagName'];
unset($value['TagName']);
}
if(!is_array($value)) {
$s .= "<{$tagname}>".(!is_numeric($value) ? '<![CDATA[' : '').$value.(!is_numeric($value) ? ']]>' : '')."</{$tagname}>";            } else {
$s .= "<{$tagname}>" . $this->array2xml($value, $level + 1)."</{$tagname}>";
}
}
$s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
return$level == 1 ? $s."</xml>" : $s;
}
/**
* 将xml转为array
* @param  string    $xml xml字符串
* @return array    转换得到的数组
*/
protected function xml2array($xml){
//禁⽌引⽤外部xml实体
libxml_disable_entity_loader(true);
$result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return$result;
}
/**
*
* 产⽣随机字符串,不长于32位
* @param int $length
* @return 产⽣的随机字符串
*/
protected function getNonceStr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ )  {
$str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return$str;
}
/**
* ⽣成签名
* @return 签名
*/
protected function makeSign($data){
//获取⽀付秘钥
$key = $this->config['pay_apikey'];
// 去空
$data=array_filter($data);
//签名步骤⼀:按字典序排序参数
$string_a=http_build_query($data);
$string_a=urldecode($string_a);
//签名步骤⼆:在string后加⼊KEY
/
/$config=$this->config;
$string_sign_temp=$string_a."&key=".$key;
//签名步骤三:MD5加密
$sign = md5($string_sign_temp);
// 签名步骤四:所有字符转为⼤写
$result=strtoupper($sign);
return$result;
}
/**
* ⽀付发起请求
*/
protected function curl_post_ssl($url, $xmldata, $second=30,$aHeader=array()){
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//这⾥设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return$data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
}
返回结果:
{
"code": 200,
"status": 1,
"message": "提交成功",
"data": {
"appId": "wxaxxxxxxxxxxxxx",
"timeStamp": 1499497335,
"nonceStr": "qqejagkybrh9rxmyfosze71qs49ppcub",
"package": "prepay_id=wx2017070815022850d11adcb80558793405",
"signType": "MD5",
"paySign": "285BCE1F0B93701D927FA5F6CBE10E86"
}
}
代码没有完美封装,博友们如果有⼀套好的封装,请留下共享地址哈啊

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。