哥最近要用到微信支付了,第一做,研究了一下。
1、首先到微信开放平台上,申请app及与T进行签约、认证
https://open.weixin.qq.com/
获取到:
/**
* 微信开放平台申请得到的 appid, 需要同时添加在info.plist文件中URL schema,用于完成时,回调到app
*/
#define WXAppId @"wxd930ea5d5a258f4f"
以上两个参数用于获取access_token
access_token是APP的全局唯一票据,APP调用各
接口时都需使用access_token。正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。
APP可以使用AppID和AppSecret调用本
接口来获取access_token。AppID和AppSecret可在开放平台后台获得。注意调用
接口时需使用https协议
接口调用请求说明
https://api.weixin.qq.com/cgi-
bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
返回说明
正常情况下,微信会返回下述JSON数据包给开发者:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
参数说明:
appkey 、partnerId、partnerKey、paySignKey
appkey:appkey就是Paysignkey,申请支付通过之后由
财付通下发。
partnerId:财付通商户身份的标识。审核通过后,在财付通发送的邮件中查看。
partnerKey:财付通商户权限密钥Key。审核通过后,在财付通发送的邮件中查看。
paySignKey:除了支付请求需要用到paySignKey,公众平台
接口API的权限获取所需密钥Key,在使用所有公众平台API时,都需要先用它去换取access_token,然后再进行调用。审核通过后,在微信发送的邮件中查看。
2、代码实现
@interface SkyAppDelegate : UIResponder <UIApplicationDelegate, WXApiDelegate> //在appDelegate方法中实现WXApiDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 向微信终端注册appID
[WXApi registerApp:WXAppId withDescription:@"weixin
demo"];
/*! @brief WXApi的成员函数,在微信终端程序中注册第三方应用。
*
* 需要在每次启动第三方应用程序时调用。第一次调用后,会在微信的可用应用列表中出现。
* @param appid 微信开发者ID
* @param appdesc 应用附加信息,长度不超过1024字节
* @return 成功返回YES,失败返回NO。
*/
return YES;
}
//用于完成支付后的程序回调,
- (BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"%@",url);//跳转到URL schema中配置的地址
return [WXApi handleOpenURL:url delegate:self];
}
//收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
- (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:strTitle
message: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;
}
}
}
}
为了保证发送请求的方便,自己封装了两个工具类
1、http
typedef void (^BKHttpCallback)(BOOL isSuccessed, NSDictionary *result);
/**
* GET方法请求数据
*
* @param url 请求的URL
* @param params 请求参数
* @param (BOOL isSuccessed, Result *result))callback 回调方法
*/
+ (void)doGetWithUrl:(NSString *)url path:(NSString *)path params:(NSDictionary *)params callback:(BKHttpCallback) callback;
/**
* 请求WebService数据
*
* @param baseUrl 请求的基础URL
* @param params 请求参数
* @param (BOOL isSuccessed, Result *result))callback 回调方法
*/
+ (vo