最近公司說要實(shí)現(xiàn)蘋果支付,查了不少文章,終于搞懂了。
1.之前看了別人的例子很疑惑錢去哪了?
蘋果支付只是一個方式,你把它比作POS機(jī),錢還是從一家銀行的賬戶到另一家銀行的賬戶。
2.既然是跟銀行有關(guān)系,是銀行的業(yè)務(wù)那就得使用銀行SDK
一是使用第三方提供商的SDK接入,另一種是讓PassKit Framework直接與銀聯(lián)的接口對接,當(dāng)然網(wǎng)絡(luò)上還有一些自己使用PassKit PaymentRequest自己生成訂單組織信息,直接與Apple對接的Demo,因為我們不可能每家銀行跑去簽約,大陸的銀行也不會給我們開放特許,因此這種方式僅僅能用于測試ApplePay的功能和API嘗鮮,并不適用于生產(chǎn)中。
原文鏈接:http://www.lxweimin.com/p/a0c4d34feadd
我用來一下中國銀聯(lián)的SDk,從這里https://open.unionpay.com/下載文檔,這個是他的文檔截圖。
NSString *tnMode;
NSMutableData* _responseData;
-(void)UnionPay{
/***/
tnMode = @"01";//01表示測試
NSURL* url = [NSURL URLWithString:@"http://101.231.204.84:8091/sim/getacptn"];//從中國銀聯(lián)拿到測試的tn
NSMutableURLRequest * urlRequest=[NSMutableURLRequest requestWithURL:url];
NSURLConnection* urlConn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[urlConn start];
}
#pragma mark - connection
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
NSHTTPURLResponse* rsp = (NSHTTPURLResponse*)response;
NSInteger code = [rsp statusCode];
if (code != 200)
{
[connection cancel];
}
else
{
_responseData = [[NSMutableData alloc] init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_responseData appendData:data];
}
/**
*? 支付接口
*
*? @param tn? ? ? ? ? ? 訂單信息
*? @param mode? ? ? ? ? 接入模式,標(biāo)識商戶以何種方式調(diào)用支付控件,00生產(chǎn)環(huán)境,01測試環(huán)境
*? @param viewController 啟動支付控件的viewController
*? @param delegate? ? ? 實(shí)現(xiàn) UPAPayPluginDelegate 方法的 UIViewController
*? @param mID? ? ? ? ? ? 蘋果公司分配的商戶號,表示調(diào)用Apple Pay所需要的MerchantID;
*? @return 返回函數(shù)調(diào)用結(jié)果,成功或失敗
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString* tn = [[NSMutableString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
if (tn != nil && tn.length > 0)
{
if([PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay]])
{
[UPAPayPlugin startPay:tn mode:tnMode viewController:self delegate:self andAPMechantID:@"merchant.com.szcares.nash.YUPIAO"];
}
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
#pragma mark -
#pragma mark 響應(yīng)控件返回的支付結(jié)果
#pragma mark -
- (void)UPAPayPluginResult:(UPPayResult *)result
{
if(result.paymentResultStatus == UPPaymentResultStatusSuccess) {
NSString *otherInfo = result.otherInfo?result.otherInfo:@"";
NSString *successInfo = [NSString stringWithFormat:@"支付成功\n%@",otherInfo];
[self showAlertMessage:successInfo];
}
else if(result.paymentResultStatus == UPPaymentResultStatusCancel){
[self showAlertMessage:@"支付取消"];
}
else if (result.paymentResultStatus == UPPaymentResultStatusFailure) {
NSString *errorInfo = [NSString stringWithFormat:@"%@",result.errorDescription];
[self showAlertMessage:errorInfo];
}
else if (result.paymentResultStatus == UPPaymentResultStatusUnknownCancel)? {
//TODO UPPAymentResultStatusUnknowCancel表示發(fā)起支付以后用戶取消,導(dǎo)致支付狀態(tài)不確認(rèn),需要查詢商戶后臺確認(rèn)真實(shí)的支付結(jié)果
NSString *errorInfo = [NSString stringWithFormat:@"支付過程中用戶取消了,請查詢后臺確認(rèn)訂單"];
[self showAlertMessage:errorInfo];
}
}
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
NSLog(@"花費(fèi): %@", payment);
BOOL asyncSuccessful = FALSE;
if(asyncSuccessful) {
completion(PKPaymentAuthorizationStatusSuccess);
NSLog(@"支付成功");
} else {
completion(PKPaymentAuthorizationStatusFailure);
NSLog(@"支付失敗");
}
}