2017-07-12 94 views
0

我對所有這些東西有點困惑,我是一名android開發人員,現在我需要在我的應用中集成付款,所以我使用了貝寶網關,但我需要一個服務器,所以我用firebase告訴我,我需要使用node.js.我不知道,但我仍然試圖做我在網上找到的東西,這裏是我的代碼,如果有人能向我解釋我錯在哪裏,或者有最簡單的方法!謝謝!Android客戶端到服務器Node.js通過Firebase付款

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 
}); 

告訴我,如果某些文件或代碼丟失,還我怎麼調用一個函數在我的index.js文件從我的android代碼?? 謝謝!

+0

什麼應該發生的和正在發生的事情? –

回答

0

您index.js缺少端口,它嘗試運行這些行添加

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

嘗試訪問您的服務器,如果u的本地機器上的節點JS 你將有一個文件稱爲運行作爲的package.json確保u有布倫特裏表達依賴然後運行npm install 使用命令node index.js使用

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

+0

OK坦克生病嘗試儘快,唯一的事情是,我已經有一個由firebase的http地址是改變什麼? –

+0

不,它不應該只要你的服務器運行的概念是一樣的檢查此鏈接https://firebase.google.com/docs/hosting/quickstart –

+0

謝謝,但它不工作,當我試圖通過android訪問它它說:java.net.ConnectException:10000ms後無法連接到本地/ 127.0.0.1(端口3333):isConnected失敗:ECONNREFUSED(連接被拒絕)!另外當我點擊鏈接http:// localhost:3333/checkout我最終無處可去,這是正常的嗎? –

0

啓動服務器

您應該能夠訪問這兩個get方法,我有兩個功能,一個工作不是這裏的另外一個他們,你能解釋我爲什麼,bigben不是另一個app.get: 函數等於firebase函數!

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); 
});