关闭

支付宝集成

128人阅读 评论(0) 收藏 举报
分类:

注意:在给支付宝传订单金额的时候,最小要精确到分,也就是0.01。如果小于0.01,跳转到支付宝后会提示:交易订单处理失败,请稍后再试。

一、下载支付宝SDK,导入到项目中:

1、文件路径问题:

#include <openssl/asn1.h> not fond 问题:

target ---> buildsetting ---> header search path ---> 添加:$(SRCROOT)/项目名中间文件夹名/Alipay

2、混编问题:

target ---> buildsetting ---> Apple LLVM 7.0 - Language ---> Compile Sources As --->Objective-C

二、在Info.plist文件中添加:

1、在target中info下的URL Types下添加:


2、LSApplicationQueriesSchemes

三、相关代码:

0、Appdelegate.m中配置:

// 当通过别的应用程序,将该应用程序打开时,会调用该方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    // 当用户通过支付宝客户端进行支付时,会回调该block:standbyCallback
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        NSLog(@"result = %@",resultDic);
    }];
    
    return YES;
}

1、Product.h

#import <Foundation/Foundation.h>

@interface Product : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *detail;
@property (nonatomic, assign) CGFloat price;

@end

2.1、Order.h

#import <Foundation/Foundation.h>

@interface Order : NSObject

@property (nonatomic, copy) NSString *partner;
@property (nonatomic, copy) NSString *seller;
@property (nonatomic, copy) NSString *productName;
@property (nonatomic, copy) NSString *productDescription;
@property (nonatomic, copy) NSString *amount;
@property (nonatomic, copy) NSString *service;
@property (nonatomic, copy) NSString *paymentType;
@property (nonatomic, copy) NSString *inputCharset;
@property (nonatomic, copy) NSString *itBPay;
@property (nonatomic, copy) NSString *showUrl;
@property (nonatomic, copy) NSString *notifyURL;
@property (nonatomic, copy) NSString *tradeNO;

@property(nonatomic, copy) NSString * rsaDate;
@property(nonatomic, copy) NSString * appID;

@property(nonatomic, readonly) NSMutableDictionary * extraParams;
@end
2.2、Order.m

#import "Order.h"

@implementation Order


- (NSString *)description
{
    NSMutableString * discription = [NSMutableString string];
    if (self.partner) {
        [discription appendFormat:@"partner=\"%@\"", self.partner];
    }
    
    if (self.seller) {
        [discription appendFormat:@"&seller_id=\"%@\"", self.seller];
    }
    if (self.tradeNO) {
        [discription appendFormat:@"&out_trade_no=\"%@\"", self.tradeNO];
    }
    if (self.productName) {
        [discription appendFormat:@"&subject=\"%@\"", self.productName];
    }
    
    if (self.productDescription) {
        [discription appendFormat:@"&body=\"%@\"", self.productDescription];
    }
    if (self.amount) {
        [discription appendFormat:@"&total_fee=\"%@\"", self.amount];
    }
    if (self.notifyURL) {
        [discription appendFormat:@"¬ify_url=\"%@\"", self.notifyURL];
    }
    
    if (self.service) {
        [discription appendFormat:@"&service=\"%@\"",self.service];//mobile.securitypay.pay
    }
    if (self.paymentType) {
        [discription appendFormat:@"&payment_type=\"%@\"",self.paymentType];//1
    }
    
    if (self.inputCharset) {
        [discription appendFormat:@"&_input_charset=\"%@\"",self.inputCharset];//utf-8
    }
    if (self.itBPay) {
        [discription appendFormat:@"&it_b_pay=\"%@\"",self.itBPay];//30m
    }
    if (self.showUrl) {
        [discription appendFormat:@"&show_url=\"%@\"",self.showUrl];//m.alipay.com
    }
    if (self.rsaDate) {
        [discription appendFormat:@"&sign_date=\"%@\"",self.rsaDate];
    }
    if (self.appID) {
        [discription appendFormat:@"&app_id=\"%@\"",self.appID];
    }
    for (NSString * key in [self.extraParams allKeys]) {
        [discription appendFormat:@"&%@=\"%@\"", key, [self.extraParams objectForKey:key]];
    }
    return discription;
}

@end


3、支付宝调用:

需导入头文件:
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    Product *product = [[Product alloc] init];
    product.title = @"商品名称";
    product.detail = @"商品描述";
    product.price = 0.1;
    [self buyProduct:product];
}

- (void)buyProduct:(Product *)product
{
    // 1.添加写partner/seller/privateKey,签约后获得
    NSString *partner = @"";
    NSString *seller = @"";
    NSString *privateKey = @""; // 不能有空格或换行
    
    // 2.生成订单
    // 2.1.创建订单
    Order *order = [[Order alloc] init];
    
    // 2.2.设置商户ID/账号ID
    order.partner = partner;
    order.seller = seller;
    
    // 2.3.设置订单号
    order.tradeNO = @"订单号";
    
    // 2.4.设置产品相关的信息
    order.productName = product.title;
    order.productDescription = product.detail;
    order.amount = [NSString stringWithFormat:@"%.2f", product.price];
    
    // 2.5.设置订单常量
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    // 2.6.回调URL(异步通知服务器的地址)
    order.notifyURL =  @""; //回调URL
    
    // 2.7.将订单信息拼接成字符串
    NSString *orderSpec = [order description];// 重写Order类的description方法
    
    // 3.对订单进行签名加密
    // 3.1.对订单进行加密
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    // 3.2.将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"", orderSpec, signedString, @"RSA"];
    
    // 4.打开支付宝客户端进行支付(用户没有安装支付宝客户端,直接在应用程序中添加一个WebView,通过网页让用户进行支付)
    // 注意:如果是通过网页支付完成,那么会回调该block:callback
    [[AlipaySDK defaultService] payOrder:orderString fromScheme:@"alipayschemes" callback:^(NSDictionary *resultDic) {
        
        NSLog(@"resultDic = %@",resultDic);
        
    }];
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15692次
    • 积分:640
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类