编程

当前位置:澳门新莆京娱乐官网 > 编程 > 微信支付开发

微信支付开发

来源:http://www.drgigabytes.com 作者:澳门新莆京娱乐官网 时间:2020-04-06 22:11

Wechat支付的demo写的倒横直竖的,各个参数名以致参数封装混乱,好不轻巧理清之后,写下那篇文章帮忙现在要做Wechat支付的小朋侪少走弯路。

图片 1业务流程

看完那张流程图可获知,我们iOS端要做的政工仅仅就是

  1. 向服务端伏乞预付付,取得prepayid以致noncestr
  2. 把参数拼起来签字
  3. 呼吁支付央求
  4. 管理支付结果

百依百顺早先时代做过Wechat共享之类的同伴已经很熟练了,小编不再赘言,请看iOS接入指南

1.向服务端伏乞预付付,得到prepayid以致noncestr
/** * 微信预支付 * * @param uid 用户id * @param sid 学校id * @param num 数量 * @param cid 商品id * @param ip 客户端ip * @param block 回调 * * @return task */+ (NSURLSessionDataTask *)getPrepayWithUid:uid sid:(NSInteger)sid num:(NSInteger)num cid:cid ip:(NSString *)ip block:(NSString *prepay_id,NSString *nonce_str, NSError *error))block{ NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; [parameters setObject:@ forKey:@"uid"]; [parameters setObject:@ forKey:@"sid"]; [parameters setObject:@ forKey:@"num"]; [parameters setObject:@ forKey:@"cid"]; [parameters setObject:ip forKey:@"ip"]; NSString *url = @"接口名"; return [[ZXApiClient sharedClient] POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id JSON) { NSString *prepay_id = [[JSON objectForKey:@"prepay_id"] stringValue]; NSString *nonce_str = [[JSON objectForKey:@"nonce_str"] stringValue]; !block?:block(prepay_id,nonce_str,nil); } failure:^(NSURLSessionDataTask *__unused task, NSError *error) { !block?:block(nil,nil,error); }];}

上述接口只是二个示范,除了ip那个参数,别的都基于自个儿须求来。事实上ip这几个参数也并未怎么卵用,因为本人看我们都是传的都以192.168.1.1

2.把参数拼起来具名

那边自身把参数封装成一个类,用来处理种种参数拼接的章程

ZXWeixinSignParams.h

//微信开发平台应用id#define Weixin_Appid @""//财付通商户号#define Weixin_MCH_ID @""//商户号对应的密钥#define Weixin_PARTNER_ID @""@interface ZXWeixinSignParams : NSObject@property (nonatomic , copy) NSString *appid;@property (nonatomic , copy) NSString *noncestr;@property (nonatomic , copy) NSString *package;@property (nonatomic , copy) NSString *partnerid;@property (nonatomic , copy) NSString *timestamp;@property (nonatomic , copy) NSString *prepayid;- (instancetype)initWithPrepayid:(NSString *)prepayid noncestr:(NSString *)noncestr;- (NSString *)sign;@end

ZXWeixinSignParams.m

@implementation ZXWeixinSignParams- (instancetype)initWithPrepayid:(NSString *)prepayid noncestr:(NSString *)noncestr{ self = [super init]; if  { self.prepayid = prepayid; self.noncestr = noncestr; } return self;}- (NSString *)appid{ if  { _appid = Weixin_Appid; } return _appid;}- (NSString *)timestamp{ if (!_timestamp) { time_t now; time; _timestamp = [NSString stringWithFormat:@"%ld", now]; } return _timestamp;}- (NSString *)package{ if (!_package) { _package = @"Sign=WXPay"; } return _package;}- (NSString *)partnerid{ if (!_partnerid) { _partnerid = Weixin_MCH_ID; } return _partnerid;}- (NSString *)sign{ NSDictionary *dic = [self keyValues]; return [self createMd5Sign:[dic mutableCopy]];}- (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=%@", Weixin_PARTNER_ID]; //得到MD5 sign签名 NSString *md5Sign = [[contentString md5] uppercaseString]; return md5Sign;}@end

- (NSString *)sign艺术正是东挪西撮参数具名了。当中NSDictionary *dic = [self keyValues];那有些自个儿利用了MJExtension很有益的词典模型调换,还应该有NSString *md5Sign = [[contentString md5] uppercaseString];那某个运用了小编本身写的md5,demo里的能够直接拿来用。

3.发起支付央求
- sendPrepay{ MBProgressHUD *hud = [MBProgressHUD showWaiting:@"" toView:self.view]; [ZXMessageBill getPrepayWithUid:GLOBAL_UID sid:[ZXUtils sharedInstance].currentSchool.sid num:self.num cid:self.messageCommodity.cid ip:@"196.168.1.1" block:^(NSString *prepay_id, NSString *nonce_str, NSError *error) { self.prepay_id = prepay_id; self.nonce_str = nonce_str; if (self.prepay_id && self.nonce_str) { [hud hide:YES]; [self weixinpay]; } else { [hud turnToError:@"订单提交失败,请重试"]; } }];}- weixinpay{ ZXWeixinSignParams *signParams = [[ZXWeixinSignParams alloc] initWithPrepayid:self.prepay_id noncestr:self.nonce_str]; //调起微信支付 PayReq *req = [[PayReq alloc] init]; req.openID = signParams.appid; req.partnerId = signParams.partnerid; req.prepayId = signParams.prepayid; req.nonceStr = signParams.noncestr; req.timeStamp = signParams.timestamp.intValue; req.package = signParams.package; req.sign = [signParams sign]; [WXApi sendReq:req];}

那般看起来是否比demo里清爽的多啊?

4. 甩卖支付结果
- application:(UIApplication *)application handleOpenURL:url{ if ([url.absoluteString hasPrefix:Weixin_Appid]) { return [WXApi handleOpenURL:url delegate:self]; } return YES;}- application:(UIApplication *)application openURL:url sourceApplication:(NSString *)sourceApplication annotation:annotation { if ([url.absoluteString hasPrefix:Weixin_Appid]) { return [WXApi handleOpenURL:url delegate:self]; } return YES;}- onResp:(BaseResp*)resp{ if([resp isKindOfClass:[SendMessageToWXResp class]]) { SendMessageToWXResp *smresp = (SendMessageToWXResp *)resp; if (smresp.errCode == 0) { [MBProgressHUD showSuccess:@"分享成功" toView:nil]; [ZXUmengHelper logShare]; } } else if([resp isKindOfClass:[PayResp class]]){ //支付返回结果,实际支付结果需要去微信服务器端查询 switch (resp.errCode) { case WXSuccess: { [MBProgressHUD showSuccess:@"支付成功" toView:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:weixnpaySuccessNotification object:nil]; } NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode); break; default: [MBProgressHUD showText:[NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr] toView:nil]; NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr); break; } }}

onResp里管理支付结果,成功了发个照应给viewController,退步了就弹个提示。

万一你跳转到Wechat里之只弹出了七个鲜明开关,那么那时候你要求检讨多少个地点

  1. appid,mch_id,partner_id是不是设置科学。mch_id对应的key其实是partnerid,partner_id对应的key其实是key,Wechat在那挖了个小赤沙,相信踩过的校友不在少数。
  2. timestamp 应为10位
  3. 具名须要排序,并用md5加密,例子里应用了全大写,小写没试过不掌握会不会出标题
  4. 一经集成了友盟,並且友盟含有微信分享,先注册友盟,再登记Wechat

本文由澳门新莆京娱乐官网发布于编程,转载请注明出处:微信支付开发

关键词: