Shopify / mobile-buy-sdk-ios

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using Apple Pay or their credit card.
MIT License
452 stars 199 forks source link

Checkout failed: The amount of the payment must match the total #1021

Closed zackshapiro closed 5 years ago

zackshapiro commented 5 years ago

I'm trying to get ApplePay checkout working and when I go to finish the transaction, I get this error:

"message": Amount The amount of the payment must match the total of 63.25.

This is the PayCheckout I'm sending into the mutation

PayCheckout(
    id: "Z2lkOi8vc2hvcGlmeS9DaGVja291dC8xZTIwNjZlODBkOGM3OWMzMTBiYzk1NTZmMTA1MDIzNz9rZXk9N2M5Y2Q3N2M2OTA0MWNiZDY1N2E0YjA2MWI2ODE4Yzc=",
    hasLineItems: true,
    needsShipping: true,
    giftCards: nil,
    discount: nil,
    shippingDiscount: nil,
    lineItems: [MobileBuySDK.PayLineItem(price: 55, quantity: 1), // paint
                MobileBuySDK.PayLineItem(price: 8.25, quantity: 1), // shipping
                MobileBuySDK.PayLineItem(price: 3.9875, quantity: 1)], // tax
    shippingAddress: Optional(MobileBuySDK.PayAddress(addressLine1: Optional("320 Main St "),
                                                      addressLine2: Optional("Apt 1"), city: Optional("Brooklyn"), country: Optional("United States"), province: Optional("New York"), zip: Optional("11217"), firstName: Optional("Zack"), lastName: Optional("Shapiro"), phone: Optional("4435555502"), email: nil)),
    shippingRate: Optional(MobileBuySDK.PayShippingRate(handle: "usps-Priority-8.25", title: "usps-Priority-8.25", price: 8.25, deliveryRange: nil)),
    currencyCode: "USD",
    subtotalPrice: 55,
    totalTax: 3.9875,
    paymentDue: 67.2375 // this value is right
)

In my completeCheckout function, I call mutationForCompleteCheckoutUsingApplePay

    func completeCheckout(_ checkout: PayCheckout, billingAddress: PayAddress, applePayToken token: String, idempotencyToken: String, completion: @escaping (Storefront.Payment?) -> Void) {
        let mutation = ClientQuery.mutationForCompleteCheckoutUsingApplePay(checkout, billingAddress: billingAddress, token: token, idempotencyToken: idempotencyToken)

Which calls

static func mutationForCompleteCheckoutUsingApplePay(_ checkout: PayCheckout, billingAddress: PayAddress, token: String, idempotencyToken: String) -> Storefront.MutationQuery {
        let mailingAddress = Storefront.MailingAddressInput.create(
            address1:  billingAddress.addressLine1.orNull,
            address2:  billingAddress.addressLine2.orNull,
            city:      billingAddress.city.orNull,
            country:   billingAddress.country.orNull,
            firstName: billingAddress.firstName.orNull,
            lastName:  billingAddress.lastName.orNull,
            province:  billingAddress.province.orNull,
            zip:       billingAddress.zip.orNull
        )

        print("paymentDue is", checkout.paymentDue) // *This value is correctly 67.2375*

        let currencyCode  = Storefront.CurrencyCode(rawValue: checkout.currencyCode)!
        let paymentAmount = Storefront.MoneyInput(amount: checkout.paymentDue, currencyCode: currencyCode)
        let paymentInput  = Storefront.TokenizedPaymentInputV2.create(
            paymentAmount:  paymentAmount,
            idempotencyKey: idempotencyToken,
            billingAddress: mailingAddress,
            type:           "apple_pay",
            paymentData:    token
        )

        return Storefront.buildMutation { $0
            .checkoutCompleteWithTokenizedPaymentV2(checkoutId: GraphQL.ID(rawValue: checkout.id), payment: paymentInput) { $0
                .checkoutUserErrors { $0
                    .field()
                    .message()
                }
                .payment { $0
                    .fragmentForPayment()
                }
            }
        }
    }

And yet I get this error and have no idea why. Maybe the error is a red herring to something else?

I'm on a deadline and super stressed this isn't working. Would love some help. Thanks!

zackshapiro commented 5 years ago

@dbart01 I'd love your help on this, I'm not sure if this is also an error from the server-side but I can't find locally where this is happening

zackshapiro commented 5 years ago

Could this have to do with TestMode being enabled?

zackshapiro commented 5 years ago

Got my answer in https://github.com/Shopify/mobile-buy-sdk-ios/issues/993