微信支付接入流程:
下载微信支付idk 地址: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&lang=zh_CN
将下载好的资源包放入工程
设置bit code 为NO
#import "WXApi.h"
//遵守协议
@interface AppDelegate ()<WXApiDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//微信支付注册
[WXApi registerApp:@"你申请的微信appId"];
return YES;
}
//下面两个方法已废弃
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [WXApi handleOpenURL:url delegate:self];
}
//注意要写此方法,否则支付结果不会回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
{
if ([url.host isEqualToString:@"safepay"]) {
return [WXApi handleOpenURL:url delegate:self];
}
//微信支付回调方法
- (void)onResp:(BaseResp *)resp
{
switch (resp.errCode) {
case WXSuccess:
{
//成功
}
break;
case WXErrCodeCommon:
{
//普通错误类型
}
break;
case WXErrCodeUserCancel:
{
//用户点击取消并返回
}
break;
case WXErrCodeSentFail:
{
//发送失败
}
break;
case WXErrCodeAuthDeny:
{
//授权失败
}
break;
case WXErrCodeUnsupport:
{
//微信不支持
}
break;
default:
break;
}
}
支付页面导入头文件
#import "WXApi.h"
- (void)pay{
//dic[@"sign"]是后台统一下单返回的数据,前端调起支付需要后台先在微信完成统一下单
NSDictionary *resultDic = dic[@"sign"];
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
int time_stamp=(int)[date timeIntervalSince1970];
//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = [resultDic objectForKey:@"mch_id"];
req.prepayId = [resultDic objectForKey:@"prepay_id"];
req.nonceStr = [resultDic objectForKey:@"nonce_str"];
req.timeStamp = time_stamp;
req.package = @"Sign=WXPay";
NSDictionary *parameters =@{@"appid":@"wxc896d9d1b6757532",@"partnerid":req.partnerId,@"prepayid":req.prepayId,@"package":@"Sign=WXPay",@"noncestr":req.nonceStr,@"timestamp":[[NSString alloc]initWithFormat:@"%d",time_stamp]};
NSMutableDictionary *mutableDic=[[NSMutableDictionary alloc]initWithDictionary:parameters];
req.sign=[self createMd5Sign:mutableDic];
//发起支付请求
[WXApi sendReq:req];
}
//微信支付创建package签名
-(NSString *)createMd5Sign:(NSMutableDictionary*)dict
{
NSMutableString *contentString =[NSMutableString string];
NSArray *keys = [dict allKeys];
//按字母顺序排序
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
//拼接字符串
for (NSString *categoryId in sortedArray) {
if ( ![[dict objectForKey:categoryId] isEqualToString:@""]
&& ![[dict objectForKey:categoryId] isEqualToString:@"sign"]
&& ![[dict objectForKey:categoryId] isEqualToString:@"key"]
){
[contentString appendFormat:@"%@=%@&", categoryId, [dict objectForKey:categoryId]];
}
}
//添加key字段
[contentString appendFormat:@"key=%@", @"你的秘钥/我用的微信的secret字符串也是可以的,因为后台在统一下单时用的这个,所以这个地方和后台保持一致就好"];
//得到MD5 sign签名
NSString *md5Sign =[contentString md5];
return md5Sign;
}
//MD5加密
- (NSString *)md5
{
if(self == nil || [self length] == 0){
return nil;
}
const char *value = [self UTF8String];
unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(value, (CC_LONG)strlen(value), outputBuffer);
NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
[outputString appendFormat:@"%02x",outputBuffer[count]];
}
return outputString;
}
这样支付就完成了,如果不回调支付结果,检查一下AppDelegate里面是不是写错了代理方法