微信小程序APIv3支付回调解密

微信小程序APIv3支付回调解密namespaceapp\api\controller;usethink\Db;classPaysuccessbackextendsCommon{publicfunctionnotify(){$receipt=file_get_contents(“php://input”);if($receipt==null){$receipt=$GLOBALS[‘HTTP_RAW_POST_DATA’];.

namespace app\api\controller;
use think\Db;
class Paysuccessback extends Common{
    public function notify(){
        $receipt = file_get_contents("php://input");
        if ($receipt == null) {
            $receipt = $GLOBALS['HTTP_RAW_POST_DATA'];
        }
        $post_data = json_decode($receipt,true);
        if ($post_data['summary'] == '支付成功') {
            $nonceStr = $post_data['resource']['nonce'];
            $associatedData = $post_data['resource']['associated_data'];
            $ciphertext = $post_data['resource']['ciphertext'];
            $ciphertext = base64_decode($ciphertext);
            if (strlen($ciphertext) <= 12) {
                return;
            }
            $result = $this->wechartDecrypt($ciphertext,$associatedData,$nonceStr);
            $dd = [
                'out_trade_no'=>$result['out_trade_no'],
                'openid'=>$result['payer']['openid'],
                'transaction_id'=>$result['transaction_id'],
                'trade_type'=>$result['trade_type'],
                'trade_state'=>$result['trade_state'],
                'appid'=>$result['appid'],
                'mchid'=>$result['mchid'],
                'payer_total'=>$result['amount']['payer_total'],
                'add_time'=>time()
            ];
            Db::startTrans();
            $pl = Db::table('pay_log')->insert($dd);
            if($result['trade_state'] != 'SUCCESS' && $pl){
                Db::commit();
                return json_encode(['code' => 'SUCCESS', 'message' => '成功']);
            }
            if($result['trade_state'] == 'SUCCESS'){
                //订单号
                $res = db('order')->field('pay_status')->where('order_no',$result['out_trade_no'])->find();
                if($res['pay_status'] == 1){
                    return json_encode(['code' => 'SUCCESS', 'message' => '成功']);
                }
                $user = 自己的业务逻辑
                    try {
                        自己的业务逻辑操作
                        // 提交事务
                        if($pl && $us !== false && $ord !== false && $tilog){
                            Db::commit();
                            return json_encode(['code' => 'SUCCESS', 'message' => '成功']);
                        }
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                    }
                }
            }
        }
    }

     //微信回调解密
    public function wechartDecrypt($text,$associated_data,$nonce) {
        $key = 'XXXXXXXXXXXXXXX';//商户平台设置的api v3 密码
        $str = sodium_crypto_aead_aes256gcm_decrypt($text,$associated_data,$nonce,$key);
        return json_decode($str,true);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/39295.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注