AppsFlyerSDK / appsflyer-flutter-plugin

Flutter Plugin for AppsFlyer SDK
MIT License
142 stars 111 forks source link

[Feature] Purchase Connector #276

Open devibrahimkarahan opened 12 months ago

devibrahimkarahan commented 12 months ago

I need to add Purchase Connector but there is no document for Flutter and i will do it with native documents but Im wondering is there any plan to add Purchase Connector to Flutter plugin?

hamishjohnson commented 3 months ago

@devibrahimkarahan how did you solve this problem in the end? Necessary feature...

devibrahimkarahan commented 2 months ago

@hamishjohnson You can use native instructions for Purchase Connector, here are my additions; You need to use AppsFlyer Flutter SDK version 6.13.0+1 (If you want to change the SDK version, you need to change Purchase Connector version also)

For iOS

You can check official doc for details.

Podfile

You need to add PurchaseConnector pod (version is important). Then run pod install

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  pod 'PurchaseConnector', '6.13.0' # You can check versions from here https://github.com/AppsFlyerSDK/appsflyer-apple-purchase-connector/?tab=readme-ov-file#-this-module-is-built-for

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

AppDelegate

Import libraries

import PurchaseConnector
import StoreKit

With MethodChannel, you need to call these codes in your application function

if call.method == "initPurchaseConnector" {
      PurchaseConnector.shared().purchaseRevenueDelegate = self
      PurchaseConnector.shared().purchaseRevenueDataSource = self
      PurchaseConnector.shared().autoLogPurchaseRevenue = [.autoRenewableSubscriptions, .inAppPurchases]
      PurchaseConnector.shared().startObservingTransactions()
      result(nil)
}

And use this extension after the AppDelegate class.

@objc class AppDelegate: FlutterAppDelegate {
   ....
}

extension AppDelegate: PurchaseRevenueDataSource, PurchaseRevenueDelegate {
    // PurchaseRevenueDelegate method implementation
    func didReceivePurchaseRevenueValidationInfo(_ validationInfo: [AnyHashable : Any]?, error: Error?) {
    }
    // PurchaseRevenueDataSource method implementation
    func purchaseRevenueAdditionalParameters(for products: Set<SKProduct>, transactions: Set<SKPaymentTransaction>?) -> [AnyHashable : Any]? {
        // Add additional parameters for SKTransactions here.
        return [:];
    }
}

For Android

You can check official doc for details. You need to implement 2 dependencies inside build.gradle (app)

implementation 'com.appsflyer:purchase-connector:2.0.1'
implementation 'com.android.billingclient:billing:5.2.1'

Update your proguard rules

-keep class com.appsflyer.** { *; }
-keep class kotlin.jvm.internal.Intrinsics{ *; }
-keep class kotlin.collections.**{ *; }

Open your MainActivity.kt file and import these libraries

import com.appsflyer.api.PurchaseClient
import com.appsflyer.api.Store
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

With MethodChannel, you need to call these codes in your configureFlutterEngine function. If you dont have, open it. It must look like this

  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
      super.configureFlutterEngine(flutterEngine)
      MethodChannel(
          flutterEngine.dartExecutor.binaryMessenger, "your_channel_id"
      ).setMethodCallHandler { call, result ->
          if (call.method == "initPurchaseConnector") {
              try {
                  val afPurchaseClient =
                      PurchaseClient.Builder(this, Store.GOOGLE)
                          .logSubscriptions(true)
                          .autoLogInApps(true)
                          .build();
                  afPurchaseClient.startObservingTransactions()
                  result.success("")
              } catch (e: Exception) {
                  result.error("", "", "")
              }
          }  
      }
  }

Native codes are done. Now you need to call initPurchaseConnector method with the MethodChannel from Flutter side. I suggest you to call it before runApp.