- 微信支付
- (void)wechatPayClick{ NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:WeChatAppID forKey:@"appid"]; [dict setObject:@"税务代理预缴税金" forKey:@"body"]; [dict setObject:WeChatMCH_ID forKey:@"mch_id"]; [dict setObject:[AppMethod getRandomString] forKey:@"nonce_str"]; [dict setObject:WeChatNOTIFY_URL forKey:@"notify_url"]; [dict setObject:@"ZLDD-201703070014" forKey:@"out_trade_no"]; [dict setObject:[AppMethod deviceIPAdress] forKey:@"spbill_create_ip"]; [dict setObject:@"1" forKey:@"total_fee"]; [dict setObject:@"APP" forKey:@"trade_type"]; NSDictionary *params = [AppMethod partnerSignOrder:dict]; NSString *postStr = [params XMLString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.mch.weixin.qq.com/pay/unifiedorder"]]; [request setHTTPMethod:@"POST"]; [request addValue: @"text/html,text/plain,text/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]]; NSURLSession *requestSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; NSURLSessionDataTask *task = [requestSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable body , NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"Response object: %@" , response); NSString *bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]; // NSDictionary *dic=[bodyString mj_JSONObject]; //打印应答中的body NSLog(@"Response body: %@" , body); NSDictionary *dic= [[XMLDictionaryParser sharedInstance] dictionaryWithData:body]; PayReq* req = [[PayReq alloc] init]; req.partnerId = WeChatMCH_ID; req.prepayId = [dic objectForKey:@"prepay_id"]; req.nonceStr = [dic objectForKey:@"nonce_str"]; req.timeStamp = [[NSString stringWithFormat:@"%.0f",[[NSDate date] timeIntervalSince1970]] intValue]; req.package = @"Sign=WXPay"; NSMutableDictionary *rdict = [NSMutableDictionary dictionary]; [rdict setObject:WeChatAppID forKey:@"appid"]; [rdict setObject:req.partnerId forKey:@"partnerid"]; [rdict setObject:req.prepayId forKey:@"prepayid"]; [rdict setObject:req.nonceStr forKey:@"noncestr"]; [rdict setObject:[NSString stringWithFormat:@"%u",(unsigned int)req.timeStamp] forKey:@"timestamp"]; [rdict setObject:req.package forKey:@"package"]; NSDictionary *result = [AppMethod partnerSignOrder:rdict]; req.sign = [result objectForKey:@"sign"]; [WXApi sendReq:req]; }]; [task resume]; }复制代码
-
拼接参数:
-
上传接口,获取预下单ID,打开微信,支付