iOS之支付

作者: 请输入账号名 | 来源:发表于2016-07-21 11:05 被阅读2848次

iOS支付

iOS支付分为两类,第三方支付应用内支付(内购)

第三方支付包括:支付宝支付、微信支付、银联支付、百度钱包、京东支付等等。

应用内支付(In-App Purchase):在应用程序内购买虚拟商品。如果你在App Store上销售的应用程序,将收到支付金额的70%。

第三方支付

弹出方式

网页

有些第三方支付没有安装客户端,可以直接弹出网页进行支付。(比如支付宝)

调用APP

手机中安装了客户端可以跳转到APP中进行支付。微信支付只能调用App进行支付。

支付宝支付

相关资料

支付流程

  1. 在商户服务平台先与支付宝签约,获得商户ID(partner)和账号ID(seller),需要提供公司资质或者营业执照,个人无法申请。

    文档地址:
    https://doc.open.alipay.com/doc2/detail?treeId=58&articleId=103542&docType=1

  2. 生成并下载相应的公钥私钥文件(加密签名用)

    文档地址:
    https://doc.open.alipay.com/doc2/detail.htm?spm=0.0.0.0.POMYKl&treeId=58&articleId=103543&docType=1

  3. 下载支付宝SDK:
    https://doc.open.alipay.com/doc2/detail?0treeId=54&articleId=103419&docType=1

  4. 生成订单信息

  5. 调用支付宝客户端,由支付宝客户端跟支付宝安全服务器打交道

  6. 支付完毕后返回支付结果给商户客户端和服务器

SDK里有集成支付宝功能的一个Demo,集成支付功能的具体操作方式,可以参考Demo。

代码集成流程

参考文档地址:
https://doc.open.alipay.com/doc2/detail.htm?spm=0.0.0.0.efmKDS&treeId=59&articleId=103676&docType=1

  1. 下载官方SDK

    下载地址:
    https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1

    本Demo使用的SDK是从官方Demo整理出来的,整理的SDK版本:201501022。

    下载地址:http://7xooko.com1.z0.glb.clouddn.com/AlipaySDK.zip

    目录结构如下:

    ├── AlipaySDK.bundle
    ├── AlipaySDK.framework
    ├── Order.h
    ├── Order.m
    ├── Util
    ├── libcrypto.a
    ├── libssl.a
    └── openssl
    

    其中:

    • AlipaySDK.bundleAlipaySDK.framework是支付宝SDK
    • Order类:定义订单信息
    • Util、libcrypto.a、libssl.a、openssl:数据签名,对订单信息进行加密
  2. 添加依赖库

  3. 用该App ID创建一个新的应用。

  4. 创建应用内付费项目,选择付费类型。

    App 内购买项目摘要填写

