天天看點

iOS內建支付寶

使用支付寶進行一個完整的支付功能,大緻有以下步驟:

1>先與支付寶簽約,獲得商戶id(partner)和賬号id(seller)(這個主要是公司的負責)

2>下載下傳相應的公鑰私鑰檔案(加密簽名用)

3>下載下傳支付寶sdk(登入網站:http://club.alipay.com/)裡面提供了非常詳細的文檔、如何簽約、如何獲得公鑰私鑰、如何調用支付接口

4>生成訂單資訊

5>調用支付寶用戶端,由支付寶用戶端跟支付寶安全伺服器打交道

6>支付完畢後傳回支付結果給商戶用戶端和伺服器。

sdk裡有內建支付寶功能的一個demo>內建支付功能的具體操作方式,可以參考demo

如何建立訂單 ( 訂單根據自己公司看是什麼樣的)

iOS內建支付寶
iOS內建支付寶

要想內建支付功能,依賴以下檔案夾的庫檔案(把這3個添加到你的用戶端中)

iOS內建支付寶

調用支付接口可以參考alixpaydemoviewcontroller的下面方法

-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath

如何簽名

如何調用支付接口

都在這個方法裡面了

//  

//選中商品調用支付寶快捷支付  

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath  

{  

    /* 

     *點選擷取prodcut執行個體并初始化訂單資訊 

     */  

    product *product = [_products objectatindex:indexpath.row];  

     *商戶的唯一的parnter和seller。 

     *本demo将parnter和seller資訊存于(alixpaydemo-info.plist)中,外部商戶可以考慮存于服務端或本地其他地方。 

     *簽約後,支付寶會為每個商戶配置設定一個唯一的 parnter 和 seller。 

    //如果partner和seller資料存于其他位置,請改寫下面兩行代碼  

    nsstring *partner = [[nsbundle mainbundle] objectforinfodictionarykey:@"partner"];  

    nsstring *seller = [[nsbundle mainbundle] objectforinfodictionarykey:@"seller"];  

    //partner和seller擷取失敗,提示  

    if ([partner length] == 0 || [seller length] == 0)  

    {  

        uialertview *alert = [[uialertview alloc] initwithtitle:@"提示"  

                                                        message:@"缺少partner或者seller。"  

                                                       delegate:self  

                                              cancelbuttontitle:@"确定"  

                                              otherbuttontitles:nil];  

        [alert show];  

        return;  

    }  

     *生成訂單資訊及簽名 

     *由于demo的局限性,本demo中的公私鑰存放在alixpaydemo-info.plist中,外部商戶可以存放在服務端或本地其他地方。 

    //将商品資訊賦予alixpayorder的成員變量  

    alixpayorder *order = [[alixpayorder alloc] init];  

    order.partner = partner;  

    order.seller = seller;  

    order.tradeno = [self generatetradeno]; //訂單id(由商家自行制定)  

    order.productname = product.subject; //商品标題  

    order.productdescription = product.body; //商品描述  

    order.amount = [nsstring stringwithformat:@"%.2f",product.price]; //商品價格  

    order.notifyurl =  @"http://www.xxx.com"; //回調url  

    //應用注冊scheme,在alixpaydemo-info.plist定義url types,用于快捷支付成功後重新喚起商戶應用  

    nsstring *appscheme = @"alixpaydemo";  

    //将商品資訊拼接成字元串  

    nsstring *orderspec = [order description];  

    nslog(@"orderspec = %@",orderspec);  

    //擷取私鑰并将商戶資訊簽名,外部商戶可以根據情況存放私鑰和簽名,隻需要遵循rsa簽名規範,并将簽名字元串base64編碼和urlencode  

    id<datasigner> signer = creatersadatasigner([[nsbundle mainbundle] objectforinfodictionarykey:@"rsa private key"]);  

    nsstring *signedstring = [signer signstring:orderspec];  

    //将簽名成功字元串格式化為訂單字元串,請嚴格按照該格式  

    nsstring *orderstring = nil;  

    if (signedstring != nil) {  

        orderstring = [nsstring stringwithformat:@"%@&sign=\"%@\"&sign_type=\"%@\"",  

                       orderspec, signedstring, @"rsa"];  

        //擷取快捷支付單例并調用快捷支付接口  

        alixpay * alixpay = [alixpay shared];  

        int ret = [alixpay pay:orderstring applicationscheme:appscheme];  

        if (ret == ksperroralipayclientnotinstalled) {  

            uialertview * alertview = [[uialertview alloc] initwithtitle:@"提示"  

                                                                 message:@"您還沒有安裝支付寶快捷支付,請先安裝。"  

                                                                delegate:self  

                                                       cancelbuttontitle:@"确定"  

                                                       otherbuttontitles:nil];  

            [alertview settag:123];  

            [alertview show];  

        }  

        else if (ret == ksperrorsignerror) {  

            nslog(@"簽名錯誤!");  

    [tableview deselectrowatindexpath:indexpath animated:yes];  

}  

//主要內建的關鍵就是下面幾步:  

//1.封裝訂單模型  

alixpayorder *order = [[alixpayorder alloc] init];  

// 生成訂單描述  

nsstring *orderspec = [order description];  

//2.簽名  

id<datasigner> signer = creatersadatasigner(@“私鑰key”);  

// 傳入訂單描述 進行 簽名  

nsstring *signedstring = [signer signstring:orderspec];  

//3.生成訂單字元串  

nsstring *orderstring = [nsstring stringwithformat:@'%@&sign='%@'&sign_type='%@'',  

                         orderspec, signedstring, @'rsa'];  

//4.調用支付接口  

alixpay * alixpay = [alixpay shared];  

// appscheme:商戶自己的協定頭  

int ret = [alixpay pay:orderstring applicationscheme:appscheme];  

繼續閱讀