iOS支付配置 ping++

發個廣告:ios開發兩年了,一步步走來  關注公衆號一起進步

ping++帖子總結

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

API Key:API Key 是 Ping++ 分配給你的唯一身份標識。在 Server SDK 的使用過程中需要配置該參數。註冊 Ping++ 賬號並通過審覈後,Ping++ 會分配給你兩個 API Key,分別爲:test key和live key。這倆 分別表明使用測試模式和真實模式,你可以根據實際應用使用場景分別使用。

應用 ID:應用 ID 是 Ping++ 分配給你的應用的唯一標識。在 Server SDK 的使用過程中需要配置該參數。

Notify URL:Notify URL 是 Ping++ 系統用來向你的應用後臺推送異步通知時使用的地址,該地址必須是一個互聯網可以訪問的地址。你可以在 Ping++ 管理平臺中對應的應用內進行設置。

一、接入步驟:

獲取 SDK

從 Github 下載 SDK, 裏面包含了 lib 文件夾和 example 文件夾。lib 文件夾裏面是 SDK 的文件,example 文件夾裏是接入示例代碼 

cocoapods 在profile中加入  pod 'Pingpp'

依賴 Frameworks:

           必需:

    1. CFNetwork.frameworkSystemConfiguration.frameworkSecurity.frameworklibc++.dylib

                 百度錢包所需:

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


    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++ 發送的交易結果的異步通知

    Ping++ 會把從渠道收到的異步通知告訴商戶 Server,客戶 Server 接收到異步通知是一個帶支付狀態的完整的 Charge 對象,客戶在接收到異步通知後需要回復 success 給 Ping++ 表明成功收到異步通知。所有的交易結果,商戶均須以異步通知結果爲準。關於異步通知具體請參見 API Reference 文檔。

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

  1. 用到微信 的話 需要用到這樣的配置

  2. 添加網絡訪問權限及白名單

    添加URL Type

發佈了70 篇原創文章 · 獲贊 44 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章