【问题标题】:Android Client to Server Node.js through Firebase for PaymentAndroid 客户端通过 Firebase 到服务器 Node.js 进行支付
【发布时间】:2017-07-12 15:30:45
【问题描述】:

我对所有这些东西有点困惑,我是一名 Android 开发人员,现在我需要在我的应用程序中集成支付,所以我使用了 paypal 网关,但我需要一个服务器,所以我使用 firebase 告诉我我需要使用node.js。我不知道,但我仍然尝试做我在网上找到的东西,这是我的代码,如果有人可以向我解释我错在哪里或者是否有最简单的方法!?谢谢!

public class MainActivity extends AppCompatActivity {

    private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;
    private static final String CONFIG_CLIENT_ID =
            "Here there is my actual client id";
    private static final int REQUEST_CODE_PAYMENT = 1;
    private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;
    private static final int REQUEST_CODE_PROFILE_SHARING = 3;
    private static final String TAG = MainActivity.class.getSimpleName();
    private static PayPalConfiguration config = new PayPalConfiguration()
            .environment(CONFIG_ENVIRONMENT)
            .clientId(CONFIG_CLIENT_ID)
            .merchantName("Example Merchant")
            .merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))
            .merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.buyItBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal("0.01"), "USD", "sample item",
                        PayPalPayment.PAYMENT_INTENT_SALE);
                Intent intent = new Intent(MainActivity.this, PaymentActivity.class);
                intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
                intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
                startActivityForResult(intent, REQUEST_CODE_PAYMENT);
            }
        });
        findViewById(R.id.futurePaymentBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, PayPalFuturePaymentActivity.class);
                intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
                startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);
            }
        });
        findViewById(R.id.profileSharingBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, PayPalProfileSharingActivity.class);
                intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
                intent.putExtra(PayPalProfileSharingActivity.EXTRA_REQUESTED_SCOPES, getOauthScopes());
                startActivityForResult(intent, REQUEST_CODE_PROFILE_SHARING);
            }
        });

        Intent intent = new Intent(this, PayPalService.class);
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
        startService(intent);
    }

    private PayPalOAuthScopes getOauthScopes() {
        Set scopes = new HashSet(Arrays.asList(PayPalOAuthScopes.PAYPAL_SCOPE_EMAIL, PayPalOAuthScopes.PAYPAL_SCOPE_ADDRESS));
        return new PayPalOAuthScopes(scopes);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PAYMENT && resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {
                    Log.i(TAG, confirm.toJSONObject().toString(4));
                    Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));
                    //TODO: envoyer 'confirm' et si possible confirm.getPayment() à votre server pour la vérification
                    Toast.makeText(getApplicationContext(), "PaymentConfirmation info received from PayPal", Toast.LENGTH_LONG).show();
                } catch (JSONException e) {
                    Log.e(TAG, "an extremely unlikely failure occurred: ", e);
                }
            }
        } else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT && resultCode == Activity.RESULT_OK) {
            PayPalAuthorization auth =
                    data.getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
            if (auth != null) {
                String authorization_code = auth.getAuthorizationCode();
                sendAuthorizationToServer(authorization_code);
            }
        } else if (requestCode == REQUEST_CODE_PROFILE_SHARING && resultCode == Activity.RESULT_OK) {
            PayPalAuthorization auth = data.getParcelableExtra(PayPalProfileSharingActivity.EXTRA_RESULT_AUTHORIZATION);
            if (auth != null) {
                String authorization_code = auth.getAuthorizationCode();
                sendAuthorizationToServer(authorization_code);
            }
        }
    }

    private void sendAuthorizationToServer(String auth) {

    }
}

这是我在函数文件夹中 index.js 中的代码:

var braintree = require("braintree");

var express = require('express'),
  app = express();

var gateway = braintree.connect({
  environment: braintree.Environment.Sandbox,
  merchantId: "actual merchand id",
  publicKey: "actual publicKey",
  privateKey: "actual privateKey"
});

var gateway = braintree.connect({
  accessToken: "actual accesstoken"
});

app.get("/client_token", function (req, res) {
  gateway.clientToken.generate({}, function (err, response) {
    res.send(response.clientToken);
  });
});

app.get("/checkout", function (req, res) {
  var nonce = req.body.payment_method_nonce;
  // Use payment method nonce here
});

如果缺少某些文件或代码,请告诉我,以及如何从我的 android 代码调用 index.js 文件中的函数? 谢谢!

【问题讨论】:

  • 应该发生什么以及发生了什么?

标签: javascript android node.js firebase


【解决方案1】:

您的 index.js 缺少一个端口,因为它在尝试添加这些行时运行

var port = '3333'; app.set('port', port);

如果您使用节点 js 在本地机器上运行,请尝试访问您的服务器 您将有一个名为 package.json 的文件,确保您在 dependencies 中有 braintreeexpress 然后运行 ​​@ 987654325@ 使用命令node index.js启动服务器

您应该能够使用

访问这两种 get 方法

http://localhost:3333/checkout 或 /client_token

【讨论】:

  • OK 坦克我会尽快尝试的,唯一的问题是我已经通过 firebase 获得了一个 http 地址,这会改变什么吗?
  • 不,只要您的服务器正在运行,它就不应该是相同的概念,请检查此链接firebase.google.com/docs/hosting/quickstart
  • 谢谢,但它不起作用,当我尝试通过 android 访问它时,它说:java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3333) after 10000ms :isConnected 失败:ECONNREFUSED(连接被拒绝)!另外,当我点击链接localhost:3333/checkout 时,我最终无处可去,这正常吗?
【解决方案2】:

我有两个函数,一个在这里工作,另一个不工作,你能解释一下为什么吗,bigben 不能工作,另一个是 app.get: functions 等于 firebase-functions !

exports.bigben = functions.https.onRequest((req, res) => {
  const hours = (new Date().getHours() % 12) + 1 // london is UTC + 1hr;
  res.status(200).send(`<!doctype html>
    <head>
      <title>Time</title>
    </head>
    <body>
      ${'BONG '.repeat(hours)}
    </body>
  </html>`);
});

app.get("/client_token", function(req, res) {
  gateway.clientToken.generate({}, function (err, response) {
    res.send(response.clientToken);
  });
});

所以我尝试将第二个重写为第一个,但我最终得到了那个,并且在不知道 js 的情况下我知道这里有问题:

exports.tokenclient = functions.https.onRequest((req, res) =>{
  res.send(response.clientToken);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多