首先要弄个支付相关的类

@interfaceMOIPayModel:NSObject//channel:1:支付宝2:微信@propertyNSIntegerchannel;//支付金额(分)@propertyfloatprice;@property(strong,nonatomic)NSString*productName;//商品标题@property(strong,nonatomic)NSString*productDescription;//商品描述//后台系统订单ID@property(nonatomic)longlongorderId;//支付宝属性/**PID*/@property(strong,nonatomic)NSString*partner;/**收款支付宝账号*/@property(strong,nonatomic)NSString*seller;/**私钥*/@property(strong,nonatomic)NSString*privateKey;/**返回URL*/@property(strong,nonatomic)NSString*notifyURL;//微信支付属性/**商户ID*/@property(nonatomic,retain)NSString*partnerId;/**预支付ID*/@property(nonatomic,retain)NSString*prepayId;/**商户密钥*/@property(nonatomic,retain)NSString*spKey;@end


先说下支付宝的支付流程吧,其实支付宝的并不需要什么动态信息,可以把所有的信息都写在客户端里,不过这样很不安全,所以还是要向自己的服务器请求相关信息。


_MOIPayModel.notifyURL=@"返回URL";_MOIPayModel.partner=@"PID";_MOIPayModel.seller=@"收款支付宝账号";_MOIPayModel.privateKey=@"商户密钥";

typedefvoid(^MOIPayOderUtilCompletion)(MOIPayModel*payModel,idmessage);typedefvoid(^MOIPayOderUtilFailed)(MOIPayModel*payModel,idmessage);staticNSString*appScheme=@"调用支付的app注册在info.plist中的scheme";-(void)goToAliPayFor:(MOIPayModel*)_payModelcompletion:(MOIPayOderUtilCompletion)_payCompletionfailed:(MOIPayOderUtilFailed)_payFailed{if(!_payModel.partner||!_payModel.seller||!_payModel.productName||!_payModel.notifyURL){NSLog(@"缺少信息,支付将失败");}//将商品信息赋予AlixPayOrder的成员变量Order*order=[[Orderalloc]init];order.partner=_payModel.partner;order.seller=_payModel.seller;order.tradeNO=_payModel.orderNO;//订单ID(由商家自行制定)order.productName=_payModel.productName;//商品标题order.productDescription=_payModel.productDescription;//商品描述order.amount=[NSStringstringWithFormat:@"%g",_payModel.price/100.f];//商品价格order.notifyURL=_payModel.notifyURL;//回调URLorder.service=@"mobile.securitypay.pay";order.paymentType=@"1";order.inputCharset=@"utf-8";order.itBPay=@"30m";order.showUrl=@"m.alipay.com";//将商品信息拼接成字符串NSString*orderSpec=[orderdescription];//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncodeid<DataSigner>signer=CreateRSADataSigner(_payModel.privateKey);NSString*signedString=[signersignString:orderSpec];//将签名成功字符串格式化为订单字符串,请严格按照该格式NSString*orderString=nil;if(signedString!=nil){orderString=[NSStringstringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",orderSpec,signedString,@"RSA"];[[AlipaySDKdefaultService]payOrder:orderStringfromScheme:appSchemecallback:^(NSDictionary*resultDic){intresultStatus=[resultDicintValueForKey:@"resultStatus"];switch(resultStatus){case9000:_payCompletion(nil,nil);break;default:{_payFailed(nil,([resultDicsafeObjectForKey:@"memo"]?:@"支付中断"));}break;}}];}else{_payFailed(nil,@"支付中断");}}


微信支付流程需要先拿到一个‘预支付id’,通常这个id是由后台请求微信服务器得到的,具体的接口什么的我就不写了,毕竟不是客户端的事情。最后需要拿到几个重要信息


_MOIPayModel.partnerId=@"商户ID"_MOIPayModel.prepayId=@"预支付ID";_MOIPayModel.spKey=@"商户密钥";


这三个重要信息都有了 把信息拼起来创建一个SIGN签名 就可以向微信发请求了


PayReq*request=[[PayReqalloc]init];request.partnerId=_payModel.partnerId;request.prepayId=_payModel.prepayId;request.package=@"Sign=WXPay";//随机串request.nonceStr=[selfret32bitString];NSDate*datenow=[NSDatedate];NSString*timeSp=[NSStringstringWithFormat:@"%ld",(long)[datenowtimeIntervalSince1970]];UInt32timeStamp=[timeSpintValue];request.timeStamp=timeStamp;NSDictionary*_dic=@{@"appid":kWXAppID,@"partnerid":request.partnerId,@"prepayid":request.prepayId,@"package":request.package,@"noncestr":request.nonceStr,@"timestamp":@(request.timeStamp)//时间戳};request.sign=[selfcreateMd5Sign:[_dicmutableCopy]spKey:_payModel.spKey];if(![WXApisendReq:request]){NSLog(@"发起支付失败");}


回调结果

-(void)onResp:(BaseResp*)resp;{if([respisKindOfClass:[PayRespclass]]){PayResp*response=(PayResp*)resp;switch(response.errCode){caseWXSuccess:NSLog(@"微信支付成功");break;caseWXErrCodeUserCancel:NSLog(@"交易取消");break;default:NSLog(@"支付失败");break;}}}


用到的一些方法


//创建sign签名-(NSString*)createMd5Sign:(NSMutableDictionary*)dictspKey:(NSString*)_spKey{NSMutableString*contentString=[NSMutableStringstring];NSArray*keys=[dictallKeys];//按字母顺序排序NSArray*sortedArray=[keyssortedArrayUsingComparator:^NSComparisonResult(idobj1,idobj2){return[obj1compare:obj2options:NSNumericSearch];}];//拼接字符串for(NSString*categoryIdinsortedArray){if(![categoryIdisEqualToString:@"sign"]&&![categoryIdisEqualToString:@"key"]){[contentStringappendFormat:@"%@=%@&",categoryId,[dictobjectForKey:categoryId]];}}//添加key字段[contentStringappendFormat:@"key=%@",_spKey];//得到MD5sign签名NSString*md5Sign=[selfmd5HexDigest:contentString];return[md5SignuppercaseString];}//MD5-(NSString*)md5HexDigest:(NSString*)password{if(!password||![passwordisKindOfClass:[NSStringclass]]||password.length==0){return@"";}constchar*original_str=[passwordUTF8String];unsignedcharresult[CC_MD5_DIGEST_LENGTH];CC_MD5(original_str,strlen(original_str),result);NSMutableString*hash=[NSMutableStringstring];for(inti=0;i<16;i++){[hashappendFormat:@"%02X",result[i]];}NSString*mdfiveString=[hashlowercaseString];returnmdfiveString;}//随机字符串-(NSString*)ret32bitString{chardata[32];for(intx=0;x<32;data[x++]=(char)('A'+(arc4random_uniform(26))));return[[NSStringalloc]initWithBytes:datalength:32encoding:NSUTF8StringEncoding];}


当然了,这里不管是支付宝还是微信支付,成功也只是客户端返回的成功,最保险的还是客户端请求自己的服务器,在自己的服务器上确认支付真的已经成功了,再继续业务流程,毕竟支付流程还是要谨慎一点的。