iOS Swift PayPal Always In Sandbox - ios

I have successfully integrated PayPal into my app. However, it seems to be stuck in Sandbox mode. Here is my code to initialize it:
PayPalMobile .initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction: "APP-0XU423690N0796541"])
As can been seen, I'm not even specifying a sandbox ID.
My code to initiate the payment is:
let payPalConfig = PayPalConfiguration()
payPalConfig.merchantName = "MacCrafters Software"
let item1 = PayPalItem(name: "Donation", withQuantity: 1, withPrice: NSDecimalNumber(string: amountField.text!), withCurrency: "USD", withSku: "Donation")
let items = [item1]
let subtotal = PayPalItem.totalPriceForItems(items)
// Optional: include payment details
let shipping = NSDecimalNumber(string: "0.00")
let tax = NSDecimalNumber(string: "0.00")
let paymentDetails = PayPalPaymentDetails(subtotal: subtotal, withShipping: shipping, withTax: tax)
let total = subtotal.decimalNumberByAdding(shipping).decimalNumberByAdding(tax)
let payment = PayPalPayment(amount: total, currencyCode: "USD", shortDescription: "Donation", intent: .Sale)
payment.items = items
payment.paymentDetails = paymentDetails
if (payment.processable) {
let paymentViewController = PayPalPaymentViewController(payment: payment, configuration: payPalConfig, delegate: self)
presentViewController(paymentViewController!, animated: true, completion: nil)
}
At the bottom of the PayPal view it always says "Mock Data". I get the same results no matter if I'm in the simulator or on a device. What am I doing wrong?

Related

How to use pkpaymentsummaryitemtype.pending in ApplePay/Swift?

In my app I need to add pending pay (after user have got some service).
I'm implement apple pay and received token. But I don't get how to make payment with item type pkpaymentsummaryitemtype.pending?
Here is my pay method:
func makePay()
{
let request = PKPaymentRequest()
request.merchantIdentifier = ApplePaySwagMerchantID
request.supportedNetworks = SupportedPaymentNetworks
request.merchantCapabilities = PKMerchantCapability.capability3DS
request.countryCode = "US"
request.currencyCode = "USD"
request.paymentSummaryItems = [
PKPaymentSummaryItem(label: "Some service", amount: 0, type: PKPaymentSummaryItemType.pending)]
let applePayController = PKPaymentAuthorizationViewController(paymentRequest: request)!
applePayController.delegate = self
var topVC = UIApplication.shared.keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
topVC = topVC!.presentedViewController
}
topVC?.present(applePayController, animated: true)
}
But when I have to show and debit final amount? I cant find examples of code on swift/IOS.
Or if I understood correct - with that type I just received authorization first and than with received token my bank/payment system will debit final amount after I send it?
Is it correct?

Paypal Payment issue of credit card payment

I have integrate the paypal payment but when i entered the credit card detail on live mode it returns the error-
UNAUTHORIZED_PAYMENT - The merchant does not accept payments of this type. (401)
I am using the business account and permissions are enabled.
var payPalConfig = PayPalConfiguration()
var environment:String = PayPalEnvironmentProduction {
willSet(newEnvironment) {
if (newEnvironment != environment) {
PayPalMobile.preconnect(withEnvironment: newEnvironment)
}
}
}
var acceptCreditCards: Bool = true {
didSet {
payPalConfig.acceptCreditCards = acceptCreditCards
}
}
func paypalPaymentGatewaySetup(){
payPalConfig.acceptCreditCards = acceptCreditCards;
payPalConfig.merchantName = "User"
payPalConfig.merchantPrivacyPolicyURL = NSURL(string: "https://www.paypal.com/webapps/mpp/ua/privacy-full") as URL?
payPalConfig.merchantUserAgreementURL = NSURL(string: "https://www.paypal.com/webapps/mpp/ua/useragreement-full") as URL?
payPalConfig.languageOrLocale = NSLocale.preferredLanguages[0]
payPalConfig.payPalShippingAddressOption = .payPal;
PayPalMobile.preconnect(withEnvironment: environment)
}
override func viewDidLoad() {
super.viewDidLoad()
paypalPaymentGatewaySetup()
}
Now i am calling function makingRequestForPayment on click of make payment button and i have import payment status delegate as well. But in paypal delegate method i am getting "The merchant does not accept this type of payment Unauthorized 401". I did app delegate setup with client ID of Production as well
func makingRequestForPayment(){
// let globalPriceInStr = UserDefaults.standard.value(forKey: "PriceForPayment") as? String ?? ""
let globalPriceInStr = "1"
let totalPrice = NSDecimalNumber(string: globalPriceInStr)
let item1 = PayPalItem(name: "Membership", withQuantity: 1, withPrice: totalPrice, withCurrency: "USD", withSku: "SivaGanesh-0001")
print(item1)
let items = [item1]
print(items)
let subtotal = PayPalItem.totalPrice(forItems: items)
let shipping = NSDecimalNumber(string: "0.00")
let tax = NSDecimalNumber(string: "0.00")
let paymentDetails = PayPalPaymentDetails(subtotal: subtotal, withShipping: shipping, withTax: tax)
let total = subtotal.adding(shipping).adding(tax)
let payment = PayPalPayment(amount: total, currencyCode: "USD", shortDescription: "Advertisement Submission", intent: .sale)
payment.items = items
payment.paymentDetails = paymentDetails
if (payment.processable) {
let paymentViewController = PayPalPaymentViewController(payment: payment, configuration: payPalConfig, delegate: self)
present(paymentViewController!, animated: true, completion: nil)
}
else{
print("Payment not processable: \(payment)")
}
}

Apple pay money not withdraw

