My issue is this: I have to implement some payment in an app iOS with conekta, I have the server running in back4app nicely before when I had only functions for braintree in the cloud code was playing nicely now that I have moved to conekta. The back4app calls fails constantly iOS gives me the error Invalid function and I have copied it from the sintax I had for the braintree ones, I'm not good in Node.js or js for that matter. the code I'm using for cloud:
// Requiring npm module
var braintree = require("braintree");
var conekta = require('conekta');
conekta.api_key = 'key_eYvWV7gSDkNYXsmr';
conekta.api_version = '2.0.0';
//conekta stuff
// Function for conekta
Parse.Cloud.define("conektaCustomer", function (request, response) {
var name = request.params.name;
var email = request.params.email;
var phone = request.params.phone;
var card = 'card';
var token_id = request.params.tokenId;
var lavado = request.params.total;
console.log(name + email + phone + card + token_id + lavado);
var customerID = conekta.Customer.create({
'name': name,
'email': [email protected]',
'phone': phone,
'payment_methods': [{
'type': card,
'token_id': token_id
}]
}, function(err, res) {
console.log(res.toObject());
});
order = conekta.Order.create({
"line_items": [{
"name": "Lavado",
"unit_price": lavado,
"quantity": 12
}],
"shipping_lines": [{
"amount": 0,
"carrier": "Automozo"
}],
"currency": "MXN",
"customer_info": {
"customer_id": customerID.id
},
"shipping_contact":{
"phone": customerID.phone,
"receiver": customerId.name,
"address": {
"street1": "Automozo",
"city": "Monterrey",
"state": "Nuevo Leon",
"country": "MX",
"postal_code": "64630",
"residential": true
}
},
"amount": lavado,
"charges":[{
"payment_method": {
"token_id": token_id,
"type": "card"
}
}]
}, function(err, res) {
console.log(res.toObject());
});
});
// Initializing gateway
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: "rw4ty7nn96t8kzcz",
publicKey: "qd77d84sy3836qbx",
privateKey: "8fc74c88de4a7b8acf417aadfb92f6c5"
});
// Function for client token generation
Parse.Cloud.define("generateToken", function(request, response) {
var clientId = request.params.clientId;
console.log(clientId)
gateway.clientToken.generate({
customerId: clientId
}, function (err, res) {
if (err) {
console.log(err);
response.error(err);
} else {
response.success(res.clientToken);
}
});
});
// Function for checkout
Parse.Cloud.define("checkout", function (request, response) {
var nonce = request.params.payment_method_nonce;
var amountPayed = request.params.amount;
var clientId = request.params.customerId;
console.log(clientId);
console.log(nonce);
console.log(amountPayed);
// Use payment method nonce here, for example:
gateway.transaction.sale({
amount: amountPayed, // $ 10.00 sale
paymentMethodNonce: nonce,
customerId: clientId,
options: {
submitForSettlement: true
}
}, function (err, result) {
if (err) {
response.error(err);
} else {
response.success();
}
});
});
//create empty customer id
Parse.Cloud.define("newCutomer", function (request, response) {
gateway.customer.create({}, function (err, result) {
if (err) {
console.log(err);
response.error(err);
} else {
console.log(result.customer.id);
response.success(result.customer.id);
}
});
});
//create new customer for cc save?
Parse.Cloud.define("createWithNonce", function (request, response) {
var clientNew = request.params.clientId;
var nonceFromTheClient = request.params.payment_method_nonce;
console.log("here");
console.log(nonceFromTheClient);
gateway.customer.create({
firstName: "Charity",
lastName: "Smith",
paymentMethodNonce: nonceFromTheClient
}, function (err, result) {
result.success;
// true
result.customer.id;
// e.g 160923
console.log(result.customer.id);
result.customer.paymentMethods[0].token;
// e.g f28wm
if (err) {
console.log(err);
response.error(err);
} else {
response.success(result.customer.id);
}
});
});
And in iOS:
func makeCardPayment() {
do {
try PFCloud.callFunction("conektaCustomer", withParameters: ["name":nameUser, "email": "[email protected]", "phone":numeroDeTelefono, "tokenID":"tok_test_visa_4242", "total":monto])
} catch let error {
print(error.localizedDescription)
}
}
It just throws invalid function, nothing more, any help?
So, I checked here and at the line: 21 from the function: "conektaCustomer", you'll need add a quotation marks, like the line below:
var customerID = conekta.Customer.create({
'name': name,
'email': '[email protected]',
'phone': phone,
'payment_methods': [{
'type': card,
'token_id': token_id
}]
}, function(err, res) {
console.log(res.toObject());
});
If you continue to get this invalid function problem, will need to investigate further.