美文网首页
微信支付

微信支付

作者: chulijun | 来源:发表于2018-04-06 22:22 被阅读18次

说明:最近公司要求做微信支付,而且还是从h5 界面 调取微信支付,支付成功之后结果返回h5 的需求,(使用WebViewJavascriptBridge 桥接 ,这里就不赘述了,有兴趣的同学可以搜索一下)。呵呵,然后的然后就整了一下微信支付的内容,写出总结埋坑经过。文章结尾附有demo 以供参考

第一步:导入SDK ,本工程是使用的pod  ,命令为 :pod 'WechatOpenSDK'

第二步 配置 info.plist 白名单 LSApplicationQueriesSchemes

    第三步  配置URL schemes  wx251d0396a1ab****  //微信开放平台获取

    第四步。  appDelegate.m 导入头文件 遵循代理 设置回调和通知监听 回调结果

```

#import "AppDelegate.h"

#import "WXApi.h" //导入微信头文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//这里需要换成 自己app 在微信开放平台申请的appID 注意 这里的appID 对应的bunlde ID 一定要和微信开放平台的一致

[WXApi registerApp:@"wxasdmmmvme123"];//注册微信 如果你的工程中导入了友盟分享 建议在友盟之后注册

return YES;

}

// 支持所有iOS系统

- (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 微信回调方法

- (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;

    //判断是微信消息的回调

    if ([resp isKindOfClass:[SendMessageToWXResp class]]){

       strTitle = [NSString stringWithFormat:@"发送媒体消息的结果"];

    }

   NSString * wxPayResult;

    //判断是否是微信支付回调 (注意是PayResp 而不是PayReq)

    if ([resp isKindOfClass:[PayResp class]]){

        //支付返回的结果, 实际支付结果需要去微信服务器端查询

strTitle = [NSString stringWithFormat:@"支付结果"];

       switch (resp.errCode) {

            case WXSuccess: {

                strMsg = @"支付结果:";

                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;

            }

        }

 //发出通知

        NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult];

        [[NSNotificationCenter defaultCenter] postNotification:notification];

    }

}

```



-(void)btnClick:(UIButton*)button{

    NSLog(@"点击了支付按钮");

    if([WXApi isWXAppInstalled]){

        // 监听一个通知

        [[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 特别注意 这里的时间戳 如果你传递的格式不对 就会一直报签名认证失败

    request.timeStamp= [[NSString stringWithFormat:@"1523021724"]intValue];

    request.sign= @"9576856176CB27B1CCAD36CFF23F882E";

    [WXApi sendReq:request];

}

#pragma mark - 收到支付成功的消息后作相应的处理

- (void)getOrderPayResult:(NSNotification *)notification{

    if ([notification.object isEqualToString:@"success"]) {

        NSLog(@"支付成功");

    } else if([notification.object isEqualToString:@"cancel"]){

        NSLog(@"取消支付");

   }else{

       NSLog(@"支付失败");

    }

}

demo地址:微信支付demo

参考资料:同时接入其他三方,不走回调

参考资料 微信支付1

参考资料 微信支付2

相关文章

网友评论

      本文标题:微信支付

      本文链接:https://www.haomeiwen.com/subject/kctehftx.html