簡體   English   中英

如何使用SQLite在Android App中實現“服務器端”條帶代碼

[英]How to implement 'server side' Stripe code into Android App using SQLite

我能夠使用Stripes Android Walkthrough創建令牌。 但是現在我想創建一個客戶並最終產生一個費用,他們會在這里顯示如何在服務器上做。 但是我在數據庫中使用SQLite,因為該應用程序僅由少數人使用,並且僅由我加載到單個平板電腦上。 所以我的問題是我可以在應用程序中創建客戶而不必在應用程序中構建整個服務器端后端嗎?

這是我創建令牌的代碼,現在我要使用該令牌創建客戶:(忽略應用程序的混亂情況,對不起,最終的應用程序將更加干凈,只是嘗試查看我是否可以使它正常工作)

package com.zeuspwr.zeuspower;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.stripe.android.*;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
import com.stripe.android.Stripe;

import java.util.HashMap;
import java.util.Map;

public class newUser extends AppCompatActivity {
    EditText fNameh;
    EditText lNameh;
    EditText emailh;
    EditText phoneNumh;
    EditText pinh;
    EditText cardNumberh;
    EditText cardCVCh;
    Spinner cardExpMonthh;
    Spinner cardExpYearh;
    String fname;
    String lname;
    String email;
    String phonenum;
    String pin;
    String cardNumber;
    String cardCVC;
    Integer cardExpMonth;
    Integer cardExpYear;
    String stripetok;
    Card cardNew;

    String cardExpMonthStr;
    String cardExpYearStr;

    private static final String PUBLISHABLE_KEY = "pk_test_iUVdhdvJuurqSxIlXpzq32LS";
    UserDBHelper newUserr;

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

        newUserr = new UserDBHelper(newUser.this);

        fNameh = (EditText) findViewById(R.id.fNameNew);
        lNameh = (EditText) findViewById(R.id.lNameNew);
        emailh = (EditText) findViewById(R.id.emailNew);
        phoneNumh = (EditText) findViewById(R.id.phoneNew);
        pinh = (EditText) findViewById(R.id.pinNew);
        cardNumberh = (EditText) findViewById(R.id.cardNumNew);
        cardCVCh = (EditText) findViewById(R.id.cvcNew);
        cardExpMonthh = (Spinner) findViewById(R.id.monthNew);
        cardExpYearh = (Spinner) findViewById(R.id.yearNew);


        ImageButton createNewUser = (ImageButton) findViewById(R.id.createNew);



        createNewUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                fname = fNameh.getText().toString();
                lname = lNameh.getText().toString();
                email = emailh.getText().toString();
                phonenum = phoneNumh.getText().toString();
                pin = pinh.getText().toString();
                cardNumber = cardNumberh.getText().toString();
                cardCVC = cardCVCh.getText().toString();
                cardExpMonthStr = cardExpMonthh.getSelectedItem().toString();
                cardExpYearStr = cardExpYearh.getSelectedItem().toString();
                cardExpMonth = Integer.valueOf(cardExpMonthStr);
                cardExpYear = Integer.valueOf(cardExpYearStr);

                cardNew = new Card(
                        cardNumber,
                        cardExpMonth,
                        cardExpYear,
                        cardCVC
                );

                if(cardNew.validateCard() & cardNew.validateCVC()) {
                    final Stripe stripe = new Stripe();
                    stripe.createToken(cardNew, PUBLISHABLE_KEY, new TokenCallback() {
                        public void onSuccess(Token token) {
                            // TODO: Send Token information to your backend to initiate a charge


                            Toast.makeText(
                                    getApplicationContext(),
                                    "Token created: " + token.getId(),
                                    Toast.LENGTH_LONG).show();
                        }

                        public void onError(Exception error) {
                            Log.d("Stripe", error.getLocalizedMessage());
                        }
                    });

                }

                else{
                    Toast.makeText(
                            getApplicationContext(),
                            "Token not made!",
                            Toast.LENGTH_LONG).show();
                }



                /*Intent retPage = new Intent(BorroworReturn.this, returno.class);

                startActivity(retPage);*/
            }
        });

不幸的是,由於這些調用需要您的Secret API密鑰,因此無法在Android應用程序中執行任何操作。 您永遠不要在Android應用程序中擁有Secret API密鑰,否則攻擊者可能會親手使用它,然后代您創建費用,退款或轉移。

您需要做的是先創建卡令牌(已完成),然后將其發送到服務器,您將在其中使用您的密鑰創建費用或客戶。 在您的Android應用程序中什么都不會發生。

您可以參考另一個問題: 使用Android發送帶區令牌的正確方法,以檢查有關如何將令牌發送到服務器的一些Android代碼。

在服務器端,您需要檢索令牌,然后使用它來創建ChargeCustomer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM