iOS-集成支付寶支付、微信支付簡單總結

支付寶快捷支付:php

官方文檔中,支付寶說建議咱們使用支付時要講簽名過程放在服務器端,這樣安全。同時給的demo中籤名是在本地移動端作的。。。不過支付寶的集成仍是較簡單的。api

爲了安全簽名固然放後臺作了。我實現支付的過程當中主要遇到是參數問題,最後提,先大概梳理一下流程。安全

1.固然第一步是註冊帳號,建立應用(應用類型根據狀況來選擇),申請開通支付功能(多久忘了,印象中是挺快的)。服務器

2.導入SDK添加庫什麼的就按照官方文檔來就行了,不贅述了微信

支付寶官方文檔
網絡

3.添加完成後command+b試下是否編譯OK,報錯就根據提示去改就行了(注意庫文件重複添加問題,通常不會出現問題)app

4.導入頭文件 #import <AlipaySDK/AlipaySDK.h>,開始調用支付API。(配置不要放本地了,交給後臺。注意:關於簽名的問題要提早和後臺溝通好怎麼作,咱們是建了個post請求)post

case 1:// 支付寶
                    {
                        //應用註冊scheme,在AlixPayDemo-Info.plist定義URL types
                        NSString *appScheme = @"Alixxxxx";//這個能夠任意自定義,不過最好是和本身的應用名字相關的
                        [[AlipaySDK defaultService] payOrder:result[@"info"] fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                            // 參數result[@"info"]是請求到的驗證後的簽名(字符串)

                            NSLog(@"resultDic === %@", resultDic);
                            
                            NSInteger heCha;
                            NSString *heChaLable = [NSString stringWithFormat:@"%@",resultDic[@"resultStatus"]];
                            heCha = [heChaLable integerValue];
                            if (heCha==9000 ) {
                            //這裏作支付成功後你想作的事情
                                [AlertUtil showAlertInfoSingle:@"支付成功"];
                            }
                            if (heCha==8000) {
                                UIAlertView *shibai=[[UIAlertView alloc]initWithTitle:@"提示" message:@"訂單正在處理中" delegate:self cancelButtonTitle:nil otherButtonTitles:@"請從新支付", nil];
                                [shibai show];
                            }
                            if (heCha==4000) {
                                UIAlertView *shibai=[[UIAlertView alloc]initWithTitle:@"提示" message:@"訂單支付失敗" delegate:self cancelButtonTitle:nil otherButtonTitles:@"請從新支付", nil];
                                shibai.delegate=self;
                                [shibai show];
                            }
                            if (heCha==6001) {
                                [AlertUtil showAlertInfoSingle:@"支付失敗"];
                            }
                            if (heCha==6002) {
                                UIAlertView *shibai=[[UIAlertView alloc]initWithTitle:@"提示" message:@"網絡鏈接出錯" delegate:self cancelButtonTitle:nil otherButtonTitles:@"請從新支付", nil];
                                shibai.delegate=self;
                                [shibai show];
                            }
                        }];
                    }
                        break;    

注意://應用註冊scheme,操做過程見下圖測試

圖中 URL Scheme 處填寫上面代碼中定義的 Alixxxxx微信支付

5.OK,這樣支付寶集成算完成了,可是!後臺返回的字符串可不必定能用啊。。。

6.因此提下我出現的問題(後臺要解決的):

  a.參數問題,必定要符合支付寶的要求,後臺給你的簽名字符串中要與下面參數的名字一毛同樣(咱們以前有個參數名字不一樣,調了N久才發現,哭),下面貼個簽名字符串的實例(參數標綠色了):

"partner=\"2088101568353491\"&seller_id=\"2088101568353491\"&out_trade_no=\"YR2VGG3G1I31XDZ\"&subject=\"1\"&body=\"我是測試數據\"&total_fee=\"0.02\"&notify_url=\"http://www.xxx.com\"&service=\"mobile.securitypay.pay\"&payment_type=\"1\"&_input_charset=\"utf-8\"&it_b_pay=\"30m\"&sign=\"GsSZgPloF1vn52XAItRAldwQAbzIgkDyByCxMfTZG%2FMapRoyrNIJo4U1LUGjHp6gdBZ7U8jA1kljLPqkeGv8MZigd3kH25V0UK3Jc3C94Ngxm5S%2Fz5QsNr6wnqNY9sx%2Bw6DqNdEQnnks7PKvvU0zgsynip50lAhJmflmfHvp%2Bgk%3D\"&sign_type=\"RSA\""

主要參數解釋(具體的不見得同樣,這個看你簽名時藥傳的有什麼了):https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.fdQytM&treeId=59&articleId=103678&docType=1

notify_url:支付的回調地址

body:商品的描述

total_fee:商品的價格

sign:簽名串

sign_type:驗籤方式

 

 

微信支付:

不少小夥伴都說微信支付真的是太坑了,官方文檔都帶有錯的。。。。我也被坑了N次,鑑於這個坑太多,作下簡單步驟和備份記錄,儘可能避免再次進坑裏。

1.註冊什麼的略過,關於支付的申請,相關資料向公司相關人員要到就OK;

2.配置:參照文檔來  微信官方文檔

3.支付請求主要代碼

case 3:// 微信支付
                    {
                        NSLog(@"微信支付");
                        NSDictionary *data = result[@"data"];// result[@"data"]是請求後臺獲取到所需數據  須要注意:這裏的數據均是二次簽名後的數據,不要用第一次簽名的數據
                        PayReq *request = [[PayReq alloc] init];
                        request.partnerId = data[@"partnerid"];//@"10000100";// 商戶號
                        request.prepayId= data[@"prepayid"];//@"1101000000140415649af9fc314aa427";// 預支付交易會話id
                        request.package = @"Sign=WXPay";// 固定值
                        request.nonceStr= data[@"noncestr"];//@"a462b76e7436e98e0ed6e13c64b4fd1c";// 隨機字符串
                        request.timeStamp= [data[@"timestamp"] intValue];//@"1397527777";// 時間戳
                        request.sign= data[@"sign"];// 簽名字符串
                        [WXApi sendReq:request];// 發起請求
                    }
                        break;

4.AppDelegate.m 中設置回調

// 微信 WXApiDelegate協議的方法
- (void)onResp:(BaseResp *)resp {
    // 若是第三方程序向微信發送了sendReq的請求,那麼onResp會被回調。sendReq請求調用後,會切到微信終端程序界面。
    NSLog(@"%@",resp);
    if ([resp isKindOfClass:[PayResp class]]) {
        
        PayResp *response = (PayResp *)resp;
        NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];

        switch (response.errCode) {
            case WXSuccess:
            {// 支付成功
                NSString *strTitle = @"支付結果";                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
                break;
                
            default:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付結果" message:@"支付失敗" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                break;
            } 
        }
    }
}

5.注意問題:

  a.若是程序中同時集成了友盟,那麼在 register 時,要先友盟後微信

  b.支付時只有一個肯定按鈕,確認返回後 ret = -2 通常就是請求時參數有問題,簽名問題的概率較大。

  c.請求所用數據必定是二次簽名的數據!

相關文章
相關標籤/搜索