主要代码实现

  1. 在工程中引入 StoreKit.framework#import <StoreKit/StoreKit.h>

  2. 获得所有的付费Product ID列表。这个可以用常量存储在本地,也可以由自己的服务器返回。

    //在内购项目中创建的商品单号
    #define ProductID_IAP_FTHJ @"com.1000phone.IAPDemo.fthj_purple" // 方天画戟 488元
    #define ProductID_IAP_XYJ @"com.1000phone.IAPDemo.xyj" // 轩辕剑 6,498元
    #define ProductID_IAP_JB @"com.1000phone.IAPDemo.jb" // 金币 6元=6金币
    

  3. 制作界面,展示所有的应用内付费项目。这些应用内付费项目的价格和介绍信息可以从App Store服务器请求,也可以是自己的服务器返回。向App Store查询速度非常慢,通常需要2-3秒钟,最好从服务器请求。

    - (void)createViews
    {
        NSArray * buttonNames = @[@"轩辕剑 6498元", @"方天画戟 488元", @"金币6元=6金币"];
        __weak typeof(self) weakSelf = self;
        [buttonNames enumerateObjectsUsingBlock:^(NSString * buttonName, NSUInteger idx, BOOL * stop) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
            [weakSelf.view addSubview:button];
            button.frame = CGRectMake(100, 100 + idx   * 60, 150, 50);
            button.titleLabel.font = [UIFont systemFontOfSize:18];
            [button setTitle:buttonName forState:UIControlStateNormal];
    
            // 设置tag值
            button.tag = PAY_BUTTON_BEGIN_TAG + idx;
            [button addTarget:self action:@selector(buyProduct:) forControlEvents:UIControlEventTouchUpInside];
        }];
    }
    
    - (void)buyProduct:(UIButton *) sender
    {
    
    }
    

  4. 当用户点击了一个IAP项目,我们先查询用户是否允许应用内付费。

    - (void)buyProduct:(UIButton *) sender
    {
        self.buyType = sender.tag - PAY_BUTTON_BEGIN_TAG;
        if ([SKPaymentQueue canMakePayments]) {
            // 执行下面提到的第5步:
            [self requestProductData];
            NSLog(@"允许程序内付费购买");
        }
        else
        {
            NSLog(@"不允许程序内付费购买");
            UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"提示"
                                                                message:@"您的手机没有打开程序内付费购买"
                                                               delegate:nil cancelButtonTitle:NSLocalizedString(@"关闭",nil) otherButtonTitles:nil];
    
            [alerView show];
    
        }
    }
    
  5. 我们先通过该IAP的ProductID向AppStore查询,获得SKPayment实例,然后通过SKPaymentQueue的 addPayment方法发起一个购买的操作。

    // 下面的ProductId应该是事先在itunesConnect中添加好的,已存在的付费项目。否则查询会失败。
    - (void)requestProductData {
       NSLog(@"---------请求对应的产品信息------------");
       NSArray *product = nil;
       switch (self.buyType) {
           case 0:
               product = [NSArray arrayWithObject:ProductID_IAP_XYJ];
               break;
           case 1:
               product = [NSArray arrayWithObject:ProductID_IAP_FTHJ];
               break;
           case 2:
               product = [NSArray arrayWithObject:ProductID_IAP_JB];
               break;
       }
       NSSet *nsset = [NSSet setWithArray:product];
       SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
       request.delegate=self;
       [request start];
    }
    
    #pragma mark - SKProductsRequestDelegate
    // 收到的产品信息回调
    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    
       NSLog(@"-----------收到产品反馈信息--------------");
       NSArray *myProduct = response.products;
       if (myProduct.count == 0) {
           NSLog(@"无法获取产品信息,购买失败。");
           return;
       }
       NSLog(@"产品Product ID:%@",response.invalidProductIdentifiers);
       NSLog(@"产品付费数量: %d", (int)[myProduct count]);
       // populate UI
       for(SKProduct *product in myProduct){
           NSLog(@"product info");
           NSLog(@"SKProduct 描述信息%@", [product description]);
           NSLog(@"产品标题 %@" , product.localizedTitle);
           NSLog(@"产品描述信息: %@" , product.localizedDescription);
           NSLog(@"价格: %@" , product.price);
           NSLog(@"Product id: %@" , product.productIdentifier);
       }
       SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
       NSLog(@"---------发送购买请求------------");
       [[SKPaymentQueue defaultQueue] addPayment:payment];
    
    }
    
    //弹出错误信息
    - (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
       NSLog(@"-------弹出错误信息----------");
       UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:[error localizedDescription]
                                                          delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
       [alerView show];
    
    }
    
    -(void) requestDidFinish:(SKRequest *)request
    {
       NSLog(@"----------反馈信息结束--------------");
    
    }
    
  6. 在viewDidLoad方法中,将购买页面设置成购买的Observer。

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self createViews];
        // 监听购买结果
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    }
    
    - (void)dealloc
    {
        [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    }
    
  7. 当用户购买的操作有结果时,就会触发下面的回调函数,相应进行处理即可。

    #pragma mark - SKPaymentTransactionObserver
    // 处理交易结果
    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
        for (SKPaymentTransaction *transaction in transactions)
        {
            switch (transaction.transactionState)
            {
                case SKPaymentTransactionStatePurchased://交易完成
                    NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
                    [self completeTransaction:transaction];
                    break;
                case SKPaymentTransactionStateFailed://交易失败
                    [self failedTransaction:transaction];
                    break;
                case SKPaymentTransactionStateRestored://已经购买过该商品
                    [self restoreTransaction:transaction];
                    break;
                case SKPaymentTransactionStatePurchasing:      //商品添加进列表
                    NSLog(@"商品添加进列表");
                    break;
                default:
                    break;
            }
        }
    
    }
    
    // 交易完成
    - (void)completeTransaction:(SKPaymentTransaction *)transaction {
        NSString * productIdentifier = transaction.payment.productIdentifier;
    //    NSString * receipt = [transaction.transactionReceipt base64EncodedString];
        if ([productIdentifier length] > 0) {
            // 向自己的服务器验证购买凭证
        }
    
        // Remove the transaction from the payment queue.
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    
    }
    
    // 交易失败
    - (void)failedTransaction:(SKPaymentTransaction *)transaction {
        if(transaction.error.code != SKErrorPaymentCancelled) {
            NSLog(@"购买失败");
        } else {
            NSLog(@"用户取消交易");
        }
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    }
    
    // 已购商品
    - (void)restoreTransaction:(SKPaymentTransaction *)transaction {
        // 对于已购商品,处理恢复购买的逻辑
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    }
    
  8. 服务器验证凭证(Optional)。如果购买成功,我们需要将凭证发送到服务器上进行验证。考虑到网络异常情况,iOS端的发送凭证操作应该进行持久化,如果程序退出,崩溃或网络异常,可以恢复重试。

