zoukankan      html  css  js  c++  java
  • 微信APP支付,支付宝APP支付demo

    最近公司新开发的APP中,需要集成微信支付和支付宝支付,2个平台申请的都是APP支付。这是个人第一次单独的,完整的做完2个平台的支付。

    这里我主要用到了2个接口:支付接口,订单查询接口,虽然2个平台的文档都挺齐全,但开发下来,还是踩了不少坑,记录下来,或许对后来人有用。

    先来说支付宝SDK:

      了解3个概念:应用公钥,应用私钥和支付宝公钥

      开发人员用支付宝提供的秘钥生成工具生成的秘钥,即为应用公钥和应用私钥。

      开发人员在给支付宝APP配置应用公钥后,支付宝主动生成了一个公钥,即为支付宝公钥。

      配置如下图示:

           

      需要把这个支付宝公钥保存下来,具体使用场景,请看后续说明。

      这里个人用到了2个接口,生成支付订单信息和订单查询。

      支付宝提供了服务端SDK的下载:https://docs.open.alipay.com/54/106370/

      其目录显示:

      

      下面直接给出支付、回调、订单查询的代码:

    // 生成支付信息
    function pay() {
    $aop = new AopClient;
    $aop->gatewayUrl = '网关地址';
    $aop->appId = 'alipay_appid';
    $aop->rsaPrivateKey = '应用私钥';
    $aop->signType = "RSA2";
    $aop->alipayrsaPublicKey = '支付宝公钥';

    $request = new AlipayTradeAppPayRequest();

    $bizcontent = "{"body":"账户充值","
    . ""subject": "用户账户充值","
    . ""out_trade_no": "订单号","
    . ""timeout_express": "30m","
    . ""total_amount": "订单金额","
    . ""product_code":"QUICK_MSECURITY_PAY""
    . "}";
    $request->setNotifyUrl('开发者回调地址');
    $request->setBizContent($bizcontent);
    $response = $aop->sdkExecute($request);

    // 注意: 官方demo里,这里加了htmlspecialchars方法,其实是错误的,应该去掉,否则会报ALI38173错误
    echo $response;
    }

    // 回调
    function callback() {
    $aop = new AopClient ();
    $aop->alipayrsaPublicKey = '支付宝公钥';

    // 支付宝回调是以POST方式提交过来的
    $result = $aop->rsaCheckV1($_POST, null, $_POST['sign_type']);

    if ($result) {
    // TODO 验证订单号,支付金额,支付宝状态等信息
    echo 'success';
    } else {
    echo 'error';
    }
    }

    // 订单查询
    function queryOrder() {
    $aop = new AopClient ();
    $aop->signType = "RSA2";
    $aop->gatewayUrl = '网关地址';
    $aop->appId = 'appid';
    $aop->rsaPrivateKey = '应用私钥';

    // 注意:这里是支付宝公钥,不是应用公钥
    $aop->alipayrsaPublicKey = '支付宝公钥';

    $request = new AlipayTradeQueryRequest ();
    $request->setBizContent("{" .
    ""out_trade_no":"订单号"," .
    ""trade_no":"交易单号"" .
    "}");
    $result = $aop->execute($request);

    $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
    $resultCode = $result->$responseNode->code;

    if(! empty($resultCode) && $resultCode == 10000) {
    if ($result->$responseNode->trade_status == 'TRADE_SUCCESS') {
    // 支付成功, 更新订单状态,用户账户余额,账户变化日志
    }
    }
    }

    微信支付:

      在开发平台申请app支付:https://open.weixin.qq.com/

      审核通过后,需要配置开发信息

      

      服务端PHP的demo:

    // 生成支付信息
    function pay() {
        $input = new WxPayUnifiedOrder();
        $input->SetBody('支付标题');
        $input->SetAttach('附加信息'); //  附加信息
        $input->SetOut_trade_no('订单号');
        $input->SetTotal_fee('订单金额,单位分'); //微信支付,单位(分)
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag('附属信息');
        $input->SetNotify_url('回调地址');
        $input->SetTrade_type("APP");
    
        // 生成预支付订单ID:prepay_id
        $unifiedOrder = WxPayApi::unifiedOrder($input);
    
        // 生成签名,返回给APP前端调用
        $result = makeWxPaySign($unifiedOrder);
    }
    
    // 生成签名
    function makeWxPaySign($UnifiedOrderResult)
    {
        if(! array_key_exists("appid", $UnifiedOrderResult)
            || ! array_key_exists("prepay_id", $UnifiedOrderResult)
            || empty($UnifiedOrderResult['prepay_id']))
        {
            throw new WxPayException("参数错误");
        }
    
        $api = new WxPayAppPay();
        $api->SetValue('appid', $UnifiedOrderResult["appid"]);
        $api->SetValue('package', "Sign=WXPay");
        $api->SetValue('prepayid', $UnifiedOrderResult['prepay_id']); // 预支付订单号
        $api->SetValue('partnerid', WxPayConfig::MCHID); // 商户号
        $api->SetValue('noncestr', WxPayApi::getNonceStr()); // 随机数
        $api->SetValue('timestamp', (string)time());
    
        // 对上面参数进行签名
        $api->SetValue('sign', $api->MakeSign());
    
        return $api->GetValues();
    }
    
    // 回调,继承微信SDK的回调类
    class PayNotifyCallBack extends WxPayNotify
    {
        //重写回调处理函数
        public function NotifyProcess($data, &$msg)
        {
            if (! array_key_exists("transaction_id", $data)) {
                return false;
            }
    
            // 调用订单查询方法,判断订单真实性
    
            return true;
        }
    }
    
    
    // 订单查询
    function queryOrder() {
        $input = new WxPayOrderQuery();
    
        $input->SetTransaction_id('支付平台交易号');
        $input->SetOut_trade_no('公司平台单号');
    
        // 订单查询结果
        $result = WxPayApi::orderQuery($input);
    
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && array_key_exists('trade_state', $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS"
            && $result["trade_state"] == "SUCCESS")
        {
            // 交易成功,todo 更新订单状态,用户账户余额,账户变化日志
        }
    }
  • 相关阅读:
    【Codeforces 349B】Color the Fence
    【Codeforces 459D】Pashmak and Parmida's problem
    【Codeforces 467C】George and Job
    【Codeforces 161D】Distance in Tree
    【Codeforces 522A】Reposts
    【Codeforces 225C】Barcode
    【Codeforces 446A】DZY Loves Sequences
    【Codeforces 429B】Working out
    【Codeforces 478C】Table Decorations
    【Codeforces 478C】Table Decorations
  • 原文地址:https://www.cnblogs.com/cqingt/p/8311723.html
Copyright © 2011-2022 走看看