iOS开发实践iOS开发iOS开发专区

支付宝支付

2015-12-27  本文已影响2190人  芝麻绿豆

支付宝集成的介绍

第三方支付平台,支付宝是用户将钱付款给支付宝,之后支付宝将钱转入我们自己的账户。

集成支付宝的步骤

集成的流程图(官方)

下载SDK

官网地址:https://www.alipay.com

根据dome配置开发环境:(微信提供了一个公共的测试账号,支付宝没有)


支付完成之后跳转的APP

集成流程

    // 1.获取签约之后的三个数据
    NSString *partner = @"";
    NSString *seller = @"";
    NSString *privateKey = @"";
    
    // 2.生成订单和签名
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = nil; //订单ID(由商家自行制定)
    order.productName = product.name; //商品标题
    order.productDescription = product.detail; //商品描述
    order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
    
    // 服务器的回调地址
    order.notifyURL =  @"http://www.xxx.com"; // 用户支付成功后,支付宝服务器会同步通知我们的服务器.我们服务器需要有一个可以回调的地址
    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 = @"alipay";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
    
    // 跳转到支付宝进行支付
    [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
    }];
  -(BOOL)application:(UIApplication *)application  openURL:(NSURL *)url  sourceApplication:(NSString *)sourceApplication  annotation:(id)annotation {
    
    //跳转支付宝钱包进行支付,处理支付结果
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        NSLog(@"result = %@",resultDic);
    }];
    
    return YES;
}
上一篇下一篇

猜你喜欢

热点阅读