天天看點

ios的Ping++支付接入步驟-b

Ping++ SDK 包括 Server 和 Client 兩部分。

  • 其中 Server SDK 提供了 

    PHP

    Java

    Python

    Node.js

    Ruby

    Go

     六種語言版本。
  • Client SDK 提供了 

    Android

    iOS

    HTML5

     三種平台版本。

應用在接入 Ping++ SDK 時,需要使用以下三個參數,這三個參數你可以在管理平台中擷取:

  1. API Key:API Key 是 Ping++ 配置設定給你的唯一身份辨別。在 Server SDK 的使用過程中需要配置該參數。注冊 Ping++ 賬号并通過稽核後,Ping++ 會配置設定給你兩個 API Key,分别為:

    Test Key

     和 

    Live Key

    。 

    Test Key

    Live Key

     分别表明使用測試模式和真實模式,你可以根據實際應用使用場景分别使用。
  2. 應用 ID:應用 ID 是 Ping++ 配置設定給你的應用的唯一辨別。在 Server SDK 的使用過程中需要配置該參數。
  3. Notify URL:Notify URL 是 Ping++ 系統用來向你的應用背景推送異步通知時使用的位址,該位址必須是一個網際網路可以通路的位址。你可以在 Ping++ 管理平台中對應的應用内進行設定。

一、接入步驟:

  1. 擷取 SDK

    從 Github 下載下傳 SDK, 裡面包含了 lib 檔案夾和 example 檔案夾。lib 檔案夾裡面是 SDK 的檔案,example 檔案夾裡是接入示例代碼

  2. 依賴 Frameworks:

               必需:

    CFNetwork.frameworkSystemConfiguration.frameworkSecurity.frameworklibc++.dylib

               百度錢包所需:

    libz.dyliblibstdc++.dylibCoreTelephony.frameworkAddressBook.frameworkAddressBookUI.frameworkAudioToolbox.frameworkCoreAudio.frameworkCoreGraphics.frameworkImageIO.frameworkMapKit.frameworkMessageUI.frameworkMobileCoreServices.frameworkQuartzCore.framework

  3. 根據所需管道導入相應的庫檔案
  4. 添加 URL Schemes:在 Xcode 中,選擇你的工程設定項,選中 "TARGETS" 一欄,在 "Info" 标簽欄的 "URL Types" 添加 "URL Schemes",如果使用微信,填入所注冊的應用程式 id,如果不使用微信,則自定義,建議起名稍複雜一些,盡量避免與其他程式沖突。
  5. 添加 Other Linker Flags:在 Build Settings 搜尋 Other Linker Flags ,添加 

    -ObjC

二、發起支付:

1. Client 發送支付要素給 Server

使用者選擇管道點選交易按鈕, Client 收集交易所需的相關參數傳遞給 Server (伺服器的位址為代碼中的 URL)。

NSDictionary* dict = @{    @"channel" : channel, // 管道 alipay, wx, upacp, bfb
    @"amount"  : amount   // 金額};NSData* data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];NSString *bodyData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];
[postRequest setHTTPMethod:@"POST"];
[postRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:postRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;    NSString* charge = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    // ...}];      

2. Server 發送支付請求并将傳回的支付憑據傳給 Client

Server 接收并處理 Client 傳過來的資料,使用 Ping++ 提供的方法向 Ping++ 發起交易,并将從 Ping++ 獲得的帶支付憑據的 Charge 對象傳回給 Client。

<?php//引用 SDK 庫檔案require_once('/path/to/Pingpp.php');//擷取用戶端的參數,這裡不能使用 $_POST 接收,是以我們提供了如下的參考方法接收$input_data = json_decode(file_get_contents("php://input"), true);//TODO 客戶在這裡自行處理接收過來的交易所需的資料//設定API KEY,如果是測試模式,這裡填入 Test Key;如果是真實模式, 這裡填入 Live Key。Pingpp::setApiKey("YOUR-KEY");//建立支付對象,發起交易$ch = Pingpp_Charge::create(    //array 裡需要哪些參數請閱讀 API Reference 文檔
    array(        "order_no"  => $orderNo,  //商戶系統自己生成的訂單号
        "app"       => array("id" => "YOUR-APP-ID"),  //Ping++ 配置設定給商戶的應用 ID
        "amount"    => $amount,  //交易金額
        "channel"   => $channel,  //交易管道
        "currency"  => "cny",        "client_ip" => $_SERVER["REMOTE_ADDR"],  //發起交易的用戶端的 IP
        "subject"   => "Your Subject",        "body"      => "Your Body",        "extra"     => null //僅用戶端為 HTML5 時此參數不為空,具體請參考 API Reference 文檔
    )
);echo $ch;      

3. Client 調起支付控件完成支付

Client 接收 Server 傳回的帶支付憑據的 Charge 對象并用之調起支付插件完成交易

[Pingpp createPayment:charge viewController:viewController appURLScheme:kUrlScheme withCompletion:^(NSString *result, PingppError *error) {    if ([result isEqualToString:@"success"]) {        // ...
    } else {        NSLog(@"PingppError: code=%lu msg=%@", error.code, [error getMsg]);
    }
}];      

4. 管道同步傳回支付結果給 Client

在上一步中使用者完成了支付,管道會傳回一個支付結果給用戶端,這裡 Client 需要做的是處理此結果。

管道為銀聯、百度錢包或者管道為支付寶但未安裝支付寶錢包時,交易結果會在調起插件時的 Completion 中傳回。 管道為微信、支付寶且安裝了支付寶錢包時,請實作 ​

​UIApplicationDelegate​

​ 的 ​

​- application:openURL:sourceApplication:annotation:​

​ 方法:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    [Pingpp handleOpenURL:url withCompletion:^(NSString *result, PingppError *error) {        if ([result isEqualToString:@"success"]) {            // ...
        } else {            NSLog(@"PingppError: code=%lu msg=%@", error.code, [error getMsg]);
        }
    }];    return  YES;
}      

5. Server 收到 Ping++ 發送的交易結果的異步通知

<?php$input_data = json_decode(file_get_contents("php://input"), true);if($input_data['object'] == 'charge') {    //TODO update database
    echo 'success';
} else {    echo 'fail';
}