All Projects → jinSasaki → Inapppurchase

jinSasaki / Inapppurchase

Licence: mit
A Simple and Lightweight framework for In App Purchase

Programming Languages

swift
15916 projects
swift4
162 projects

Projects that are alternatives of or similar to Inapppurchase

Loadingshimmer
An easy way to add a shimmering effect to any view with just one line of code. It is useful as an unobtrusive loading indicator.
Stars: ✭ 1,180 (+484.16%)
Mutual labels:  ios11, carthage
SSAppUpdater
SSAppUpdater is an open-source framework that compares the current version of the app with the store version and returns the essential details of it like app URL, new app version number, new release note, etc. So you can either redirect or notify the user to update their app.
Stars: ✭ 58 (-71.29%)
Mutual labels:  carthage, appstore
Quickshop Reremake
QuickShop is a shop plugin, that allows players to sell items from a chest with no commands. It allows players to purchase any number of items easily. In fact, this plugin doesn't even have any commands that a player would ever need!
Stars: ✭ 82 (-59.41%)
Mutual labels:  purchase, store
Appstoreios11
iOS 11 Appstore clone.. practise of laying out views purely programatically
Stars: ✭ 346 (+71.29%)
Mutual labels:  appstore, ios11
Inapppy
Python In-app purchase validator for Apple AppStore and GooglePlay.
Stars: ✭ 110 (-45.54%)
Mutual labels:  appstore, purchase
Textdetection
Vision Framework Demo on Text Detection
Stars: ✭ 173 (-14.36%)
Mutual labels:  ios11
Uploadcare Widget
Uploadcare Widget, an ultimate tool for HTML5 file upload supporting multiple file upload, drag&drop, validation by file size/file extension/MIME file type, progress bar for file uploads, image preview.
Stars: ✭ 183 (-9.41%)
Mutual labels:  store
Beefun Pro
Github client for iOS in Swift.
Stars: ✭ 172 (-14.85%)
Mutual labels:  appstore
General Store
Simple, flexible store implementation for Flux. #hubspot-open-source
Stars: ✭ 171 (-15.35%)
Mutual labels:  store
Tkradarchart
A customizable radar chart in Swift
Stars: ✭ 199 (-1.49%)
Mutual labels:  carthage
Swiftyanimate
Composable animations in Swift
Stars: ✭ 194 (-3.96%)
Mutual labels:  carthage
Fluttergames
Flutter app for purchasing and renting games.
Stars: ✭ 182 (-9.9%)
Mutual labels:  store
Icimulator
Simulate camera functions on iOS Simulator with images, videos, or your MacBook Camera.
Stars: ✭ 177 (-12.38%)
Mutual labels:  carthage
Vue Vant Store
基于vue,vantUI的商城demo,包含前端和后端
Stars: ✭ 187 (-7.43%)
Mutual labels:  store
Irldocumentscanner
A drop-in Objective-C ViewController that will Automatically scan a document for you you.
Stars: ✭ 172 (-14.85%)
Mutual labels:  carthage
Imagetransition
Library for smooth animation of images during transitions.
Stars: ✭ 195 (-3.47%)
Mutual labels:  carthage
Wkcookiewebview
WKWebView with cookie sharing support
Stars: ✭ 171 (-15.35%)
Mutual labels:  carthage
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (-12.38%)
Mutual labels:  carthage
Reto
Flexible and efficient React Store with hooks.
Stars: ✭ 194 (-3.96%)
Mutual labels:  store
Cleanjson
Swift JSON decoder for Codable
Stars: ✭ 178 (-11.88%)
Mutual labels:  carthage

InAppPurchase

Build Status Carthage compatible Version Platform codecov

A Simple, Lightweight and Safe framework for In App Purchase

Feature

Installation

Carthage

github "jinSasaki/InAppPurchase"

CocoaPods

pod "InAppPurchase"

Usage

Setup Observer

NOTE: This method should be called at launch.

let iap = InAppPurchase.default
iap.addTransactionObserver(fallbackHandler: {
    // Handle the result of payment added by Store
    // See also `InAppPurchase#purchase`
})

