QrPayView.lua 5.17 KB
local QrPayView = dialog.uinode("ui/QrPayLayer.csb",import(".BaseView"))

QrPayView.tabIndex = 1;

--支付宝微信二维码支付界面
function QrPayView:ctor()
    QrPayView.super.ctor(self);
end

function QrPayView:show(params)
    local scene = cc.Director:getInstance():getRunningScene()
    local inst = QrPayView:create();
    scene:addChild(inst, dialog.ZORDER_APP);

    inst:setContentSize(cc.size(display.width,display.height));
    ccui.Helper:doLayout(inst);

    local panel = inst:getChildByName("panel");
    panel:setScale(0.5);
    local scaleTo = cc.ScaleTo:create(0.2,1);
    panel:runAction(scaleTo);

    inst.params = params;

    inst:init();

    inst.onPaySuccResFunHandler = handler(inst,inst.onPaySuccResFun);
    cmsg.on("gateway_msg.notify_pay_succ_msg_res",inst.onPaySuccResFunHandler);

    return inst;
end

function QrPayView:onPaySuccResFun(params)
    local product = params.data.product;

    cmsg.off("gateway_msg.notify_pay_succ_msg_res",self.onPaySuccResFunHandler);

    self:close();
end

function QrPayView:init()
    self:getChildByName("panel"):getChildByName("txt_name"):setString("产品名称:"..self.params.subject);
    self:getChildByName("panel"):getChildByName("txt_price"):setString("¥"..self.params.price);

    self.qr = self:getChildByName("panel"):getChildByName("qr");
    self.btn_alipay = self:getChildByName("panel"):getChildByName("btn_alipay");
    self.btn_alipay.fn = handler(self,self.onAliPay);
    self.btn_alipay:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, self.btn_alipay.fn)end);

    self.btn_wx = self:getChildByName("panel"):getChildByName("btn_wx");
    self.btn_wx.fn = handler(self,self.onWxPay);
    self.btn_wx:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, self.btn_wx.fn)end);

    local btn_close = self:getChildByName("panel"):getChildByName("btn_close");
    btn_close.fn = handler(self,self.onCancel);
    btn_close:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, btn_close.fn)end);

    self.nodes[1] = {self.btn_alipay,self.btn_wx};
    self.nodes[2] = {btn_close};
    self.nodeIndex = 1;
    self:updateSelectedState(false);

    self.tabIndex = 1;
    self:updateTab();
end

function QrPayView:onAliPay()
    self.tabIndex = 1;
    self:updateTab();
end

function QrPayView:onWxPay()
    self.tabIndex = 2;
    self:updateTab();
end

function QrPayView:updateTab()
    for i,v in ipairs(self.nodes[1]) do
        if i == self.tabIndex then
            v:getChildByName("bg"):setVisible(true);
            self:getChildByName("panel"):getChildByName("qr_"..i):setVisible(true);
        else
            v:getChildByName("bg"):setVisible(false);
            self:getChildByName("panel"):getChildByName("qr_"..i):setVisible(false);
        end
    end

    local qr_view = self:getChildByName("panel"):getChildByName("qr_"..self.tabIndex);
    local function callBack(params)
        if params.result == 0 then
            local qr_url = params.url;
            local NetImage = require("src.app.views.view.NetImage");
            local img = NetImage:create(qr_url,250,250);

            qr_view:removeAllChildren();
            qr_view:addChild(img);
        else
            Alert.showOne(params.msg);
        end
    end

    if not qr_view.isShow then
        local trade_no = self.params.trade_no;
        local subject = self.params.subject;
        local total_amount = tonumber(self.params.price);
--        total_amount = 0.01;
        if self.tabIndex == 1 then
            local zfb_url = self.params.zfb_url;
            local paramsStr = "out_trade_no="..trade_no.."&appid=1".."&subject="..subject.."&total_amount="..total_amount.."&notify_url="..zfb_url;
            MyHttpRequest:getInstance():request("POST",PAY_URL.."/alipay/getqr.php","",paramsStr,callBack);
        else
            local wx_url = self.params.wx_url;
            local paramsStr = "out_trade_no="..trade_no.."&appid=1".."&subject="..subject.."&total_amount="..total_amount.."&notify_url="..wx_url;
            MyHttpRequest:getInstance():request("POST",PAY_URL.."/wxpay/getqr.php","",paramsStr,callBack);
        end
        qr_view.isShow = true;
    end
end

function QrPayView:onCancel()
    cmsg.off("gateway_msg.notify_pay_succ_msg_res",self.onPaySuccResFunHandler);
    self:close();
end

--返回键处理函数
function QrPayView:onKeypadBack()
    cmsg.off("gateway_msg.notify_pay_succ_msg_res",self.onPaySuccResFunHandler);
    self:close();
end

--左键处理函数
function QrPayView:onKeypadLeft(isBounce)
    if self.selectedIndex > 1 then
        QrPayView.super.onKeypadLeft(self,false);
    end
    local curNode = self.nodes[self.nodeIndex][self.selectedIndex];
    if curNode:getName() == "btn_alipay" then
        self:onAliPay();
    elseif curNode:getName() == "btn_wx" then
        self:onWxPay();
    end
end

--左键处理函数
function QrPayView:onKeypadRight(isBounce)
    if self.selectedIndex < #self.nodes then
        QrPayView.super.onKeypadRight(self,false);
    end
    local curNode = self.nodes[self.nodeIndex][self.selectedIndex];
    if curNode:getName() == "btn_alipay" then
        self:onAliPay();
    elseif curNode:getName() == "btn_wx" then
        self:onWxPay();
    end
end

return QrPayView