一、在pod文件中添加以下代码导入SDK
pod 'Stripe'
二、在AppDelegate.m中引入并初始化sdk
#import <Stripe/Stripe.h>
//配置stripe支付
[[STPPaymentConfiguration sharedConfiguration] setPublishableKey:@"key"]; [[STPPaymentConfiguration sharedConfiguration] setAppleMerchantIdentifier:@"merchantId"];
三、#pragma mark- 苹果支付<PKPaymentAuthorizationViewControllerDelegate>
- (void)startApplePay {
/*
* 开始配置支付信息
*/
PKPaymentRequest*paymentRequest = [Stripe paymentRequestWithMerchantIdentifier:@"merchantId"country:@"IT"currency:@"EUR"];
paymentRequest.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
paymentRequest.merchantCapabilities = PKMerchantCapability3DS;
NSDecimalNumber *totalAmount = [NSDecimalNumber decimalNumberWithString:self.payMoney];
NSString *summary01 = @"标题";
NSString *summary02 = @"Pay内容";
paymentRequest.paymentSummaryItems=@[
[PKPaymentSummaryItem summaryItemWithLabel:summary01 amount:totalAmount],
//它的前缀是“Pay”(即“付款”)。"Pay iHats, Inc $50")
[PKPaymentSummaryItem summaryItemWithLabel:summary02 amount:totalAmount]
];
PKPaymentAuthorizationViewController *xxPaymentController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:paymentRequest];
xxPaymentController.delegate = (id<PKPaymentAuthorizationViewControllerDelegate>)self;
[self.navigationController presentViewController:xxPaymentController animated:YES completion:nil];
}
#pragma mark - PKPaymentAuthorizationViewControllerDelegate苹果支付代理
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment*)payment
handler:(void(^)(PKPaymentAuthorizationResult*result))completion{
[[STPAPIClient sharedClient] createTokenWithPayment:payment completion:^(STPToken *token, NSError *error) {
NSLog(@"token:%@",token);
if(token ==nil || error !=nil) {
// Present error to user...
//NSLog(@"苹果支付出错 --- %@",error);
PKPaymentAuthorizationResult *re = [[PKPaymentAuthorizationResult alloc]initWithStatus:PKPaymentAuthorizationStatusFailure errors:nil];
completion(re);
return;
}
//将获取的token传递给服务端进行支付和验证
}];
}
网友评论