If you want to detect the unexpected transactions, pass addTransactionObserver() with fallbackHandler.
For example, your app requested a payment, but it crashed in that process. That transaction is not finished, and then will receive at next launch.
This fallbackHandler is called when any handlers are not set to InAppPurchase via purchase(productIdentifier: handler:) method and so on.

Promoting In App Purchases is available from iOS 11. InAppPurchase supports it!

Add observer with shouldAddStorePaymentHandler.
See also SKPaymentTransactionObserver#paymentQueue(_:shouldAddStorePayment:for:)and Promoting In-App Purchases Guides

promoting

let iap = InAppPurchase.default
iap.set(shouldAddStorePaymentHandler: { (product) -> Bool in
    // Return whether starting payment
}, handler: { (result) in
    // Handle the result of payment added by Store
    // See also `InAppPurchase#purchase`
})

⚠️ Do not use Product#priceLocale

Only if purchase via AppStore Promoting, SKProduct#priceLocale has been not initialized. It occurs a BAD_ACCESS crash. This is a StoreKit bug. InAppPurchace resolved the crash that is occurred when received the payment, but it occurs when accessed Product#priceLocale yet. So, I recommend not to use Product#priceLocale in AppStore Promoting Payment process.

Stop payment observing if needed.

let iap = InAppPurchase.default
iap.removeTransactionObserver()

Fetch Product Information

let iap = InAppPurchase.default
iap.fetchProduct(productIdentifiers: ["PRODUCT_ID"], handler: { (result) in
    switch result {
    case .success(let products):
        // Use products
    case .failure(let error):
        // Handle `InAppPurchase.Error`
    }
})

Restore Completed Transaction

let iap = InAppPurchase.default
iap.restore(handler: { (result) in
    switch result {
    case .success(let productIds):
        // Restored with product ids
    case .failure(let error):
        // Handle `InAppPurchase.Error`
    }
})

Purchase

let iap = InAppPurchase.default
iap.purchase(productIdentifier: "PRODUCT_ID", handler: { (result) in
    // This handler is called if the payment purchased, restored, deferred or failed.

    switch result {
    case .success(let response):
        // Handle `PaymentResponse`
    case .failure(let error):
        // Handle `InAppPurchase.Error`
    }
})

For Dependency Injection

The purchase logic in the App should be safe and testable.

For example, you implemented a class to execute In-App-Purchase as follows.

// PurchaseService.swift

import Foundation
import InAppPurchase

final class PurchaseService {
    static let shared = PurchaseService()

    func purchase() {
        // Purchase with `InAppPurchase`
        InAppPurchase.default.purchase(productIdentifier: ...) {
            // Do something
        }
    }
}

It is hard to test this class because using the InAppPurchase.default in the purchase process.

This PurchaseService can be refactored to inject the dependency.
Use InAppPurchaseProvidable protocol.

// PurchaseService.swift

import Foundation
import InAppPurchase

final class PurchaseService {
    static let shared = PurchaseService()

    let iap: InAppPurchaseProvidable

    init(iap: InAppPurchaseProvidable = InAppPurchase.default) {
        self.iap = iap
    }

    func purchase() {
        // Purchase with `InAppPurchase`
        iap.purchase(productIdentifier: ...) {
            // Do something
        }
    }
}

And then you can test PurchaseService easily with InAppPurchaseStubs.framework.

// PurchaseServiceTests.swift

import XCTest
@testable import YourApp
import InAppPurchaseStubs

// Test
final class PurchaseServiceTests: XCTestCase {
    func testPurchase() {
        let expectation = self.expectation(description: "purchase handler was called.")
        let iap = StubInAppPurchase(purchaseHandler: { productIdentifier, handler in
            // Assert productIdentifier, handler, and so on.
        })
        let purchaseService = PurchaseService(iap: iap)
        purchaseService.purchase(productIdentifier: ...) {
            // Assert result
            expectation.fulfill()
        }

        wait(for: [expectation], timeout: 1)
    }
}

If you want more information for test, see also InAppPurchaseStubs and Tests.

Requirements

  • iOS 9.0+
  • Xcode 9+
  • Swift 4+

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].