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

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

  1. 第一步、创建两个Project,分别是电商的App和支付宝的App这里写图片描述

  2. 第二步、在电商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中的配置路径为:
URL Type路径

然后再配置这两个参数:
这里写图片描述

  1. 在支付宝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.整个数据的传递其实就是应用之间的跳转,弄清楚这些基本概念,学起来思路就会清晰一些。附上一个概念图:
这里写图片描述

未完待续,@感谢大家的支持!谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值