Ping++ SDK 包括 Server 和 Client 兩部分。php
其中 Server SDK 提供了 PHP
, Java
, Python
, Node.js
, Ruby
, Go
六種語言版本。ios
Client SDK 提供了 Android
, iOS
, HTML5
三種平臺版本。c++
應用在接入 Ping++ SDK 時,須要使用如下三個參數,這三個參數你能夠在管理平臺中獲取:git
API Key:API Key 是 Ping++ 分配給你的惟一身份標識。在 Server SDK 的使用過程當中須要配置該參數。註冊 Ping++ 帳號並經過審覈後,Ping++ 會分配給你兩個 API Key,分別爲:Test Key
和 Live Key
。 Test Key
和 Live Key
分別代表使用測試模式和真實模式,你能夠根據實際應用使用場景分別使用。github
應用 ID:應用 ID 是 Ping++ 分配給你的應用的惟一標識。在 Server SDK 的使用過程當中須要配置該參數。json
Notify URL:Notify URL 是 Ping++ 系統用來向你的應用後臺推送異步通知時使用的地址,該地址必須是一個互聯網能夠訪問的地址。你能夠在 Ping++ 管理平臺中對應的應用內進行設置。服務器
1、接入步驟:微信
獲取 SDKapp
從 Github 下載 SDK, 裏面包含了 lib 文件夾和 example 文件夾。lib 文件夾裏面是 SDK 的文件,example 文件夾裏是接入示例代碼異步
依賴 Frameworks:
必需:
CFNetwork.frameworkSystemConfiguration.frameworkSecurity.frameworklibc++.dylib
百度錢包所需:
libz.dyliblibstdc++.dylibCoreTelephony.frameworkAddressBook.frameworkAddressBookUI.frameworkAudioToolbox.frameworkCoreAudio.frameworkCoreGraphics.frameworkImageIO.frameworkMapKit.frameworkMessageUI.frameworkMobileCoreServices.frameworkQuartzCore.framework
根據所需渠道導入相應的庫文件
添加 URL Schemes:在 Xcode 中,選擇你的工程設置項,選中 "TARGETS" 一欄,在 "Info" 標籤欄的 "URL Types" 添加 "URL Schemes",若是使用微信,填入所註冊的應用程序 id,若是不使用微信,則自定義,建議起名稍複雜一些,儘可能避免與其餘程序衝突。
添加 Other Linker Flags:在 Build Settings 搜索 Other Linker Flags ,添加 -ObjC
。
2、發起支付:
用戶選擇渠道點擊交易按鈕, 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]; // ...}];
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;
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]); } }];
在上一步中用戶完成了支付,渠道會返回一個支付結果給客戶端,這裏 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; }
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'; }