iOS应用添加第三方支付

最简单明了的教程还是官方文档,以下是我给应用添加支付功能,记录下。

1、微信支付(官方文档:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5)

假设你的应用在微信开放平台已经创建、通过审核并获取了支付功能。

拿到的相应参数、项目中成功导入微信支付SDK

APP_ID-----公众账号ID
partnerId---商户号
API_KEY----商户秘钥

1-1、在AppDelegate设置相应的函数

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//向微信注册
[WXApiregisterApp:@"wxd930ea5d5a258f4f"withDescription:@"demo 2.0"];
returnYES;
}
//授权后回调WXApiDelegate
-(void)onResp:(BaseReq*)resp{
if([respisKindOfClass:[PayRespclass]]){//写个通知、告诉之前微信支付所在的页面响应相应的动作
NSString*paySuccessWeixinMsg=nil;
paySuccessWeixinMsg=[[NSStringalloc]initWithFormat:@"%d",aresp.errCode];
[[NSNotificationCenterdefaultCenter]postNotificationName:@"PayStateWeixin"object:paySuccessWeixinMsg];
}
}
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
return[WXApihandleOpenURL:urldelegate:[WXApiManagersharedManager]];
}

//返回app的回调
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
return[WXApihandleOpenURL:urldelegate:[WXApiManagersharedManager]];
}

微信支付跟支付宝支付还不太一样,在调起手机微信客户端支付的时得先获取prepay_id,这一步也就是官方文档说的统一下单API。统一下单API在我们的服务器端完成,通过接口返回prepay_id。接下来就是调用手机端微信支付了以下是VC中调用支付的关键代码:发起成功后就会调AppDelegate中的-(void)onResp:(BaseReq*)resp;

-(void)weiXinPayAction:(NSString*)out_trade_noStr prepay_id:(NSString*)prepay_idStr{
payRequsestHandler*handler=[[payRequsestHandleralloc]init];
intrandomX =arc4random() %10000;
NSDate* date = [NSDatedateWithTimeIntervalSinceNow:0];
inttime_stamp=(int)[datetimeIntervalSince1970];
PayReq*request=[[PayReqalloc]init];
request.openID=APP_ID;//公众账号ID
request.partnerId=partnerId;//商户号
request.prepayId=prepay_idStr;//(接口返回--预支付交易会话ID)
request.package=@"Sign=WXPay";//(固定写法)
request.nonceStr=[WXUtilmd5:[[NSStringalloc]initWithFormat:@"%d",randomX]];//随机字符串
request.timeStamp= time_stamp;//时间戳
NSDictionary*parameters =@{@"appid":APP_ID,@"partnerid":partnerId,@"prepayid":prepay_idStr,@"package":@"Sign=WXPay",@"noncestr":request.nonceStr,@"timestamp":[[NSStringalloc]initWithFormat:@"%d",time_stamp]};
NSMutableDictionary*mutableDic=[[NSMutableDictionaryalloc]initWithDictionary:parameters];
[handlersetKey:API_KEY];
request.sign=[handlercreateMd5Sign:mutableDic];
[WXApisendReq:request];
}

以上是实现微信支付需要在app端添加的代码。

2、支付宝支付(官方文档:http://open.alipay.com/platform/document.htm#down)

应用已在支付宝开放平台审核通过,获取相应的参数。项目中成功导入支付宝SDK结合官方Demo

PARTNER---合作身份者ID--商家账户中找到!以2088开头

SELLER-----支付宝收款账号

PRIVATEKEY-商户方的私钥,pkcs8格式

在AppDelegate中添加该方法

- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDKdefaultService]processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary*resultDic) {
NSLog(@"result = %@",resultDic);
}];
returnYES;
}

在支付的VC页面,调用我们自己的服务器后台接口获取tradeNO(订单ID由商家自行制定),然后初始化订单对象,并赋予相应的值

-(void)zhiFuBaoAction:(NSString*)out_trade_no{
//生成订单信息及签名,将商品信息赋予AlixPayOrder的成员变量
Order*order = [[Orderalloc]init];
order.partner=PARTNER;//合作身份者ID
order.seller=SELLER;//支付宝收款账号
order.tradeNO= out_trade_no;//订单ID(由商家自行制定)
order.productName=@"商品标题";//商品标题
order.productDescription=@"商品描述";//商品描述
order.amount= [NSStringstringWithFormat:@"%.2f",[moneyTF.textfloatValue]];//商品价格
order.notifyURL=[[NSStringalloc]initWithFormat:@"%@/pay/alipay/receive_notify",SERVERURL];//回调URL
order.service=@"mobile.securitypay.pay";
order.paymentType=@"1";
order.inputCharset=@"utf-8";
order.itBPay=@"30m";
order.showUrl=@"m.alipay.com";
//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
NSString*appScheme =@"Yunshu";
//将商品信息拼接成字符串
NSString*orderSpec = [orderdescription];
NSLog(@"orderSpec = %@",orderSpec);
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id signer =CreateRSADataSigner(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) {
NSLog(@"reslut = %@",resultDic);
NSString*memoStr=[resultDicobjectForKey:@"memo"];
NSString*resultStatus=[resultDicobjectForKey:@"resultStatus"];
if([resultStatusisEqualToString:@"9000"]){
PaySuccesViewController*paySuccessVC=[[PaySuccesViewControlleralloc]initWithMoney:moneyTF.textpayStyle:@"支付宝"out_trade_noStr:nil];
[self.navigationControllerpushViewController:paySuccessVCanimated:YES];
//发一个通知,提醒支付成功,刷新列表
[[NSNotificationCenterdefaultCenter]postNotificationName:@"RefreshCallsList"object:nil];
}elseif([resultStatusisEqualToString:@"4000"]) {
[HUDCommonViewHUDErrorWithHudSuperView:self.viewinfoStr:memoStr];
//self.navView.titleLab.text=@"充值失败";
}elseif([resultStatusisEqualToString:@"6001"]) {
[HUDCommonViewHUDErrorWithHudSuperView:self.viewinfoStr:memoStr];
//self.navView.titleLab.text=@"充值失败";
}elseif([resultStatusisEqualToString:@"6002"]) {
[HUDCommonViewHUDErrorWithHudSuperView:self.viewinfoStr:memoStr];
//self.navView.titleLab.text=@"充值失败";
}}];}}}

以上是实现支付宝支付功能在app端需要添加的代码.






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值