最近公司说要实现苹果支付,查了不少文章,终于搞懂了。
1.之前看了别人的例子很疑惑钱去哪了?
苹果支付只是一个方式,你把它比作POS机,钱还是从一家银行的账户到另一家银行的账户。
2.既然是跟银行有关系,是银行的业务那就得使用银行SDK
一是使用第三方提供商的SDK接入,另一种是让PassKit Framework直接与银联的接口对接,当然网络上还有一些自己使用PassKit PaymentRequest自己生成订单组织信息,直接与Apple对接的Demo,因为我们不可能每家银行跑去签约,大陆的银行也不会给我们开放特许,因此这种方式仅仅能用于测试ApplePay的功能和API尝鲜,并不适用于生产中。
原文链接:http://www.jianshu.com/p/a0c4d34feadd
我用来一下中国银联的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"];//从中国银联拿到测试的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 接入模式,标识商户以何种方式调用支付控件,00生产环境,01测试环境
* @param viewController 启动支付控件的viewController
* @param delegate 实现 UPAPayPluginDelegate 方法的 UIViewController
* @param mID 苹果公司分配的商户号,表示调用Apple Pay所需要的MerchantID;
* @return 返回函数调用结果,成功或失败
*/
- (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 响应控件返回的支付结果
#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表示发起支付以后用户取消,导致支付状态不确认,需要查询商户后台确认真实的支付结果
NSString *errorInfo = [NSString stringWithFormat:@"支付过程中用户取消了,请查询后台确认订单"];
[self showAlertMessage:errorInfo];
}
}
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
NSLog(@"花费: %@", payment);
BOOL asyncSuccessful = FALSE;
if(asyncSuccessful) {
completion(PKPaymentAuthorizationStatusSuccess);
NSLog(@"支付成功");
} else {
completion(PKPaymentAuthorizationStatusFailure);
NSLog(@"支付失败");
}
}