一、在微信開發者平臺獲取開發者認證:(一年300元人民幣)
PS:具體流程按照微信流程指示操做便可,在這就不廢話了。php
二、下載微信最新的SDK,引入到工程裏邊api
三、導入相關係統庫及文件,不導入會報錯瀏覽器
四、在你的工程文件中選擇Build Setting,在Search Paths中添加 libWeChatSDK.a, WXApi.h, WXApiObject.h 三個文件所在位置安全
五、配置info.plist文件服務器
六、URL type設置
選中你的工程設置項,選中targets一欄,在info標籤欄的URL type 添加URL scheme 爲你所註冊的應用程序id微信
一、app跳轉微信公衆號
在工程中添加pch文件,導入 WXApi.h頭文件,並在你須要跳轉的界面增長WXApiDelegate協議
在你的跳轉方法中添加一下代碼:微信開發
JumpToBizProfileReq *req = [JumpToBizProfileReq new]; req.username = @"gh_64a69d298ecf"; // 原始ID req.profileType = WXBizProfileType_Normal; req.extMsg = @""; [WXApi sendReq:req];
這樣APP就能夠掉起微信界面,進入微信公衆號(若是以前沒有關注過此微信公衆號,就會進入微信公衆號的關注界面)app
二、APP跳轉到微信相關的其餘網頁(如微信小店相關商品信息界面)異步
OpenWebviewReq *req = [OpenWebviewReq new]; req.url = @"須要微信內置瀏覽器打開的網址"; [WXApi sendReq:req];
PS:
原始ID: 申請公共號時系統給你的惟一編號,有此 id你更改微信號後依然能綁定公共號
APPID: 開通 api 服務時,系統給你的惟一編號
APPSECRET: 通俗一點兒來講就是密鑰微信支付
流程可能看起來有點複雜,下面我儘可能給大家配圖來講明
一、APP客戶端下單(選擇商品)
二、APP將商品數據傳給後臺服務器,請求生成支付訂單
三、後臺調用統一下單API向微信服務器發送請求生成預付單,並生成一個prepay_id返回給後臺,後臺再將prepay_id放回給客戶端,成功生成訂單信息
生成訂單相關代碼
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *signParaDic = @{@"schoolId":schoolId, @"studentId":studentId, @"parentId":parentId, @"parentTel":phone, @"totalFee":[NSString stringWithFormat:@"%i",payProduct.totalFee], @"productId":[NSString stringWithFormat:@"%i",payProduct.productId], @"productName":payProduct.productName}; NSString *sign = [EncodeParameter md5:signParaDic]; NSDictionary *parameters = @{@"schoolId":schoolId, @"parentId":parentId, @"studentId":studentId, @"parentTel":phone, @"totalFee":[NSString stringWithFormat:@"%i",payProduct.totalFee], @"productId":[NSString stringWithFormat:@"%i",payProduct.productId], @"productName":payProduct.productName, @"sign":sign}; NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:WechatPlaceOrderAPI parameters:parameters error:nil]; _palceOrderOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { // NSDictionary *resDic = (NSDictionary *)responseObject; NSNumber *code = [resDic objectForKey:@"result"]; NSString *msg = [resDic objectForKey:@"message"]; if ([code integerValue] == Success) { WeChatPreOrder *preOrder = [[WeChatPreOrder alloc] initWithDic:resDic]; if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderSucess:preOder:)]) { [self.delegate placeOrderSucess:self preOder:preOrder]; } } else if ([code integerValue] == 300) { if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderFailed:error:)]) { [self.delegate placeOrderFailed:self error:msg]; } }else { if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderFailed:error:)]) { [self.delegate placeOrderFailed:self error:RequestError]; } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderFailed:error:)]) { [self.delegate placeOrderFailed:self error:RequestError]; } }];
四、APP調用SDK打開微信,進行支付
微信客戶端向微信服務器發起支付請求並返回支付結果(中間交互參數prepay_id,微信服務器要驗證這個參數是否跟以前的prepay_id一致)
掉起微信支付相關代碼
PayReq* req = [[PayReq alloc] init]; req.partnerId = weChatPreOrder.partnerId; req.prepayId = weChatPreOrder.prepayId; req.nonceStr = weChatPreOrder.noncestr; req.timeStamp = [weChatPreOrder.timestamp intValue]; req.package = weChatPreOrder.package; req.sign = weChatPreOrder.sign; [WXApi sendReq:req];
五、用戶輸入支付密碼後,微信客戶端跟微信服務器交互,確認支付
六、微信服務器將支付結果返回給微信客戶端,並異步發送支付結果給咱們後臺
七、客戶端能夠調用後臺查詢支付訂單接口,查詢是否支付成功,並顯示支付結果
查詢訂單相關代碼
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *signParaDic = @{@"schoolId":[BCHTTPRequest schoolId],@"parentId":[BCHTTPRequest getUserId]}; NSString *sign = [EncodeParameter md5:signParaDic]; NSDictionary *parameters = @{@"schoolId":[BCHTTPRequest schoolId],@"parentId":[BCHTTPRequest getUserId],@"sign":sign}; NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:QueryOrderAPI parameters:parameters error:nil]; _operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { // NSDictionary *resDic = (NSDictionary *)responseObject; NSNumber *code = [resDic objectForKey:@"result"]; NSString *msg = [resDic objectForKey:@"message"]; if ([code integerValue] == Success) { NSArray *order = [self orderArray:resDic]; if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderSucess:orders:)]) { [self.delegate queryOrderSucess:self orders:order]; } } else if ([code integerValue] == 300) { if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderFailed:error:)]) { [self.delegate queryOrderFailed:self error:msg]; } }else { if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderFailed:error:)]) { [self.delegate queryOrderFailed:self error:RequestError]; } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderFailed:error:)]) { [self.delegate queryOrderFailed:self error:RequestError]; } }];
+ (NSString *)md5:(NSDictionary *)paraDic { // NOTE: 排序,得出最終請求字串 NSArray* sortedKeyArray = [[paraDic allKeys] sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2]; }]; NSMutableArray *tmpArray = [NSMutableArray new]; for (NSString* key in sortedKeyArray) { NSString* value = [paraDic objectForKey:key]; if ([value isKindOfClass:[NSNumber class]]) { value = [(NSNumber *)value stringValue]; } if (value.length > 0) { NSString *keyValueStr = [NSString stringWithFormat:@"%@=%@",key,value]; [tmpArray addObject:keyValueStr]; } } NSString *parameterStr = [tmpArray componentsJoinedByString:@"&"]; NSString *res = [NSString stringWithFormat:@"%@&key=%@",parameterStr,Key]; return [[BCBaseObject MD5Hash:res] uppercaseString]; }