Question about. SquareMobilePaymentsSDK

import Foundation
import SquareMobilePaymentsSDK
import UIKit
import SwiftUI

class PaymentViewController: UIViewController, PaymentManagerDelegate {
private var paymentHandle: PaymentHandle?
func startPayment() {
let idempotencyKey = UUID().uuidString

    let amountMoney = Money(amount: 100, currency: .USD)

    let paymentParams = PaymentParameters(
        idempotencyKey: idempotencyKey,
        amountMoney: amountMoney,
        processingMode: .autoDetect
    )
    paymentParams.note = "POS Order"
    paymentParams.referenceID = "Order_\(idempotencyKey)"

    let promptParams = PromptParameters(
        mode: .default,
        additionalMethods: .tapToPay
    )

    paymentHandle = MobilePaymentsSDK.shared.paymentManager.startPayment(
        paymentParams,
        promptParameters: promptParams,
        from: self,
        delegate: self
    )
}
// MARK: - PaymentManagerDelegate
func paymentManager(_ paymentManager: PaymentManager, didStart payment: Payment) {
    print("🟡 didStart: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didFinish payment: Payment) {
    print("✅ didFinish: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didCancel payment: Payment) {
    print("⚪️ didFail: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didFail payment: Payment, withError error: Error) {
    print("❌ : \(payment.id) error: \(error.localizedDescription)")
}
func paymentManager(_ paymentManager: PaymentManager, willFinish payment: Payment) {
    print("📘willFinish: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, willCancel payment: Payment) {
    print("📘 willCancel: \(payment.id)")
}

}

HERE is my code, keep give me ERROR:
1: Type ‘PaymentViewController’ does not conform to protocol ‘PaymentManagerDelegate’

2: Type ‘String’ has no member ‘USD’ ,
if i change to : let amountMoney = Money(amount: 100, currency: “USD”)
THEN : amountMoney: amountMoney, here will error :Argument type ‘Money’ does not conform to expected type ‘MoneyAmount’

please help .

:waving_hand: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Enable Apple Pay
Build on iOS
Enable Secure Remote Commerce - iOS

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

Based on the SDK code and the errors you’re encountering, here’s the corrected version of your code:

import Foundation
import SquareMobilePaymentsSDK
import UIKit
import SwiftUI

class PaymentViewController: UIViewController, PaymentManagerDelegate {
    private var paymentHandle: PaymentHandle?
    
    func startPayment() {
        let idempotencyKey = UUID().uuidString
        
        // Fix 2: Use MoneyAmount instead of Money
        let amountMoney = MoneyAmount(amount: 100, currency: .USD)
        
        let paymentParams = PaymentParameters(
            idempotencyKey: idempotencyKey,
            amountMoney: amountMoney,
            processingMode: .autoDetect
        )
        paymentParams.note = "POS Order"
        paymentParams.referenceID = "Order_\(idempotencyKey)"
        
        let promptParams = PromptParameters(
            mode: .default,
            additionalMethods: .tapToPay
        )
        
        paymentHandle = MobilePaymentsSDK.shared.paymentManager.startPayment(
            paymentParams,
            promptParameters: promptParams,
            from: self,
            delegate: self
        )
    }
    
    // MARK: - PaymentManagerDelegate
    func paymentManager(_ paymentManager: PaymentManager, didStartPayment payment: Payment) {
        print("🟡 didStart: \(payment.id)")
    }
    
    func paymentManager(_ paymentManager: PaymentManager, didFinishPayment payment: Payment) {
        print("✅ didFinish: \(payment.id)")
    }
    
    func paymentManager(_ paymentManager: PaymentManager, didCancelPayment payment: Payment) {
        print("⚪️ didFail: \(payment.id)")
    }
    
    func paymentManager(_ paymentManager: PaymentManager, didFailPayment payment: Payment, withError error: Error) {
        print("❌ : \(payment.id) error: \(error.localizedDescription)")
    }
    
    func paymentManager(_ paymentManager: PaymentManager, willFinishPayment payment: Payment) {
        print("📘willFinish: \(payment.id)")
    }
    
    func paymentManager(_ paymentManager: PaymentManager, willCancelPayment payment: Payment) {
        print("📘 willCancel: \(payment.id)")
    }
}

The key changes made to fix the errors:

  1. Fixed Error 1 - Protocol Conformance:
  • Changed the delegate method names to match exactly what the protocol expects:
  • didStart → didStartPayment
  • didFinish → didFinishPayment
  • didCancel → didCancelPayment
  • didFail → didFailPayment
  • willFinish → willFinishPayment
  • willCancel → willCancelPayment
  1. Fixed Error 2 - Money Type:
  • Changed Money to MoneyAmount
  • The currency should be accessed as an enum case .USD rather than a string "USD"

Make sure you have imported the latest version of the Square Mobile Payments SDK. The SDK uses MoneyAmount instead of Money for representing monetary values, and the currency is defined as an enum rather than a string.

Also, ensure you’ve properly initialized the SDK before using it:

// In your app delegate or early in app lifecycle:
MobilePaymentsSDK.shared.initialize()

:slight_smile:

HI BRYAN

i have tried your code, but some error .

1: Type ‘PaymentViewController’ does not conform to protocol ‘PaymentManagerDelegate’

2: ‘any MoneyAmount’ cannot be constructed because it has no accessible initializers

and i clean and reinstall the sdk . and the error still there ? i think it might be have some issue about the proper import SquareMobilePaymentsSDK, any suggest ?

also i want to mention is that my project use Square In-App Payments SDK before ,and i dont know install both sdk will cause that problem ? but i have remove the Square In-App Payments SDK , and error still remain.

Oh, so do you have both SDKs? If so that won’t work. You can only use either Mobile Payments SDK or In-App Payments SDK. Not both in the same app. :slight_smile:

SO i had Square In-App Payments SDK at first ,and it works,
then i tried to add SquareMobilePaymentsSDK to my project, but with out removing Square In-App Payments SDK. so the error show up, so i realize maybe both SDK at some time is not good, and then i remove both SDK, and reinstall the SquareMobilePaymentsSDK. but now the error still shows, what did i do wrong ? and how can i fix it ? thanks Bryan.

do you think , is the COREPAYMENTCARD AND THREEDS_SDK FRAMEWORK remain in the project will cause problem ?

It wouldn’t surprise me if that’s causing problems. Is there a reason you kept them? They’re only used with In-App Payments SDK. :slight_smile: