微信小程序 requestPayment:fail check tichet rejected 报错,附php代码
新注册了一个小程序,申请完支付,变成新版样子
需要使用两个接口:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序APPID&secret=小程序密钥
附上生成小程序支付的php代码:
/**
* 获得jsSdk支付参数
* @param $openid
* @param $out_trade_no
* @param $total_fee
* @param $attach
* @param $body
* @param string $detail
* @param string $trade_type
* @param array $options
* @return array|string
*/
public static function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
{
$access_token = '你的access_token';
$postdata=array();
$postdata["openid"]=$openid;
$postdata["combine_trade_no"]=$out_trade_no;
// $postdata["expire_time"]=$out_trade_no;
$postdata["sub_orders"][0] = array();
$postdata["sub_orders"][0]['mchid']='你的mchid';
$postdata["sub_orders"][0]['amount']=$total_fee * 100;
$postdata["sub_orders"][0]['trade_no']=$out_trade_no;
$postdata["sub_orders"][0]['description']=substr($body,0,50);
$jsonStr = json_encode($postdata);
//echo $jsonStr;die;
$ret = self::http_post('https://api.weixin.qq.com/shop/pay/createorder?access_token='. access_token, $jsonStr, 20);
if($ret['data']['errcode'] > 0){
$ret = self::http_post('https://api.weixin.qq.com/shop/pay/createorder?access_token='. access_token, $jsonStr, 20);
}
$retdata = array();
$retdata["status"] = 200;
$retdata["msg"] = "ok";
$retdata["data"]["status"] = "WECHAT_PAY";
$retdata["data"]["result"]["jsConfig"] = array();
$retdata["data"]["result"]["order_id"] = $out_trade_no;
$retdata["data"]["result"]["jsConfig"] = $ret['data']['payment_params'];
$ret['data']['payment_params']["appId"] = $postdata["openid"];
return $ret['data']['payment_params'];
// $t = self::paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
// return self::paymentService()->configForJSSDKPayment($t);
}
小程序前端代码:
wx.requestOrderPayment({
// orderInfo: {},
timeStamp: data.timeStamp + '',
nonceStr: data.nonceStr,
package: data.package,
signType: data.signType,
paySign: data.sign || data.paySign,
success (res) {
lln.tips({title:data.successTip||'支付成功',duration:duration},function(){callback && callback(true);});
},
fail (res) {
let title = '支付失败';
if (res.errMsg === "requestOrderPayment:fail cancel") {
title = '用户取消支付';
}
console.log(res)
lln.tips({title:title,duration:duration},function(){callback && callback(false);});
}
})
支付成功回调处理方法:
在微信小程序:开发管理>开发设置>消息推送 开启消息推送功能
附上服务端代码:
///消息推送
public function message(Request $request){
Log::error('开始 --------- signature:'.$_GET["signature"]);
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$echostr = $_GET["echostr"]?? '';
$nonce = $_GET["nonce"];
$token = "你的token";
$key = "你的key";
$appId = "你的appid";
if(!$signature || !$timestamp || !$nonce){
Log::error('参数不完整');
return app('json')->fail('error');
}
//$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
//og::error('$tmpStr'.$tmpStr.' $signature:'.$signature);return;
if ($tmpStr == $signature ) {
//首次服务器验证
if($echostr){
Log::error("服务器验证成功");
return $echostr; return;
}
$encryptMsg = file_get_contents('php://input');
libxml_disable_entity_loader(true);
$xml_obj = (array)simplexml_load_string($encryptMsg, 'SimpleXMLElement', LIBXML_NOCDATA);
Log::error($xml_obj);
//支付回调
if($xml_obj['Event'] == "funds_order_pay"){
//Log::error($xml_obj['order_info']);
foreach ($xml_obj['order_info'] as $key => $value) {
if($key == "trade_no"){ //订单
try {
// 订单处理
$services = app()->make(StoreOrderSuccessServices::class);
$orderInfo = $services->getOne(['order_id' => $value]);
if ($orderInfo && !$orderInfo->paid){
$services->paySuccess($orderInfo->toArray(), 'weixin', ['trade_no' => $value]);
}
} catch (\Exception $e) {
echo 'fail'; return;
}
try {
//用户充值
$userRecharge = app()->make(UserRechargeServices::class);
if ($userRecharge->be(['order_id' => $value, 'paid' => 1])) {echo 'fail'; return; }
$userRecharge->rechargeSuccess($value);
} catch (\Exception $e) {
echo 'fail'; return;
}
}
// Log::error($key.' '.$value);
}
}
echo 'success'; return;
} else {
echo ''; return;
}
return app('json')->success($_GET["signature"]);
}