php Laravel 微信支付V3 支付回调验证

话不多说直接上代码(如果有用的话-记得打赏哦)

/**
 * 从证书中获取私钥
 * @param $cert_path (微信平台证书---从《获取平台证书》接口中获得)
 * @return bool
 */
function certPubKey($cert_path){
    $cert_path = file_get_contents($cert_path);
    $pub_key  = openssl_pkey_get_public($cert_path);
    if($pub_key){
        $keyData = openssl_pkey_get_details($pub_key);
        return $keyData['key'];
    }
    return false;
}

/**
 * 验证回调主方法
 * @param $http_data header头的信息(微信回调发送的header头)
 * @param $body 应答主体信息 (微信发送的数据)(未解密的数据!!!)
 * @return int 验证成功返回 1  失败返回 0
 */

function verifySigns($http_data,$body){
        $wechatpay_timestamp    = $http_data['Wechatpay-Timestamp'];
        $wechatpay_nonce        = $http_data['Wechatpay-Nonce'];
        $wechatpay_signature    = $http_data['Wechatpay-Signature'];
        $signature = base64_decode($wechatpay_signature);
        $pub_key = certPubKey('wxpay_key/cert.pem');//平台证书路径
        $body = str_replace('\\','',$body);
        $message       =
            $wechatpay_timestamp . "\n" .
            $wechatpay_nonce . "\n" .
            $body . "\n";
        $res = openssl_verify($message,$signature, $pub_key,OPENSSL_ALGO_SHA256);

        if ($res == 1) {
            return 1;
        }
        return 0;
}
微信支付v3版的支付回调是基于异步通知机制进行的,需要在接收支付结果的服务器端编写Java代码来处理回调请求。以下是一个简单的示例代码: ```java import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/pay") public class WechatPayCallbackController { private final WxPayService wxPayService; public WechatPayCallbackController() { // 初始化微信支付服务 wxPayService = new WxPayServiceImpl(); wxPayService.setKey("your-wxpay-api-key"); // 设置支付密钥 } @PostMapping("/callback") public String payCallback(@RequestBody String notifyData) { try { // 解析支付通知 wxPayService.parseNotifyResult(notifyData); // 处理支付成功逻辑,例如更新订单状态等 // 返回成功响应给微信支付平台 return WxPayNotifyResponse.success("处理成功"); } catch (WxPayException e) { // 处理支付失败逻辑,例如记录错误日志等 // 返回失败响应给微信支付平台 return WxPayNotifyResponse.fail(e.getMessage()); } } } ``` 上述代码是一个使用Spring Boot框架的控制器类,负责处理微信支付回调请求。在回调方法`payCallback`中,通过`wxPayService.parseNotifyResult`方法解析支付通知的XML数据,并在处理成功或失败后返回相应的结果。 需要注意的是,为了保证安全性,建议在实际开发中将`your-wxpay-api-key`替换为实际的支付密钥。 另外,此代码仅作为示例,实际情况可能需要结合具体的业务逻辑进行扩展和修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值