說(shuō)明:最近公司要求做微信支付,而且還是從h5 界面 調(diào)取微信支付,支付成功之后結(jié)果返回h5 的需求,(使用WebViewJavascriptBridge 橋接 ,這里就不贅述了,有興趣的同學(xué)可以搜索一下)。呵呵,然后的然后就整了一下微信支付的內(nèi)容,寫(xiě)出總結(jié)埋坑經(jīng)過(guò)。文章結(jié)尾附有demo 以供參考
第一步:導(dǎo)入SDK ,本工程是使用的pod ?,命令為 :pod 'WechatOpenSDK'
第二步 配置 info.plist 白名單 LSApplicationQueriesSchemes

? ? 第三步? 配置URL schemes? wx251d0396a1ab**** ?//微信開(kāi)放平臺(tái)獲取

? ? 第四步。? appDelegate.m 導(dǎo)入頭文件 遵循代理 設(shè)置回調(diào)和通知監(jiān)聽(tīng) 回調(diào)結(jié)果
```
#import "AppDelegate.h"
#import "WXApi.h" //導(dǎo)入微信頭文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//這里需要換成 自己app 在微信開(kāi)放平臺(tái)申請(qǐng)的appID 注意 這里的appID 對(duì)應(yīng)的bunlde ID 一定要和微信開(kāi)放平臺(tái)的一致
[WXApi registerApp:@"wxasdmmmvme123"];//注冊(cè)微信 如果你的工程中導(dǎo)入了友盟分享 建議在友盟之后注冊(cè)
return YES;
}
// 支持所有iOS系統(tǒng)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
BOOL result = [WXApi handleOpenURL:url delegate:(id)self];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options{
?BOOL result = [WXApi handleOpenURL:url delegate:(id)self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
?BOOL result = [WXApi handleOpenURL:url delegate:(id)self];
return result;
}
#pragma mark 微信回調(diào)方法
- (void)onResp:(BaseResp *)resp{
? ? NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
? ? NSLog(@"strMsg: %@",strMsg);
? ?NSString * errStr? ? ? = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
? ? NSLog(@"errStr: %@",errStr);
? ? NSString * strTitle;
? ? //判斷是微信消息的回調(diào)
? ? if ([resp isKindOfClass:[SendMessageToWXResp class]]){
? ? ? ?strTitle = [NSString stringWithFormat:@"發(fā)送媒體消息的結(jié)果"];
? ? }
? ?NSString * wxPayResult;
? ? //判斷是否是微信支付回調(diào) (注意是PayResp 而不是PayReq)
? ? if ([resp isKindOfClass:[PayResp class]]){
? ? ? ? //支付返回的結(jié)果, 實(shí)際支付結(jié)果需要去微信服務(wù)器端查詢
strTitle = [NSString stringWithFormat:@"支付結(jié)果"];
? ? ? ?switch (resp.errCode) {
? ? ? ? ? ? case WXSuccess: {
? ? ? ? ? ? ? ? strMsg = @"支付結(jié)果:";
? ? ? ? ? ? ? ? NSLog(@"支付成功: %d",resp.errCode);
? ? ? ? ? ? ? ?wxPayResult = @"success";
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case WXErrCodeUserCancel:{
? ? ? ? ? ? ? ? strMsg = @"用戶取消了支付";
? ? ? ? ? ? ? ? NSLog(@"用戶取消支付: %d",resp.errCode);
? ? ? ? ? ? ? ? wxPayResult = @"cancel";
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
default:
? ? ? ? ? ? {
? ? ? ? ? ? ? ? strMsg = [NSString stringWithFormat:@"支付失敗! code: %d? errorStr: %@",resp.errCode,resp.errStr];
? ? ? ? ? ? ? ? NSLog(@":支付失敗: code: %d str: %@",resp.errCode,resp.errStr);
? ? ? ? ? ? ? ? wxPayResult = @"faile";
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
?//發(fā)出通知
? ? ? ? NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult];
? ? ? ? [[NSNotificationCenter defaultCenter] postNotification:notification];
? ? }
}
```
-(void)btnClick:(UIButton*)button{
? ? NSLog(@"點(diǎn)擊了支付按鈕");
? ? if([WXApi isWXAppInstalled]){
? ? ? ? // 監(jiān)聽(tīng)一個(gè)通知
? ? ? ? [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
? ? ? ? [self wxpayRequest];
? ? }else{
? ? ? ? [self alertShowWxpayResoult];
? ? }
}
-(void)wxpayRequest{
? ? PayReq *request = [[PayReq alloc] init];
? ? request.partnerId = @"1491180982";
? ? request.prepayId= @"wx06213524871559ea7697f9dc3524188023";
? ? request.package = @"Sign=WXPay";
? ? request.nonceStr= @"JOXFbaB5PHqAWdFb";
#warning Mark 特別注意 這里的時(shí)間戳 如果你傳遞的格式不對(duì) 就會(huì)一直報(bào)簽名認(rèn)證失敗
? ? request.timeStamp= [[NSString stringWithFormat:@"1523021724"]intValue];
? ? request.sign= @"9576856176CB27B1CCAD36CFF23F882E";
? ? [WXApi sendReq:request];
}
#pragma mark - 收到支付成功的消息后作相應(yīng)的處理
- (void)getOrderPayResult:(NSNotification *)notification{
? ? if ([notification.object isEqualToString:@"success"]) {
? ? ? ? NSLog(@"支付成功");
? ? } else if([notification.object isEqualToString:@"cancel"]){
? ? ? ? NSLog(@"取消支付");
? ?}else{
? ? ? ?NSLog(@"支付失敗");
? ? }
}
demo地址:微信支付demo