参考链接

  1. iOS开发内购全套图文教程
  2. iOS应用内付费(IAP)开发步骤列表
  3. iOS内购实现及测试Check List

苹果支付( Pay)

苹果支付是一种在应用内运行的具有隐秘性和安全性非接触式的支付方式。它允许触摸付款,你可以用来购买实体商品和服务。

Apple 不会存储或共享客户的实际信用卡和借记卡卡号,因此商家和 App 开发者无需负责管理和保护实际的信用卡和借记卡卡号。

先决条件

除了使用 PassKit 框架实施 Apple Pay 之外,您还必须:

参考资料

  1. 官方Pay教程
  2. Apple Pay 中文入门

 Pay VS In-App Purchase

 Pay In-App Purchase
框架 PassKit StoreKit
适用范围 实体商品(如食品杂货、服装和电器)和服务(如俱乐部会员、酒店预订和活动门票) 销售虚拟商品,如适用于您的 App 的优质内容及订阅数字内容;程序内的内容和功能性;程序内货币服务;数码订阅
支付处理 自己的支付平台处理付款 苹果公司处理付款

相关文章

  • iOS开发支付集成之微信支付

    iOS开发支付集成之微信支付

  • ios-支付集成

    iOS开发之第三方支付支付宝支付教程,史上最新最全第三方支付宝支付方式实现、支付宝集成教程,支付宝实现流程 iOS...

  • 收录 : iOS支付开发

    iOS 银联支付开发流程iOS 微信支付开发流程iOS 支付宝支付开发流程iOS Apple Pay开发流程App...

  • iOS之支付

    iOS支付 iOS支付分为两类,第三方支付和应用内支付(内购)。 第三方支付包括:支付宝支付、微信支付、银联支付、...

  • 如何调试支付宝(iOS)

    如何调试支付宝(iOS)如何调试支付宝(iOS)

  • 如何调试支付宝(iOS)

    如何调试支付宝(iOS)如何调试支付宝(iOS)

  • ios支付宝签名

    支付宝: iOS 支付功能概述 - 简书 1.iOS集成支付宝 - 简书 2.iOS 集成支付宝 - 简书 3.i...

  • 文章汇总

    ios iOS 网络篇 网络基础总结 支付宝集成 iOS支付宝支付集成 - demo例子集 - 博客园 理解 iO...

  • iOS支付的配置

    iOS支付 iOS支付分为两类,第三方支付和应用内支付(内购)。 第三方支付包括:支付宝支付、微信支付、银联支付、...

  • ios支付

    iOS支付 iOS支付分为两类,第三方支付和应用内支付(内购)。 第三方支付包括:支付宝支付、微信支付、银联支付、...

网友评论

  • ad193b2e3cf5:在测试阶段都是没有问题的,审核也通过了。
    但是刚发布的app,会在一段时间内,内购返回的pruduct.count = 0.过几个小时就好了。
    楼主有没有遇到过这样的问题。
    ad193b2e3cf5:@请输入账号名 你们app一发布就可以直接用iap支付吗
    请输入账号名:@ad193b2e3cf5 没遇到这种情况!

本文标题:iOS之支付

本文链接:https://www.haomeiwen.com/subject/eudbjttx.html