标签:ima href ann 提示 suse 返回 sar 网上 发微信
pod 'WechatOpenSDK'
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 注册APP,这里的字符串就是Wechat URL Scheme里面对应的ID 也是申请回来的ID,必须一致
[WXApi registerApp:@"这里填写申请回来的ID"];
return YES;
}
#pragma mark - 微信支付
- (void)wechatPay {
// 把生成的订单信息组装起来传给服务器,如何组装就和服务器约定好
[[TWTShoppingCartLogic sharedData] goToWechatEasyPay:self.orderStr way:@"2" complete:^(NSError *error, id data) {
NSMutableString *stamp = [data objectForKey:@"timestamp"];
// 调起微信支付
PayReq *req = [[PayReq alloc] init];
req.partnerId = [data objectForKey:@"partnerid"];
req.prepayId = [data objectForKey:@"prepayid"];
req.nonceStr = [data objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [data objectForKey:@"package"];
req.sign = [data objectForKey:@"sign"];
[WXApi sendReq:req];
}];
}
{
"appid" : "wxb4b",微信开放平台审核通过的AppID
"noncestr" : "171127dd056d05e423c8b9e",随机字符串
"package" : "Sign=WXPay", 固定值
"partnerid" : "130", 微信支付分配的商户ID
"prepayid" : "wx201609291601", 预支付交易会话ID
"sign" : "684371081C049B6017641", 签名,除了sign,剩下6个组合的再次签名字符串
"timestamp" : 147513 当前时间
}
// 创建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:@""]
&& ![categoryId isEqualToString:@"sign"]
&& ![categoryId isEqualToString:@"key"]
) {
[contentString appendFormat:@"%@=%@&", categoryId, [dict objectForKey:categoryId]];
}
}
// 添加key字段
[contentString appendFormat:@"key=%@", self.spKey];
// 得到MD5 sign签名
NSString *md5Sign =[contentString MD5];
return md5Sign;
}
- (NSMutableDictionary *)payWithprePayid:(NSString *)prePayid {
if(prePayid == nil) {
NSLog(@"prePayid 为空");
return nil;
}
// 获取到prepayid后进行第二次签名
NSString *package, *time_stamp, *nonce_str;
// 设置支付参数
time_t now;
time(&now);
time_stamp = [NSString stringWithFormat:@"%ld", now];
nonce_str = [time_stamp MD5];
// 重新按提交格式组包,微信客户端暂只支持package = Sign = WXPay格式,须考虑升级后支持携带package具体参数的情况
// package = [NSString stringWithFormat:@"Sign = %@",package];
package = @"Sign = WXPay";
// 第二次签名参数列表
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
NSLog(@"%@", signParams);
[signParams setObject: self.appId forKey:@"appid"];
[signParams setObject: self.mchId forKey:@"partnerid"];
[signParams setObject: nonce_str forKey:@"noncestr"];
[signParams setObject: package forKey:@"package"];
[signParams setObject: time_stamp forKey:@"timestamp"];
[signParams setObject: prePayid forKey:@"prepayid"];
// 生成签名
NSString *sign = [self createMd5Sign:signParams];
// 添加签名
[signParams setObject: sign forKey:@"sign"];
//返回参数列表
return signParams;
}
Appdelegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// 跳转到URL scheme中配置的地址
// NSLog(@"跳转到URL scheme中配置的地址-->%@",url);
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
// 支付成功时调用,回到第三方应用中
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
// 微信调用结束
if ([url.scheme isEqualToString:WECHAT_APPKEY]) {
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
}
[[NSUserDefaultsstandardUserDefaults] setValue:self.orderStrforKey:@"WECHAT_PAY_ORDER_TRADEID"];
[[NSUserDefaultsstandardUserDefaults] synchronize];
// 微信回调,有支付结果的时候会回调这个方法
- (void)onResp:(BaseResp *)resp {
if([resp isKindOfClass:[PayResp class]]) {
// 支付返回结果,实际支付结果需要去微信服务器端查询
NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"];
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付结果:成功!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
// 这里别用返回的状态来确定是否正真支付成功了,这样是不对的,我们必须拿着存到本地的traderID去服务器再次check,这样和服务器收到的异步回调结果匹配之后才能确认是否真的已经支付成功了
[[TWTShoppingCartLogic sharedData] gotoCheckWeChatOrder:tradeID compelete:^(NSError *error, id data) {
// 二次确认
}];
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
break;
}
}
}
标签:ima href ann 提示 suse 返回 sar 网上 发微信
原文地址:https://www.cnblogs.com/CH520/p/10801180.html