菜鸟教程小白 发表于 2022-12-12 23:54:24

javascript - 云代码无效功能的back4app问题


                                            <p><p>我的问题是:我必须在带有 conekta 的 iOS 应用程序中实现一些支付,我的服务器在 back4app 中运行得很好,之前我只有云中的 Braintree 功能,现在我已经搬到了科内克塔back4app 调用不断失败 iOS 给了我错误 Invalid function ,我已经从我为 Braintree 的 sintax 中复制了它,我在 Node.js 或 js 方面并不擅长。我用于云的代码:</p>

<pre><code>// Requiring npm module
var braintree = require(&#34;braintree&#34;);
var conekta = require(&#39;conekta&#39;);
conekta.api_key = &#39;key_eYvWV7gSDkNYXsmr&#39;;
conekta.api_version = &#39;2.0.0&#39;;

//conektastuff
// Function for conekta
Parse.Cloud.define(&#34;conektaCustomer&#34;, function (request, response) {
var name = request.params.name;
var email = request.params.email;
var phone = request.params.phone;
var card = &#39;card&#39;;
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({
    &#39;name&#39;: name,
    &#39;email&#39;: [email protected]&#39;,
    &#39;phone&#39;: phone,
    &#39;payment_methods&#39;: [{
      &#39;type&#39;: card,
      &#39;token_id&#39;: token_id
    }]
}, function(err, res) {
      console.log(res.toObject());
    });

    order = conekta.Order.create({
    &#34;line_items&#34;: [{
      &#34;name&#34;: &#34;Lavado&#34;,
      &#34;unit_price&#34;: lavado,
      &#34;quantity&#34;: 12
    }],
    &#34;shipping_lines&#34;: [{
      &#34;amount&#34;: 0,
      &#34;carrier&#34;: &#34;Automozo&#34;
    }],
    &#34;currency&#34;: &#34;MXN&#34;,
    &#34;customer_info&#34;: {
   &#34;customer_id&#34;: customerID.id
    },
    &#34;shipping_contact&#34;:{
   &#34;phone&#34;: customerID.phone,
   &#34;receiver&#34;: customerId.name,
   &#34;address&#34;: {
       &#34;street1&#34;: &#34;Automozo&#34;,
       &#34;city&#34;: &#34;Monterrey&#34;,
       &#34;state&#34;: &#34;Nuevo Leon&#34;,
       &#34;country&#34;: &#34;MX&#34;,
       &#34;postal_code&#34;: &#34;64630&#34;,
       &#34;residential&#34;: true
   }
   },
   &#34;amount&#34;: lavado,
&#34;charges&#34;:[{
    &#34;payment_method&#34;: {
      &#34;token_id&#34;: token_id,
      &#34;type&#34;: &#34;card&#34;
    }
}]
}, function(err, res) {
      console.log(res.toObject());
});

});


// Initializing gateway
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: &#34;rw4ty7nn96t8kzcz&#34;,
publicKey: &#34;qd77d84sy3836qbx&#34;,
privateKey: &#34;8fc74c88de4a7b8acf417aadfb92f6c5&#34;
});

// Function for client token generation
Parse.Cloud.define(&#34;generateToken&#34;, 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(&#34;checkout&#34;, 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(&#34;newCutomer&#34;, 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(&#34;createWithNonce&#34;, function (request, response) {
var clientNew = request.params.clientId;
var nonceFromTheClient = request.params.payment_method_nonce;
console.log(&#34;here&#34;);
console.log(nonceFromTheClient);
gateway.customer.create({
firstName: &#34;Charity&#34;,
lastName: &#34;Smith&#34;,
paymentMethodNonce: nonceFromTheClient
}, function (err, result) {
result.success;
// true

result.customer.id;
// e.g 160923
console.log(result.customer.id);

result.customer.paymentMethods.token;
// e.g f28wm
    if (err) {
      console.log(err);
      response.error(err);
    } else {
      response.success(result.customer.id);
    }
});
});
</code></pre>

<p>在 iOS 中:</p>

<pre><code>func makeCardPayment() {

      do {

            try PFCloud.callFunction(&#34;conektaCustomer&#34;, withParameters: [&#34;name&#34;:nameUser, &#34;email&#34;: &#34;[email protected]&#34;, &#34;phone&#34;:numeroDeTelefono, &#34;tokenID&#34;:&#34;tok_test_visa_4242&#34;, &#34;total&#34;:monto])

      } catch let error {

            print(error.localizedDescription)

      }
    }
</code></pre>

<p>它只是抛出无效函数,仅此而已,有什么帮助吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>所以,我在这里检查了函数:“conektaCustomer”中的第 21 行,您需要添加引号,如下行:</p>

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var customerID = conekta.Customer.create({
    &#39;name&#39;: name,
    &#39;email&#39;: &#39;[email protected]&#39;,
    &#39;phone&#39;: phone,
    &#39;payment_methods&#39;: [{
      &#39;type&#39;: card,
      &#39;token_id&#39;: token_id
    }]
}, function(err, res) {
      console.log(res.toObject());
    });</code></pre>

                                                <p style="font-size: 20px;">关于javascript - 云代码无效功能的back4app问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42322280/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42322280/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - 云代码无效功能的back4app问题