Hello,
I’m currently trying to integrate PayPal into my Anvil app. I have a custom HTML file that contains the PayPal code and it is being used within a form (form2) that holds the PayPal code and other elements like buttons. My app has a startup form (form1) that is separate from form2.
I’m trying to call a server function from inside the custom HTML of form2, specifically within the PayPal code, so that I can flag a user as paid after a successful transaction. However, I’m receiving a script error when I try to do this. I’ve included the code I’m using within the onApprove function below:
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
if (anvil.users.logged_in()){
var user = anvil.users.get_user();
anvil.server.call('mark_user_as_paid',
{order_id: data.orderID, user_email: user["email"]})
}
});
}
This is the server function:
@anvil.server.callable
def mark_user_as_paid(order_id, user_email):
user = anvil.users.get_user()
if user == None:
user = anvil.users.login_with_form()
else:
user_row = app_tables.users.get(uuid=user_email)
user_row['paid'] = True
user_row.update()
print("Transaction successful")
I’m sorry if my questions aren’t making sense or if I haven’t provided enough details. I’m a bit lost right now and would really appreciate the help.