您的位置:首页 > 其它

模拟支付宝的交易过程(一)

2015-07-05 21:23 375 查看
如今,大多数公司的App都会涉及到商品的销售与推广,用户在购买相应的产品的时候更多的是选择第三方的平台进行交易。国内用的比较普遍的是阿里的支付宝和腾讯的微信支付等。因此在这里简单学习下手机中的App(支付宝)是如何进行支付的,为以后系统开发做好准备。

第一步、创建两个Project,分别是电商的App和支付宝的App


第二步、在电商App中搭建商品的界面,我是以UITableViewController来作为window的RooterViewController.



这里我以iPhone手机的展示为例,当用户浏览到心仪的手机时,点击相应的cell,页面就会跳转到支付宝对应的界面。在实现页面跳转之前,电商的App需要完成以下具体的操作:

a.订单模型的创建

/*
@property (nonatomic,copy) NSString *amount;//支付金额
@property (nonatomic,copy) NSString *alipayAccount;//: 接收方支付宝帐号
@property (nonatomic,copy) NSString *orderNo;// 定单号
@property (nonatomic,copy) NSString *orderDesc;//定单描述
@property (nonatomic,copy) NSString *tradeType;// 交易类型 1.付款 2.退款
@property (nonatomic,copy) NSString *tradeStatus;//交易状态 0.付款中 1.成功 2.失败
@property (nonatomic,copy) NSString *appScheme;//给支付宝通知结果用
*/


b.UITableView Cell的数据显示

#pragma mark-实现UITableViewController的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return self.products.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID=@"product";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
CZProduct *product=self.products[indexPath.row];
cell.textLabel.text=product.name;
cell.detailTextLabel.text=product.price;
return cell;
}


c.当用户点击的时候,实现UITableViewController的代理方法,来讲商品的信息发送到支付宝的界面上显示出来

#pragma mark-实现UITableViewController的代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CZProduct *product=self.products[indexPath.row];

CZOrder *order=[[CZOrder alloc] init];

order.amount=product.price;
order.alipayAccount=@"xx@qq.com";
order.orderDesc=product.name;

NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
formatter.dateFormat=@"yyyyMMddHHmmss";
order.orderNo=[NSString stringWithFormat:@"CZ%@",[formatter stringFromDate:[NSDate date]]];

order.tradeType=@"1";

order.tradeStatus=@"0";

order.appScheme=@"dianshang://result";

[[CZAlipay sharedAlipay] paymentWithOrder:order result:^(CZOrder *result) {

NSString *message=[[NSString alloc] init];
if ([result.tradeStatus isEqualToString:@"1"])
{
message=[@"恭喜,支付成功" stringByAppendingFormat:@" The product withOrderNo %@",result.orderNo];
}else if(([result.tradeStatus isEqualToString:@"2"]))
{
message=[@"恭喜,支付失败" stringByAppendingFormat:@" The product withOrderNo %@",result.orderNo];
}
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"支付结果" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];

}];
}


d.在电商App跳转到支付宝App的时候,需要告诉电商App跳转的URL,即要在支付宝App中进行配置。在Xcode中的配置路径为:



然后再配置这两个参数:



在支付宝App Xcode StoryBoard创建商品信息和交易金额等数据显示的UIViewController:



4.支付宝App将支付的结果信息传到电商App,前提是需要配置电商App的URL Type.



5.在App间的跳转会调用系统AppDelegate的代理方法:

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSString *urlstring=[url.query stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"支付宝:%@",[urlstring componentsSeparatedByString:@"&"]);

CZOrder *order=[CZOrder orderWithUrlQuery:urlstring];

UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *paymentNav=[storyBoard instantiateViewControllerWithIdentifier:@"paymentNav"];

CZPaymentViewController *paymentVc=(CZPaymentViewController *)paymentNav.topViewController;
paymentVc.order=order;

[self.window.rootViewController presentViewController:paymentNav animated:YES completion:NULL];

return YES;
}


6.整个数据的传递其实就是应用之间的跳转,弄清楚这些基本概念,学起来思路就会清晰一些。附上一个概念图:



未完待续,@感谢大家的支持!谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: