<commit_before>//
//  BSPurchaseDataModel.swift
//  BluesnapSDK
//
//  Created by <NAME> on 18/04/2017.
//  Copyright © 2017 Bluesnap. All rights reserved.
//

import Foundation

/**
 Available payment types
 */
public enum BSPaymentType : String {
    case CreditCard = "CC"
    case ApplePay = "APPLE_PAY"
    case PayPal = "PAYPAL"
}


/**
 Base class for payment request; this will be the result of the payment flow (one of the inherited classes: BSCcDetails/BSApplePayPaymentRequest/BSPayPalPaymentRequest)
 */
@objc public class BSBasePaymentRequest : NSObject {
    
    // determines the request type: CC, Apple Pay, PayPal, etc.
    public var paymentType : BSPaymentType!

    var priceDetails: BSPriceDetails!
    
    // These fields hold the original amounts in USD, to keep precision in case of currency change
    internal var originalAmount : Double! = 0.0
    internal var originalTaxAmount : Double! = 0.0
    internal var originalRate : Double?
        
    internal init(initialData: BSInitialData) {
        super.init()
        self.priceDetails = initialData.priceDetails.copy() as! BSPriceDetails
    }
    
    // MARK: Change currency methods
    
    /*
    Set amounts will reset the currency and amounts, including the original
    amounts.
    */
    public func setAmountsAndCurrency(amount: Double!, taxAmount: Double?, currency: String) {
        
        self.priceDetails.amount = amount
        self.originalAmount = amount
        self.priceDetails.taxAmount = taxAmount
        self.originalTaxAmount = taxAmount ?? 0.0
        self.priceDetails.currency = currency
        self.originalRate = nil
    }
    
    /*
    Change currency will also change the amounts according to the change rates
    */
    public func changeCurrency(oldCurrency: BSCurrency?, newCurrency : BSCurrency?) {
        
        if originalRate == nil {
            if let oldCurrency = oldCurrency {
                originalRate = oldCurrency.getRate() ?? 1.0
            } else {
                originalRate = 1.0
            }
        }
        if let newCurrency = newCurrency {
            self.priceDetails.currency = newCurrency.code
            let newRate = newCurrency.getRate() / originalRate!
            self.priceDetails.amount = originalAmount * newRate
            self.priceDetails.taxAmount = originalTaxAmount * newRate
        }
    }
    
    // MARK: getters and setters
    
    public func getAmount() -> Double! {
        return priceDetails.amount
    }
    
    public func getTaxAmount() -> Double! {
        return priceDetails.taxAmount
    }
    
    public func getCurrency() -> String! {
        return priceDetails.currency
    }
}


/**
 price details: amount, tax and currency
 */
@objc public class BSPriceDetails : NSObject, NSCopying {
    
    public var amount : Double! = 0.0
    public var taxAmount : Double! = 0.0
    public var currency : String! = "USD"
    
    public init(amount : Double!, taxAmount : Double!, currency : String?) {
        super.init()
        self.amount = amount
        self.taxAmount = taxAmount
        self.currency = currency ?? "USD"
    }
    
    public func copy(with zone: NSZone? = nil) -> Any {
        let copy = BSPriceDetails(amount: amount, taxAmount: taxAmount, currency: currency)
        return copy
    }
}


/**
  Class holds initial data for the flow: 
    - Price details
    - (optional) Shopper details
    - Flow flavors (withShipping, withBilling)
 */
@objc public class BSInitialData : NSObject {

    public var withShipping: Bool = false
    public var fullBilling : Bool = false<commit_msg>MOB-211: Enable merchant to choose if to collect email or not [just add the configuration to initialData]<commit_after>//
//  BSPurchaseDataModel.swift
//  BluesnapSDK
//
//  Created by <NAME> on 18/04/2017.
//  Copyright © 2017 Bluesnap. All rights reserved.
//

import Foundation

/**
 Available payment types
 */
public enum BSPaymentType : String {
    case CreditCard = "CC"
    case ApplePay = "APPLE_PAY"
    case PayPal = "PAYPAL"
}


/**
 Base class for payment request; this will be the result of the payment flow (one of the inherited classes: BSCcDetails/BSApplePayPaymentRequest/BSPayPalPaymentRequest)
 */
@objc public class BSBasePaymentRequest : NSObject {
    
    // determines the request type: CC, Apple Pay, PayPal, etc.
    public var paymentType : BSPaymentType!

    var priceDetails: BSPriceDetails!
    
    // These fields hold the original amounts in USD, to keep precision in case of currency change
    internal var originalAmount : Double! = 0.0
    internal var originalTaxAmount : Double! = 0.0
    internal var originalRate : Double?
        
    internal init(initialData: BSInitialData) {
        super.init()
        self.priceDetails = initialData.priceDetails.copy() as! BSPriceDetails
    }
    
    // MARK: Change currency methods
    
    /*
    Set amounts will reset the currency and amounts, including the original
    amounts.
    */
    public func setAmountsAndCurrency(amount: Double!, taxAmount: Double?, currency: String) {
        
        self.priceDetails.amount = amount
        self.originalAmount = amount
        self.priceDetails.taxAmount = taxAmount
        self.originalTaxAmount = taxAmount ?? 0.0
        self.priceDetails.currency = currency
        self.originalRate = nil
    }
    
    /*
    Change currency will also change the amounts according to the change rates
    */
    public func changeCurrency(oldCurrency: BSCurrency?, newCurrency : BSCurrency?) {
        
        if originalRate == nil {
            if let oldCurrency = oldCurrency {
                originalRate = oldCurrency.getRate() ?? 1.0
            } else {
                originalRate = 1.0
            }
        }
        if let newCurrency = newCurrency {
            self.priceDetails.currency = newCurrency.code
            let newRate = newCurrency.getRate() / originalRate!
            self.priceDetails.amount = originalAmount * newRate
            self.priceDetails.taxAmount = originalTaxAmount * newRate
        }
    }
    
    // MARK: getters and setters
    
    public func getAmount() -> Double! {
        return priceDetails.amount
    }
    
    public func getTaxAmount() -> Double! {
        return priceDetails.taxAmount
    }
    
    public func getCurrency() -> String! {
        return priceDetails.currency
    }
}


/**
 price details: amount, tax and currency
 */
@objc public class BSPriceDetails : NSObject, NSCopying {
    
    public var amount : Double! = 0.0
    public var taxAmount : Double! = 0.0
    public var currency : String! = "USD"
    
    public init(amount : Double!, taxAmount : Double!, currency : String?) {
        super.init()
        self.amount = amount
        self.taxAmount = taxAmount
        self.currency = currency ?? "USD"
    }
    
    public func copy(with zone: NSZone? = nil) -> Any {
        let copy = BSPriceDetails(amount: amount, taxAmount: taxAmount, currency: currency)
        return copy
    }
}


/**
  Class holds initial data for the flow: 
    - Price details
    - (optional) Shopper details
    - Flow flavors (withShipping, withBilling, withEmail)
 */
@objc public class BSInitialData : NSObject {

    public var withEmail: Bool = true
    public var withShipping: Bool = false
    public var fullBilling : Bool = false
{'fraction_non_alphanumeric': 0.0830953352373181, 'fraction_numerical': 0.007479940160478716, 'mean_word_length': 2.374942634235888, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3947909', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1999, 'n_words': 637}