How to update database based on a javascript function being called within my app

I am building functionality for accepting in app payments and am stuck with how to update a user once they have subscribed on a Google device.

Currently the user triggers a payment with some custom code within the onclick function of a button in the UI Builder.

This displays the in-app payment interface. If they complete the payment then the documentation I am using says the following will happen.

I am stuck with how to use this function to update the user record in the database so that I can display the functionality for a subscribed user.

Any suggestions or links to documentation / videos for how I could do this would be very helpful.

Thanks!

Hello @Luc_Zentar

If I can take a look at your custom onclick logic please, provide us with your app id, page name, and component path.
Could you provide a link to the documentation from the screenshot?

Regards

Hi Viktor,

The app ID is 8518B92E-A6AA-5589-FFA0-B863E4A63F00

Container is mobileApp

Page name is calculationPage

The button mentioned is in the modal >> modal__card >> buttonBlock >> confirmButton

The link for the documentation is Google In-App Purchases

Thank you

Note - the functionality isn’t live in the app yet and you’ll notice that the custom code has a placeholder destination for purchases using a Google device.

Hello Luc Zentar!

I’m not sure I understood you correctly, please correct me if I’m wrong.
If you want to update a user record, I think after calling the block with custom code, you can add a Save Object in Backendless block and update the user with their objectId.
Or is the problem something else?

Regards,
Alexander

Hello,

I’m not sure that will work.

My understanding of the flow is like this:

1 - the user clicks the button

2- the custom code executes and displays a Google Pay UI for the user to subscribe to my service using an in-app payment

3 - if the user completes this process then the app will call a JavaScript function on the page gonative_iap_purchases

4 - when i know the user has completed the in-app payment process, I update the database so they are shown the content for a subscribed user

After executing the custom code in step 2 I don’t know if the user has completed the payment process or not, so I can’t update their record at that point, otherwise any user that does not complete payment will still access subscriber content.

As I understand it, the gonative.iap.purchase function will be executed in the second step.
I couldn’t find information about what this function returns in the documentation, but it’s possible it returns information about whether the operation was successful. Based on that information, you could decide to update the record in the database.

Regards,
Alexander

Thank you, the gonative.iap.purchase may be executed just after the second step. But if the user does not complete payment, it won’t be executed.

When it is executed, I don’t understand how to access the information it returns.

Is it in UI builder or do I have to use cloud code?

I meant that you can assign the result of the function execution to a variable within the custom code block and inspect its content.
I have a guess as to how this works, but you’ll need to create a test page with minimal logic required for testing and test data for logging in (if needed).

Regards,
Alexander

Thank you, I’ll try this and let you know if it is the solution

I think, it would be better if you create a testing page because, upon further examination of your question, I doubt that the proposed option will work. I am more inclined to believe that gonative.iap.purchase, after succeeding, will simply call gonative_iap_purchases and not return anything.

Regards,
Alexander

That is what I think will happen as well, I don’t think it is returned as part of the call I make, but when the payment is made by the user it is then called separately.

I am not at a point where I can test this yet, but wanted to understand if there is a usual way to access a Javascript function that is called on the page when triggered by an outside event.

Yes, but you have to figure out how it gets there. For example, If it’s added to the global scope, for example, in the window object, we can wrap it in another function that modifies the user record:

const setUserId = () => console.log('setUserId!')

if (window.gonative_iap_purchases) {
  const tmp = window.gonative_iap_purchases

  window.gonative_iap_purchases = (...args) => {
    tmp(...args)
    
    setUserId(args)
  }
}


window.gonative_iap_purchases()

I believe you should explore this direction further.

Regards,
Alexander

1 Like

Hi Alexander,

I hope you can help with a related query, that will then allow me to test the solution you have provided above.

I’m using the GoNative shell for my app. They have added the in-app payments native plugin, which uses a Javascript bridge to allow the webApp to access functionality and data from the app. https://gonative.readme.io/docs/gonative-javascript-bridge

With in-app payments, their documentation asks me to define a function using JavaScript on page load but not to call it.

https://gonative.readme.io/docs/apple-iap

How do I access the data from this function?

The data should be in this format:

I’m trying this on page load, but it’s returning null

If the function is called by the app when the page loads, does the console.log mean that it will return the data in a way that can be accessed through UI Builder? Or do I need to do something else to access it?

Hi @Luc_Zentar

I’m not familiar with the GoNative, but seems like you can data you need by calling the following code

return gonative.iap.info()

in case you need to put a function into a global (window) scope you need to use the following code:

window.gonative_info_ready = function(...){
...
}

I assume GoNative calls it once it’s initialized, so make sure you register the function before inviting the library/module

@Alexander_Pavelko

Yes, but you have to figure out how it gets there. For example, If it’s added to the global scope, for example, in the window object, we can wrap it in another function that modifies the user record:

const setUserId = () => console.log('setUserId!')

if (window.gonative_iap_purchases) {
  const tmp = window.gonative_iap_purchases

  window.gonative_iap_purchases = (...args) => {
    tmp(...args)
    
    setUserId(args)
  }
}


window.gonative_iap_purchases()

I believe you should explore this direction further.

Regards,
Alexander

I’m working on this now, where would I put this code in UI Builder? Would it be in custom code on page load?

Hi there @Luc_Zentar,

That’s correct, you need to add this code to the Custom Code block. You will have to try different stages of page loading. As we cannot be sure when the library will add the functions.

Regards,
Marina