We have app in the App Store. When user have pay, the payment is success but money not withdraw from credit card. What can be a problem? Maybe we need add bank information or something like this?
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: supportedPaymentNetworks) {
let request = PKPaymentRequest(
request.merchantIdentifier = "merchant.com.my_app_identifier"
request.merchantCapabilities = .capability3DS
request.countryCode = "RU"
request.currencyCode = "RUR"
request.supportedNetworks = supportedPaymentNetworks
request.paymentSummaryItems = [PKPaymentSummaryItem(label: egrn.name, amount: NSDecimalNumber(value: price))]
let applePayController = PKPaymentAuthorizationViewController(paymentRequest: request)!
applePayController.delegate = delegate
self.present(applePayController, animated: true, completion: nil)
}

show yearly price in InApp Purchase swift ios

Currently i am working on a iOS App using Swift. I am enabled InApp Purchased. Payment is doing perfectly but when i am showing data from iTunes Connect, the monthly subscription data showing perfectly but i want show price on yearly tab in monthly format with some discount and when user tap on card, it should be show yearly price. I am unable to do this. Description shown in image. Thanks in Advance.
image1
image 2
I want show price like this
// Currently i am getting product info with this method
// MARK: - REQUEST IAP PRODUCTS
func productsRequest (_ request:SKProductsRequest, didReceive response:SKProductsResponse) {
if (response.products.count > 0) {
iapProducts = response.products
// showHUD("Loading...")
let indexPath = IndexPath.init(row: 0, section: 0)
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell else { return }
let numberFormatter = NumberFormatter()
let firstProduct = response.products[0] as SKProduct
print("localizedDescription", firstProduct.localizedDescription)
print("localizedTitle", firstProduct.localizedTitle)
// Get its price from iTunes Connect
numberFormatter.formatterBehavior = .behavior10_4
numberFormatter.numberStyle = .currency
numberFormatter.locale = firstProduct.priceLocale
let price1Str = numberFormatter.string(from: firstProduct.price)
// Show its description
cell.monthlyLabel.text = "\(firstProduct.localizedTitle)"
cell.rupeesLabel.text = "\(price1Str!)"
cell.perMonthLabel.text = "\(firstProduct.localizedDescription)"
let indexPath1 = IndexPath.init(row: 1, section: 0)
guard let cell2 = collectionView.cellForItem(at: indexPath1) as? CollectionViewCell else { return }
let secondProd = response.products[1] as SKProduct
// Get its price from iTunes Connect
numberFormatter.locale = secondProd.priceLocale
let price2Str = numberFormatter.string(from: secondProd.price)
// Show its description
cell2.monthlyLabel.text = "\(secondProd.localizedTitle)"
cell2.rupeesLabel.text = "\(price2Str!)"
cell2.perMonthLabel.text = "\(secondProd.localizedDescription)"
// ------------------------------------
}
}
Showing an annual subscription in terms of months is a little more complicated than just dividing by twelve. SKProduct.price is an NSDecimalNumber class, not a regular float, so standard divide operator doesn't work.
You need to do something like this
product.price.dividing(by: NSDecimalNumber(decimal: Decimal(12.0)))
This will give you the divided NSDecimalNumber which you can pass to the formatter. One problem is that the divided value might round to an incorrect value. The trick is to create a custom NSDecimalNumberHandler that rounds as you want.
let behavior = NSDecimalNumberHandlerroundingMode: .down, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
product.price.dividing(by: NSDecimalNumber(decimal: Decimal(12.0)), withBehavior: behavior)
That should give you all the control you need to display the annual price at the monthly rate. I would also suggest that you display the total price nearby to not mislead the user too much. There is a fine line between optimizing your purchase flow and trying to scam people.
Thanks a lot Jacob Eiting. This is worked for me in Swift3.
let behavior = NSDecimalNumberHandler(roundingMode: .down, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
let price2Str = numberFormatter.string(from: (secondProd.price.dividing(by: NSDecimalNumber(decimal: Decimal(12.0)), withBehavior: behavior)))
cell2.rupeesLabel.text = "\(price2Str!)"

Swift PKPayment Request error

I'm currently working with a PKPaymentAuthorizationViewController and presenting it to validate a payment within my application. I am able to present the controller but receive an error stating "Invalid in-app payment request. PKPaymentRequest must contain a property 'countryCode' of type 'NSString'". I have set the country code for the payment request and still receive this error. Attached below if the code I'm using to create the payment request and present the controller. I have X'd out my merchant ID for the purpose of posting. Any help would be appreciated.
var applePayController = PKPaymentAuthorizationViewController()
applePayController.delegate = self
let request = PKPaymentRequest()
let supportedPaymentNetworks = [PKPaymentNetwork.visa, PKPaymentNetwork.masterCard, PKPaymentNetwork.amex, PKPaymentNetwork.discover]
let applePayMerchantID = "merchant.com.xxxxxxxxxx.XXXXXXXXXXX"
request.countryCode = "US"
request.currencyCode = "USD"
request.merchantIdentifier = applePayMerchantID
request.supportedNetworks = supportedPaymentNetworks
request.merchantCapabilities = PKMerchantCapability.capability3DS
let price = Decimal(self.creditsPrice[index])
let priceDecimal = NSDecimalNumber(decimal: price)
request.paymentSummaryItems.append(PKPaymentSummaryItem(label: "for \(self.credits[index]) Credits", amount: priceDecimal))
applePayController = PKPaymentAuthorizationViewController(paymentRequest: request)
self.paymentName = self.credits[index]
self.paymentValue = self.creditsPrice[index]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: supportedPaymentNetworks) {
self.present(applePayController, animated: true, completion: nil)
} else {
self.displayAlert(title: "Error", message: "Please setup Apple Pay before trying to purchase credits")
}

Resources