微信支付相关代码
2015-08-24 21:00
483 查看
1.申请AppID,导入依赖库,配置URL Scheme这里就不深究了,各位看官请自行百度.
2.在AppDelegate中导入头文件,遵守WXApiDelegate,然后在launch里注册AppID.
3.在支付界面导入头文件,创建支付对象,调起微信
//调起微信支付,传入参数
PayReq* req = [[[PayReq alloc] init]autorelease];
req.openID = APPI_ID;
req.partnerId = PARTNER_ID;
req.prepayId = prePayid;
req.nonceStr = nonce_str;
req.timeStamp = now;
req.package = package;
req.sign = sign;
[WXApi safeSendReq:req];
4.微信支付的回调需要添加在AppDelegate中
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
//返回应用后的回调,通过handleOpenURL进入微信支付的回调onResp:(BaseResp*)resp
return [WXApi handleOpenURL:url delegate:self];
}
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
PayResp *response = (PayResp *)resp;
NSString *strTitle = [NSString stringWithFormat:@"支付结果"];
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", response.errCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitlemessage:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
switch (response.errCode) {
case WXSuccess: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"fail"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
}
但是将微信支付相关的代码全部写到AppDelegate中又会显得代码冗余,这时我们需要创建通知,在成功回调后发出通知,在支付界面监听回调,完成支付.
2.在AppDelegate中导入头文件,遵守WXApiDelegate,然后在launch里注册AppID.
3.在支付界面导入头文件,创建支付对象,调起微信
//调起微信支付,传入参数
PayReq* req = [[[PayReq alloc] init]autorelease];
req.openID = APPI_ID;
req.partnerId = PARTNER_ID;
req.prepayId = prePayid;
req.nonceStr = nonce_str;
req.timeStamp = now;
req.package = package;
req.sign = sign;
[WXApi safeSendReq:req];
4.微信支付的回调需要添加在AppDelegate中
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
//返回应用后的回调,通过handleOpenURL进入微信支付的回调onResp:(BaseResp*)resp
return [WXApi handleOpenURL:url delegate:self];
}
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
PayResp *response = (PayResp *)resp;
NSString *strTitle = [NSString stringWithFormat:@"支付结果"];
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", response.errCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitlemessage:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
switch (response.errCode) {
case WXSuccess: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default: {
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"fail"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
}
但是将微信支付相关的代码全部写到AppDelegate中又会显得代码冗余,这时我们需要创建通知,在成功回调后发出通知,在支付界面监听回调,完成支付.
相关文章推荐
- Android 微信分享信息
- 微信朋友圈如何同时分享(图片+文字)
- 微信支付中的jsapi返回提示信息
- 如何在微信企业号中使用简道云
- Android微信SDK实现分享
- opencv基础小程序大集合
- Android应用加入微信分享
- PHP实现微信发红包程序
- 微信打开网址添加在浏览器中打开提示
- .net 2.0 3.5 微信 jssdk 源码
- 微信为什么要限制律师公众号的申请?
- 仿微信朋友圈选择图片
- c#/.net 基于文件流FileStream读写的文本操作小程序
- 利用LibreOffice与ImageMagick将网页分享至微信
- PHP实现微信发红包程序
- 通达云OA2015版及钉钉、微信办公集成产品正式发布
- 公众号搭建流程
- 微信oauth获取用户的信息页面授权
- 微信oauth获取用户的信息页面授权
- 这次带来的小程序是:确定一个字符串有多少个单词,然后最长单词和最短单词有哪些