iOS中實現IAP和Apple Pay支付的注意點
IAP開發需要的注意點
在沙箱環境測試
Store Kit在模擬器上無法執行,需要使用真機測試
sanbox和app store內容一致,只是不執行真實的支付動作
在測試的iphone上面退出itunes賬戶
不能在測試的iphone手機上登入測試賬戶登入
在程式中購買商品後,storekit會提示你去驗證交易,這個時候你需要用測試賬戶登入,並批覆支付
登入itunes connect去使用者和職能中新增沙盒技術測試賬戶
IAP開發在客戶端和服務端需要做的事情
在客戶端 檢測是否可以進行支付-》獲取商品資訊-》建立一個支付物件-》為支付佇列註冊一個觀察者物件-》交易結果-》提示給使用者
服務端將接收到的資料以base64編碼的方式建立JSON併發送給APPSore進行驗證
程式碼如下
#import <StoreKit/StoreKit.h>
@interface ViewController ()<SKProductsRequestDelegate,SKStoreProductViewControllerDelegate>
@property (nonatomic,strong)SKProduct *product;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)startPayAction:(UIButton *)sender {
//判斷當前裝置是否允許支付
if(![SKPaymentQueue canMakePayments]) {
return;
}
NSSet *set =[NSSet setWithObjects:@"", nil];
SKProductsRequest *req=[[SKProductsRequest alloc] initWithProductIdentifiers:set];
req.delegate=self;
[req start];
}
#pragma mark SKStoreProductViewControllerDelegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
}
#pragma mark SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
//
for (SKProduct *product in response.products) {
self.product = product;
}
//將獲取到的產品資訊,加入到支付佇列
SKPayment *payment =[SKPayment paymentWithProduct: self.product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
break;
case SKPaymentTransactionStateFailed:
break;
case SKPaymentTransactionStateRestored:
break;
default:
break;
}
}
}
if([PKPaymentAuthorizationController canMakePayments]) {
PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
PKPaymentSummaryItem *wi=[PKPaymentSummaryItem summaryItemWithLabel:@"fdfd" amount:[NSDecimalNumber decimalNumberWithString:@"0.22"]];
request.countryCode = @"US";
request.currencyCode = @"USD";
request.supportedNetworks = @[PKPaymentNetworkAmex,PKPaymentNetworkMasterCard];
request.merchantIdentifier = @"fdfd";
request.merchantCapabilities=PKMerchantCapabilityEMV;
request.requiredBillingAddressFields=PKAddressFieldEmail|PKAddressFieldPostalAddress;
PKPaymentAuthorizationController *paymentPane = [[PKPaymentAuthorizationController alloc] initWithPaymentRequest:request];
paymentPane.delegate = self;
[self presentViewController:paymentPane animated:true completion:nil];
}