Search code examples
ioscordovacapacitor

iOS how to get the user id in the receipt returned from apple server-to-server notification


This really boils down to iOS and the apple receipt server-to-server notifications but I have a capacitor app using the cordova-plugin-purchase plugin. Im trying to set the user id somehow so i can match up the purchase to a user when i get my purchase receipt on my webhook from apple. I have tried to set the user id the best i could figure out like this:

store.applicationUsername = UUID;

I tried a UUID, string, etc and nothing seems to work. I have set it in multiple places along the flow also with no luck. The resonse i get back from apple for my purchase never includes the user id:

{
"transactionId": "2000000725875555",
"originalTransactionId": "2000000725875555",
"bundleId": "com.example",
"productId": "product1",
"purchaseDate": 1727338993000,
"originalPurchaseDate": 1727338993000,
"quantity": 1,
"type": "Consumable",
"inAppOwnershipType": "PURCHASED",
"signedDate": 1727339027581,
"environment": "Sandbox",
"transactionReason": "PURCHASE",
"storefront": "USA",
"storefrontId": "555555",
"price": 5000,
"currency": "USD"
}

I also tried setting it in the order() function with no luck:

await offer.order({
     applicationUsername: '9e7ee220aced5367585f2b048f97b33a',
});

Im beginning to wonder if im doing this right at all. I dont see how people are matching up purchases with users from a purchase receipt. The only thing left for me to do is to grab the purchase and send it to my backend before every purchase to match on that. That is just more traffic to my server that i was trying to avoid though.

At this point any suggestions or examples on what other people do what be greatly appreciated!


Solution

  • For anyone else encountering this, for iOS the applicationUsername HAS to be a UUID (v4 i think). The applicationUsername variable in the CDVPurchase HAS to also be callable. Then if properly set it will return the uuid like so in the server-to-server notification from apple like this:

    "appAccountToken":"4758a858-26cc-47a9-ae79-1ad8254b96ea"
    

    I set it like this:

    store.applicationUsername = () => user.uuid
    

    Also to note this works in sandbox also.

    Dont be misled by the string type in here in the store.ts. It does not work as a string:

    public applicationUsername?: string | (() => string | undefined)