0
votes

Paypal mobile SDKs are deprecated, people suggested to use braintree. I'm creating a demo iOS app. This the error message i'm getting from server side: "The currency of this PayPal transaction must match the currency of the merchant account". The currency code i'm passing is PHP. Paypal automatically converts some currencies and PHP is not one of them. Is there any additional setup i need to do for this currency conversion?

Here is the code on client side:

class ViewController: UIViewController {

var btClient: BTAPIClient?
var dataCollector: BTDataCollector?

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.btClient = BTAPIClient(authorization: tokenizationKey)
    self.dataCollector = BTDataCollector(apiClient: btClient!)
    
    
}
@IBAction func buttonTouchUpInside(_ sender: Any) {
    startCheckout()
}

@objc func startCheckout() {
    let payPalDriver = BTPayPalDriver(apiClient: self.btClient!)
    payPalDriver.viewControllerPresentingDelegate = self
    payPalDriver.appSwitchDelegate = self
    let payPalRequest = BTPayPalRequest(amount: "1000.00")
    payPalRequest.currencyCode = "PHP"
    
    payPalRequest.lineItems = [.init(quantity: "1", unitAmount: "1000", name: "Item name", kind: .debit)]
    
    payPalRequest.isShippingAddressEditable = true
    payPalRequest.isShippingAddressRequired = true
    
    //Shipping address
    let postalAddress = BTPostalAddress()
    postalAddress.streetAddress = "1234 Main St."
    postalAddress.countryCodeAlpha2 = "PH"
    postalAddress.extendedAddress = "Unit 1"
    postalAddress.locality = "Chicago"
    postalAddress.postalCode = "60652"
    postalAddress.recipientName = "Jhon doe"
    postalAddress.region = "IL"
    payPalRequest.shippingAddressOverride = postalAddress
    
    payPalDriver.requestOneTimePayment(payPalRequest) { (tokenizedPayPalAccount, error) -> Void in
        if let tokenizedPayPalAccount = tokenizedPayPalAccount {
            print("nonce: \(tokenizedPayPalAccount.nonce)")
            self.dataCollector?.collectDeviceData() { deviceData in
                self.sendRequestPaymentToServer(nonce: tokenizedPayPalAccount.nonce, amount: "1000.00", deviceData: deviceData)
            }
        } else if let error = error {
            print(error.localizedDescription)
        } else {
            print("cancelled")
        }
    }
}

func sendRequestPaymentToServer(nonce: String, amount: String, deviceData: String) {
    print("payment_method_nonce=\(nonce)&amount=\(amount)&deviceData=\(deviceData)")
    let paymentURL = URL(string: "http://localhost/newServer/pay.php")!
    var request = URLRequest(url: paymentURL)
    request.httpBody = "payment_method_nonce=\(nonce)&amount=\(amount)&deviceData=\(deviceData)".data(using: String.Encoding.utf8)
    request.httpMethod = "POST"
    
    URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) -> Void in
        guard let data = data else {
            print(error!.localizedDescription)
            return
        }
        print(String(decoding: data, as: UTF8.self))
        guard let result = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let success = result["success"] as? Bool, success == true else {
            print("Transaction failed. Please try again.")
            return
        }
        
        print("Successfully charged. Thanks So Much :)")
        }.resume()
    }
}

This is the code on server side:

$paymentMethodNonce =  $_POST['payment_method_nonce'];
$amount = $_POST['amount'];
$deviceData = $_POST['deviceData'];

$result = $gateway->transaction()->sale([
    'amount' => $amount,
    'paymentMethodNonce' => $paymentMethodNonce,
    'options' => [
        'submitForSettlement' => true
    ],
    'deviceData' => $deviceData
]);

echo json_encode($result);
1

1 Answers

0
votes
self.btClient = BTAPIClient(authorization: tokenizationKey)

A tokenization key can only be used for a full Braintree gateway integration. Such an integration requires setting up the new currency, PHP, on the gateway sandbox and later production accounts that you are using for processing. If you want to use the Braintree gateway in production, your business needs to apply and be approved for this.

So basically, you need to set up PHP in your gateway interface if you are going to use that new currency with the above integration method.


To instead use the Braintree SDK with PayPal more directly (no gateway account), the credential you need to use for authentication first is an access token. Use the access token on your server to obtain a client token, which your client will fetch as needed: https://developers.braintreepayments.com/guides/authorization/client-token

A sandbox PayPal Braintree access token can be obtained from the bottom of: https://www.paypal.com/signin?intent=developer&returnUri=https%3A%2F%2Fdeveloper.paypal.com%2Fdeveloper%2Fapplication (For live mode, via https://www.paypal.com/api )