微信支付:
首先需要在入口函数注册:
//向微信注册 [WXApi registerApp:APP_ID withDescription:@"微信Api"];
注册回调delegate:
- (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 { if ([sourceApplication isEqualToString:@"com.tencent.xin"]) { return [WXApi handleOpenURL:url delegate:self]; } }
- (void)onResp:(BaseResp *)resp { if ([resp isKindOfClass:[PayResp class]]) { PayResp *response = (PayResp *)resp; switch (response.errCode) { case WXSuccess: { //服务器端查询支付通知或查询API返回的结果再提示成功 break; } default: { break; } } } }
发起微信支付代码:
//================================ //预付单参数订单设置 //================================ NSDictionary *dict = [resultDic objectForKey:@"data"]; NSNumber *ordernoNum = [dict objectForKey:@"payId"]; NSString *orderno = [ordernoNum stringValue]; NSString *order_name = @"来啦洗车-支付订单"; NSString *order_price = [NSString stringWithFormat:@"%d", [[SharedInfo shared].totalAmount intValue] * 100]; srand( (unsigned)time(0) ); NSString *noncestr = [NSString stringWithFormat:@"%d", rand()]; NSMutableDictionary *packageParams = [NSMutableDictionary dictionary]; NSString *ipAddress = [SharedInfo getIPAddress:YES]; [packageParams setObject: APP_ID forKey:@"appid"]; //开放平台appid [packageParams setObject: MCH_ID forKey:@"mch_id"]; //商户号 [packageParams setObject: noncestr forKey:@"nonce_str"]; //随机串 [packageParams setObject: @"APP" forKey:@"trade_type"]; //支付类型,固定为APP [packageParams setObject: order_name forKey:@"body"]; //订单描述,展示给用户 [packageParams setObject: NOTIFY_URL forKey:@"notify_url"]; //支付结果异步通知 [packageParams setObject: orderno forKey:@"out_trade_no"];//商户订单号 [packageParams setObject: ipAddress forKey:@"spbill_create_ip"];//发器支付的机器ip [packageParams setObject: order_price forKey:@"total_fee"]; //订单金额,单位为分 //获取prepayId(预支付交易会话标识) payRequsestHandler *payHandler = [[payRequsestHandler alloc] init]; [payHandler init:APP_ID mch_id:MCH_ID]; [payHandler setKey:PARTNER_ID]; NSString *prePayid; prePayid = [payHandler sendPrepay:packageParams]; if ( prePayid != nil) { //获取到prepayid后进行第二次签名 NSString *package, *time_stamp, *nonce_str; //设置支付参数 time_t now; time(&now); time_stamp = [NSString stringWithFormat:@"%ld", now]; nonce_str = [WXUtil md5:time_stamp]; //重新按提交格式组包,微信客户端暂只支持package=Sign=WXPay格式,须考虑升级后支持携带package具体参数的情况 //package = [NSString stringWithFormat:@"Sign=%@",package]; package = @"Sign=WXPay"; //第二次签名参数列表 NSMutableDictionary *signParams = [NSMutableDictionary dictionary]; [signParams setObject: APP_ID forKey:@"appid"]; [signParams setObject: nonce_str forKey:@"noncestr"]; [signParams setObject: package forKey:@"package"]; [signParams setObject: MCH_ID forKey:@"partnerid"]; [signParams setObject: time_stamp forKey:@"timestamp"]; [signParams setObject: prePayid forKey:@"prepayid"]; //[signParams setObject: @"MD5" forKey:@"signType"]; //生成签名 NSString *sign = [payHandler createMd5Sign:signParams]; //添加签名 [signParams setObject: sign forKey:@"sign"]; NSMutableString *stamp = [signParams objectForKey:@"timestamp"]; //调起微信支付 PayReq* req = [[PayReq alloc] init]; req.openID = [signParams objectForKey:@"appid"]; req.partnerId = [signParams objectForKey:@"partnerid"]; req.prepayId = [signParams objectForKey:@"prepayid"]; req.nonceStr = [signParams objectForKey:@"noncestr"]; req.timeStamp = stamp.intValue; req.package = [signParams objectForKey:@"package"]; req.sign = [signParams objectForKey:@"sign"]; [SharedInfo shared].aliPayType = @"2"; [WXApi sendReq:req];
微信分享代码:
UIImage *image = [UIImage imageNamed:@"ShareSmallImage"]; WXMediaMessage *message = [WXMediaMessage message]; [message setThumbImage:image]; // WXImageObject *ext = [WXImageObject object]; // ext.imageData = UIImageJPEGRepresentation(image, 1.0f); WXAppExtendObject *ext = [WXAppExtendObject object]; ext.url = @"https://itunes.apple.com/cn/app/lai-la-xi-che/id990872887?l=en&mt=8"; message.mediaObject = ext; message.title = @"来啦洗车"; message.description = @"欢迎使用来啦洗车"; SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; req.bText = NO; // req.text = @"123"; req.message = message; req.scene = 0; [WXApi sendReq:req];
scene: 0为会话,1为朋友圈,2为收藏
message和text只能二选一
时间: 2024-10-09 23:12:23