Skip to content

Commit

Permalink
feat: handles subscription and plan id as mandatory in purchase produ…
Browse files Browse the repository at this point in the history
…ct api
  • Loading branch information
cb-haripriyan committed Feb 20, 2023
1 parent 03a4e7e commit aa0bf7c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.chargebee.android.network.ReceiptDetail
import java.io.Serializable

data class PurchaseResult (
val subscriptionId: String?,
val planId: String?,
val subscriptionId: String,
val planId: String,
val status: Boolean
) : Serializable {
constructor(receiptDetail: ReceiptDetail, status: Boolean) : this(receiptDetail.subscription_id, receiptDetail.plan_id, status)
Expand Down
14 changes: 9 additions & 5 deletions ios/CBPurchaseResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import Foundation


struct CBPurchaseResult: Codable {

let subscriptionId: String?
let planId: String?
let subscriptionId: String
let planId: String
let status: Bool

enum CodingKeys: String, CodingKey {
Expand All @@ -21,10 +22,13 @@ struct CBPurchaseResult: Codable {
}

extension CBPurchaseResult {
init(fromTuple: (status:Bool, subscriptionId:String?, planId:String?)) {
self.subscriptionId = fromTuple.subscriptionId
init(fromTuple: (status: Bool, subscriptionId: String?, planId: String?)) throws {
guard let subscriptionId = fromTuple.subscriptionId, let planId = fromTuple.planId else {
throw CBReactNativeError.systemError
}
self.subscriptionId = subscriptionId
self.status = fromTuple.status
self.planId = fromTuple.planId
self.planId = planId
}
}

Expand Down
14 changes: 14 additions & 0 deletions ios/CBReactNativeError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// CBError+Extensions.swift
// ChargebeeReactNative
//
// Created by cb-haripriyan on 20/02/23.
//

import Foundation
import Chargebee

enum CBReactNativeError: String, Error {
case systemError = "System error"
}

8 changes: 6 additions & 2 deletions ios/ChargebeeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ public class ChargebeeHelper: NSObject {
CBPurchase.shared.purchaseProduct(product: product, customerId: customerId) { result in
switch result {
case .success(let result):
let purchasedProduct = CBPurchaseResult(fromTuple: result)
resolver(purchasedProduct.asDictionary)
do {
let purchasedProduct = try CBPurchaseResult(fromTuple: result)
resolver(purchasedProduct.asDictionary)
} catch (let error as NSError) {
rejecter("\(error.code)", error.localizedDescription, error)
}
case .failure(let error as NSError):
rejecter("\(error.code)", error.localizedDescription, error)
}
Expand Down

0 comments on commit aa0bf7c

Please sign in to comment.