我通過作爲客戶端的Android應用程序調用Rails REST API來創建新客戶。Stripe :: InvalidRequestError(No such token:tok _ ***)
我的Rails服務器有一個Charges控制器。在該控制器(app/controllers/charges_controller.rb
),我有以下方法:
def createPaymentInfo
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
render :json => customer
end
這種方法公開https://my-url.com/charges/createPaymentInfo
與我在config/routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :charges
resources :users
root 'static_pages#home'
get '/login', to: 'static_pages#login'
get '/register', to: 'users#new'
get '/portal', to: 'static_pages#portal'
match '/charges/createPaymentInfo' => 'charges#createPaymentInfo', via: :post
end
我還設置做了我測試密鑰和測試可發佈的關鍵環境變量在我的服務器上設置。
然後在我的Android應用程序中,我創建了一個HTTP POST。
stripe = null;
try {
stripe = new Stripe("pk_test_***");
stripe.createToken(card, new TokenCallback() {
public void onSuccess(Token token) {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(CreditCardActivity.this);
String url = "https://my-url/charges/createPaymentInfo?"
+ "stripeToken=" + token.getId()
+ "&stripeEmail=" + userManager.getUser().getEmail();
Log.d("URL", url);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String responseJSON) {
// your response
Log.d("HTTP-RESPONSE", responseJSON);
try {
JSONObject obj = new JSONObject(responseJSON);
String stripeCustId = obj.get("id").toString();
Log.d("STRIPE-RESPONSE", obj.toString());
AuthenticatedUser user = AuthenticatedUserManager.getInstance().getUser();
user.setStripeCustId(stripeCustId);
userManager.setUser(user);
Toast.makeText(CreditCardActivity.this, "Card saved successfully!", Toast.LENGTH_SHORT).show();
mDatabase.child("users").child(mFirebaseAuth.getCurrentUser().getUid()).child("stripeCustId").setValue(stripeCustId);
startActivity(new Intent(CreditCardActivity.this, SettingsActivity.class));
} catch (Throwable t) {
Log.e("FROM OBJ", "Could not parse malformed JSON: \"" + responseJSON + "\"");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
Toast.makeText(CreditCardActivity.this,
"Something happened with your payment method.\n" +
"Please make sure you're info is correct",
Toast.LENGTH_SHORT).show();
}
}
);
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
public void onError(Exception error) {
// Show localized error message
Toast.makeText(CreditCardActivity.this, error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
}
);
} catch (AuthenticationException e) {
e.printStackTrace();
}
但是我收到以下錯誤的Heroku:
2017-02-14T22:37:50.658271+00:00 app[web.1]: I, [2017-02-14T22:37:50.658187 #4] INFO -- : [263c291d-a442-4ec1-ab80-c4d9b5a10960] Started POST "/charges/createPaymentInfo?stripeToken=tok_***&[email protected]" for 24.34.87.34 at 2017-02-14 22:37:50 +0000
2017-02-14T22:37:50.659644+00:00 app[web.1]: I, [2017-02-14T22:37:50.659593 #4] INFO -- : [263c291d-a442-4ec1-ab80-c4d9b5a10960] Processing by ChargesController#createPaymentInfo as HTML
2017-02-14T22:37:51.241972+00:00 app[web.1]: I, [2017-02-14T22:37:51.241865 #4] INFO -- : [263c291d-a442-4ec1-ab80-c4d9b5a10960] Completed 500 Internal Server Error in 582ms
2017-02-14T22:37:51.243029+00:00 app[web.1]: F, [2017-02-14T22:37:51.242973 #4] FATAL -- : [263c291d-a442-4ec1-ab80-c4d9b5a10960]
2017-02-14T22:37:51.243067+00:00 app[web.1]: F, [2017-02-14T22:37:51.243030 #4] FATAL -- : [263c291d-a442-4ec1-ab80-c4d9b5a10960] Stripe::InvalidRequestError (No such token: tok_***):
2017-02-14T22:37:51.243100+00:00 app[web.1]: F, [2017-02-14T22:37:51.243068 #4] FATAL -- : [263c291d-a442-4ec1-ab80-c4d9b5a10960]
2017-02-14T22:37:51.243136+00:00 app[web.1]: F, [2017-02-14T22:37:51.243106 #4] FATAL -- : [263c291d-a442-4ec1-ab80-c4d9b5a10960] app/controllers/charges_controller.rb:9:in `createPaymentInfo'
2017-02-14T22:37:51.242058+00:00 heroku[router]: at=info method=POST path="/charges/createPaymentInfo?stripeToken=tok_***&[email protected]" host=my-url.com request_id=263c291d-a442-4ec1-ab80-c4d9b5a10960 fwd="24.34.87.34" dyno=web.1 connect=0ms service=587ms status=500 bytes=1669
爲什麼是我通過Java的stripe.createToken()
導致Stripe::InvalidRequestError (No such token: tok_***)
創建我的客戶令牌?