String accessToken = "86_K5ysiq0CNLb662dxUYjHt-UduesOD99RUqcisuMsSePoQiCjwYUcdSqyeZxyyDVfsX25khhmEoIoEccy2odwsml3PB_uIaue9wNYaimNIjWTkFdv9WeCTEfclmS2nkBgCkGY5H0kUJ1zLyAfBJEcAEDKSN";
HashMap map = new HashMap<>();
map.put("mchid", "1697798151");
map.put("out_trade_no", "1217752501201407033233368018");
map.put("env", 0);
String paySign = WechatMiniProgramSignUtil.sign("/retail/B2b/getorder", map, APP_PROD_KEY);
String jsonString = FastJsonUtils.toJsonString(map);
System.out.println("paySign:" + paySign);
System.out.println("body:" + jsonString);
String url = "https://api.weixin.qq.com/retail/B2b/getorder?access_token=" + accessToken + "&pay_sig=" + paySign;
System.out.println("url:" + url);
HttpRequestHeaders headers = (new HttpRequestHeaders()).contentType("application/json");
HttpResponse post = HttpUtils.post(url, headers, jsonString);
String content = post.getContent();
System.out.println(content);
调用小程序b2b门店助手里面的b2b支付相关接口。渠道返回的错误:
调用的是查询订单接口:/retail/B2b/getorder
请求响应报文:
paySign:4900a058bcfe734166a7dd1a866ce266681c99054965dcc1ee5138e2fe3efc40
body:{"mchid":"1697798151","out_trade_no":"1217752501201407033233368018","env":0}
响应报文:
{"errcode":9403200,"errmsg":"参数为空或非法 detail:[支付签名[pay_sig]校验失败] rid: 672ae6ea-6971f120-76724ca5"}
签名方法(java语言):
package com.ursa.middle.channel.wechat.mini.program.b2b.store.util;
import com.ursa.base.serialize.json.FastJsonUtils;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Map;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
* @author xxx
* @version 1.0.0
* @Description
* @date 2024/10/22
*/
public class WechatMiniProgramSignUtil {
private final static String SHA256 = "HmacSHA256";
public static String sign(String uri, Map signBody, String appKey) throws SignatureException {
String bodyStr = FastJsonUtils.toJsonString(signBody).replace("\":\"", "\": \"");
String signMsg = uri + "&" + bodyStr;
return hmacSHA256(signMsg, appKey);
}
public static String sign(Map signBody, String sessionKey) throws SignatureException {
String bodyStr = FastJsonUtils.toJsonString(signBody).replace("\":\"", "\": \"");
return hmacSHA256(bodyStr, sessionKey);
}
private static String hmacSHA256(String data, String key) throws SignatureException {
try {
Mac sha256_HMAC = Mac.getInstance(SHA256);
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), SHA256);
sha256_HMAC.init(keySpec);
byte[] bytes = sha256_HMAC.doFinal(data.getBytes());
return byteArrayToHexString(bytes);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new SignatureException("加签异常:" + e.getMessage());
}
}
private static String byteArrayToHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (byte value : b) {
sb.append(String.format("%02x", value));
}
return sb.toString();
}
}
退款同问,也是这个报错 $postdata = []; $postdata['mchid'] = $paylog['mch_id']; $postdata['out_trade_no'] = $ordernum; $postdata['order_id'] = $paylog['transaction_id']; $postdata['out_refund_no'] = date('YmdHis').rand(1000,9999); $postdata['refund_amount'] = intval(bcmul($refundmoney, 100, 0)); $postdata['refund_from'] = 1;//退款来源,枚举值 1:人工客服退款 2:用户自己退款 3:其他 // $postdata['refund_reason'] = 0;//退款原因,枚举值 0:暂无描述 1:产品问题 2:售后问题 3:意愿问题 4:价格问题 5:其他原因 $appkey = $appinfo['wxpay_b2b_appkey']; $pay_sig = self::b2b_pay_sig('/retail/B2b/refund',jsonEncode($postdata), $appkey); $rs = curl_post($url.'&pay_sig='.$pay_sig, jsonEncode($postdata)); private function b2b_pay_sig($uri, $post_body, $appkey) { $need_sign_msg = $uri . '&' . $post_body; return hash_hmac('sha256', $need_sign_msg, $appkey); }