iOS對(duì)接微信、支付寶支付、App內(nèi)購(gòu)流程

一:支付寶支付

1:簽約成為支付寶商戶(hù)獲取“私鑰(PID)”和“秘鑰”

簽約時(shí)一定要有公司營(yíng)業(yè)執(zhí)照 簽約步驟

2:App支付客戶(hù)端DEMO&SDK

移動(dòng)端SDK下載

3:集成支付寶SDK步驟iOS集成流程

4:支付寶支付回調(diào)

注意: 一定不能以客戶(hù)端返回作為用戶(hù)支付的結(jié)果,應(yīng)以服務(wù)器端的接收的支付通知或查詢(xún)API返回的結(jié)果為準(zhǔn)。

/**
支付寶支付完成之后跳轉(zhuǎn)回來(lái)
 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //如果極簡(jiǎn) SDK 不可用,會(huì)跳轉(zhuǎn)支付寶錢(qián)包進(jìn)行支付,需要將支付寶錢(qián)包的支付結(jié)果回傳給 SDK
    if ([url.host isEqualToString:@"safepay"]) {
        //跳轉(zhuǎn)支付寶錢(qián)包進(jìn)行支付,處理支付結(jié)果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"支付寶客戶(hù)端支付結(jié)果result = %@",resultDic);
            if (resultDic && [resultDic objectForKey:@"resultStatus"] && ([[resultDic objectForKey:@"resultStatus"] intValue] == 9000)) {
                 
                // 發(fā)通知帶出支付成功結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnSucceedPayNotification object:resultDic];
            } else {
                 
                // 發(fā)通知帶出支付失敗結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnFailedPayNotification object:resultDic];
            }
             
        }];
    }
     
    if ([url.host isEqualToString:@"platformapi"]){//支付寶錢(qián)包快登授權(quán)返回 authCode
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"支付寶網(wǎng)頁(yè)版result = %@",resultDic);
            if (resultDic && [resultDic objectForKey:@"resultStatus"] && ([[resultDic objectForKey:@"resultStatus"] intValue] == 9000)) {
                 
                // 發(fā)通知帶出支付成功結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnSucceedPayNotification object:resultDic];
            } else {
                 
                // 發(fā)通知帶出支付失敗結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnFailedPayNotification object:resultDic];
            }
        }];
    }
     
    return YES;
}
 
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
     
    //如果極簡(jiǎn) SDK 不可用,會(huì)跳轉(zhuǎn)支付寶錢(qián)包進(jìn)行支付,需要將支付寶錢(qián)包的支付結(jié)果回傳給 SDK
    if ([url.host isEqualToString:@"safepay"]) {
        //跳轉(zhuǎn)支付寶錢(qián)包進(jìn)行支付,處理支付結(jié)果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"支付寶客戶(hù)端支付結(jié)果result = %@",resultDic);
            if (resultDic && [resultDic objectForKey:@"resultStatus"] && ([[resultDic objectForKey:@"resultStatus"] intValue] == 9000)) {
                 
                // 發(fā)通知帶出支付成功結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnSucceedPayNotification object:resultDic];
            } else {
                 
                // 發(fā)通知帶出支付失敗結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnFailedPayNotification object:resultDic];
            }
        }];
    }
     
    if ([url.host isEqualToString:@"platformapi"]){//支付寶錢(qián)包快登授權(quán)返回 authCode
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"支付寶網(wǎng)頁(yè)版result = %@",resultDic);
            if (resultDic && [resultDic objectForKey:@"resultStatus"] && ([[resultDic objectForKey:@"resultStatus"] intValue] == 9000)) {
                 
                // 發(fā)通知帶出支付成功結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnSucceedPayNotification object:resultDic];
            } else {
                 
                // 發(fā)通知帶出支付失敗結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLAliReturnFailedPayNotification object:resultDic];
            }
        }];
    }
     
    return YES;
}

5:補(bǔ)充

..........

二:微信支付

1:微信商戶(hù)申請(qǐng)步驟

商戶(hù)在微信公眾平臺(tái)或開(kāi)放平臺(tái)提交微信支付申請(qǐng),微信支付工作人員審核資料無(wú)誤后開(kāi)通相應(yīng)的微信支付權(quán)限。微信支付申請(qǐng)審核通過(guò)后,商戶(hù)在申請(qǐng)資料填寫(xiě)的郵箱中收取到由微信支付小助手發(fā)送的郵件,此郵件包含開(kāi)發(fā)時(shí)需要使用的支付賬戶(hù)信息

2:資源下載微信SDK及demo下載

3:iOS接入指南

指南APP端開(kāi)發(fā)步驟
IOS開(kāi)發(fā)要點(diǎn)說(shuō)明

4:支付結(jié)果回調(diào)

照微信SDK Sample,在類(lèi)實(shí)現(xiàn)onResp函數(shù),支付完成后,微信APP會(huì)返回到商戶(hù)APP并回調(diào)onResp函數(shù),開(kāi)發(fā)者需要在該函數(shù)中接收通知,判斷返回錯(cuò)誤碼,如果支付成功則去后臺(tái)查詢(xún)支付結(jié)果再展示用戶(hù)實(shí)際支付結(jié)果。

注意: 一定不能以客戶(hù)端返回作為用戶(hù)支付的結(jié)果,應(yīng)以服務(wù)器端的接收的支付通知或查詢(xún)API返回的結(jié)果為準(zhǔn)。

// 支付返回結(jié)果,實(shí)際支付結(jié)果需要去微信服務(wù)器端查詢(xún)
-(void)onResp:(BaseResp *)resp {
   if([resp isKindOfClass:[PayResp class]]){
      switch (resp.errCode) {
          case WXSuccess:{
             NSlog(@"支付成功");
            // 發(fā)通知帶出支付成功結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLWXReturnSucceedPayNotification object:resp];
          }
            break;
        default:{
            NSlog(@“支付失敗:%d”,resp.errCode);
             // 發(fā)通知帶出支付失敗結(jié)果
                [[NSNotificationCenter defaultCenter] postNotificationName:ZLWXReturnFailedPayNotification object:resp];
        }
          break;
    }
  }  
}

5補(bǔ)充

......

三:App內(nèi)購(gòu)

1:開(kāi)啟協(xié)議稅務(wù)銀行步驟AppstoreConnect后臺(tái)

AppConnect后臺(tái)創(chuàng)建內(nèi)購(gòu)項(xiàng)目
鏈接文章圖解1
鏈接文章圖解2

2:iOS代碼集成步驟

1:導(dǎo)入StoreKit.framework

.h.m代碼

#import <StoreKit/StoreKit.h>

enum{
IAP0p20=20,
IAP1p100,
IAP4p600,
IAP9p1000,
IAP24p6000,
}buyCoinsTag;

//代理
@interface RechargeVC : UIViewController <SKPaymentTransactionObserver,SKProductsRequestDelegate >

{
int buyType;
}

- (void) requestProUpgradeProductData;

-(void)RequestProductData;

-(void)buy:(int)type;

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;

-(void) PurchasedTransaction: (SKPaymentTransaction *)transaction;

- (void) completeTransaction: (SKPaymentTransaction *)transaction;

- (void) failedTransaction: (SKPaymentTransaction *)transaction;

-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction;

-(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error;

- (void) restoreTransaction: (SKPaymentTransaction *)transaction;

-(void)provideContent:(NSString *)product;

-(void)recordTransaction:(NSString *)product;

@end
#import "RechargeVC.h"

//在內(nèi)購(gòu)項(xiàng)目中創(chuàng)的商品單號(hào)
#define ProductID_IAP0p20 @"Nada.JPYF01"http://20
#define ProductID_IAP1p100 @"Nada.JPYF02" //100
#define ProductID_IAP4p600 @"Nada.JPYF03" //600
#define ProductID_IAP9p1000 @"Nada.JPYF04" //1000
#define ProductID_IAP24p6000 @"Nada.JPYF05" //6000

@interface RechargeVC ()

@end

@implementation RechargeVC

- (void)viewDidLoad {

    [super viewDidLoad];

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [self buy:IAP0p20];

}

-(void)buy:(int)type
{
    buyType = type;
    if ([SKPaymentQueue canMakePayments]) {
    [self RequestProductData];
    NSLog(@"允許程序內(nèi)付費(fèi)購(gòu)買(mǎi)");
}
else
{
    NSLog(@"不允許程序內(nèi)付費(fèi)購(gòu)買(mǎi)");
    UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"提示"
    message:@"您的手機(jī)沒(méi)有打開(kāi)程序內(nèi)付費(fèi)購(gòu)買(mǎi)"
    delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];

    [alerView show];

    }
}

-(void)RequestProductData
{
    NSLog(@"---------請(qǐng)求對(duì)應(yīng)的產(chǎn)品信息------------");
    NSArray *product = nil;
    switch (buyType) {
        case IAP0p20:
        product=[[NSArray alloc] initWithObjects:ProductID_IAP0p20,nil];
        break;
        case IAP1p100:
        product=[[NSArray alloc] initWithObjects:ProductID_IAP1p100,nil];
        break;
        case IAP4p600:
        product=[[NSArray alloc] initWithObjects:ProductID_IAP4p600,nil];
        break;
        case IAP9p1000:
        product=[[NSArray alloc] initWithObjects:ProductID_IAP9p1000,nil];
        break;
        case IAP24p6000:
        product=[[NSArray alloc] initWithObjects:ProductID_IAP24p6000,nil];
        break;

        default:
        break;
}
    NSSet *nsset = [NSSet setWithArray:product];
    SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
    request.delegate=self;
    [request start];

}

//<SKProductsRequestDelegate> 請(qǐng)求協(xié)議
//收到的產(chǎn)品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{

    NSLog(@"-----------收到產(chǎn)品反饋信息--------------");
    NSArray *myProduct = response.products;
    NSLog(@"產(chǎn)品Product ID:%@",response.invalidProductIdentifiers);
    NSLog(@"產(chǎn)品付費(fèi)數(shù)量: %d", (int)[myProduct count]);
    // populate UI
    for(SKProduct *product in myProduct){
    NSLog(@"product info");
    NSLog(@"SKProduct 描述信息%@", [product description]);
    NSLog(@"產(chǎn)品標(biāo)題 %@" , product.localizedTitle);
    NSLog(@"產(chǎn)品描述信息: %@" , product.localizedDescription);
    NSLog(@"價(jià)格: %@" , product.price);
    NSLog(@"Product id: %@" , product.productIdentifier);
}
    SKPayment *payment = nil;
    switch (buyType) {
        case IAP0p20:
        payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP0p20];    //支付25
        break;
        case IAP1p100:
        payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP1p100];    //支付108
        break;
        case IAP4p600:
        payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP4p600];    //支付618
        break;
        case IAP9p1000:
        payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP9p1000];    //支付1048
        break;
        case IAP24p6000:
        payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP24p6000];    //支付5898
        break;
        default:
        break;
}
    NSLog(@"---------發(fā)送購(gòu)買(mǎi)請(qǐng)求------------");
    [[SKPaymentQueue defaultQueue] addPayment:payment];

}
- (void)requestProUpgradeProductData
{
    NSLog(@"------請(qǐng)求升級(jí)數(shù)據(jù)---------");
    NSSet *productIdentifiers = [NSSet setWithObject:@"com.productid"];
    SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];

}
//彈出錯(cuò)誤信息
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
    NSLog(@"-------彈出錯(cuò)誤信息----------");
    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(@"----------反饋信息結(jié)束--------------");

}

-(void) PurchasedTransaction: (SKPaymentTransaction *)transaction{
    NSLog(@"-----PurchasedTransaction----");
    NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
    [self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
}

//<SKPaymentTransactionObserver> 千萬(wàn)不要忘記綁定,代碼如下:
//----監(jiān)聽(tīng)購(gòu)買(mǎi)結(jié)果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易結(jié)果
{
    NSLog(@"-----paymentQueue--------");
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:{//交易完成
            [self completeTransaction:transaction];
            NSLog(@"-----交易完成 --------");

            UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@""
            message:@"購(gòu)買(mǎi)成功"
            delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];

            [alerView show];

        } break;
        case SKPaymentTransactionStateFailed://交易失敗
            { [self failedTransaction:transaction];
            NSLog(@"-----交易失敗 --------");
            UIAlertView *alerView2 =  [[UIAlertView alloc] initWithTitle:@"提示"
            message:@"購(gòu)買(mǎi)失敗,請(qǐng)重新嘗試購(gòu)買(mǎi)"
            delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];

            [alerView2 show];

        }break;
        case SKPaymentTransactionStateRestored://已經(jīng)購(gòu)買(mǎi)過(guò)該商品
            [self restoreTransaction:transaction];
            NSLog(@"-----已經(jīng)購(gòu)買(mǎi)過(guò)該商品 --------");
            case SKPaymentTransactionStatePurchasing:      //商品添加進(jìn)列表
            NSLog(@"-----商品添加進(jìn)列表 --------");
            break;
            default:
            break;
        }
    }
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction

{
    NSLog(@"-----completeTransaction--------");
    // Your application should implement these two methods.
    NSString *product = transaction.payment.productIdentifier;
    if ([product length] > 0) {

    NSArray *tt = [product componentsSeparatedByString:@"."];
    NSString *bookid = [tt lastObject];
    if ([bookid length] > 0) {
    [self recordTransaction:bookid];
    [self provideContent:bookid];
}
}

    // Remove the transaction from the payment queue.

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

//記錄交易
-(void)recordTransaction:(NSString *)product{
    NSLog(@"-----記錄交易--------");
}

//處理下載內(nèi)容
-(void)provideContent:(NSString *)product{
    NSLog(@"-----下載--------");
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction{
    NSLog(@"失敗");
    if (transaction.error.code != SKErrorPaymentCancelled)
    {

    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}
-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction{

}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@" 交易恢復(fù)處理");

}

-(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error{
    NSLog(@"-------paymentQueue----");
}

#pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"%@",  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    switch([(NSHTTPURLResponse *)response statusCode]) {
        case 200:
        case 206:
        break;
        case 304:
        break;
        case 400:
        break;
        case 404:
        break;
        case 416:
        break;
        case 403:
        break;
        case 401:
        case 500:
        break;
        default:
        break;
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"test");
}

-(void)dealloc
{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];//解除監(jiān)聽(tīng)

}

@end

3:測(cè)試及遇到的問(wèn)題

去AppConnect后臺(tái)添加測(cè)試賬號(hào)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。