我通过作为客户端的Android应用程序调用Rails REST api来创建新客户。
My 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_***&stripeEmail=rebeccasheeler@gmail.com" 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_***&stripeEmail=rebeccasheeler@gmail.com" 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_***)
?
答案 0 :(得分:0)
您希望确保您为Android应用和Rails应用使用的API密钥都是针对相同的条带帐户,并且都在测试模式下。最可能的原因是他们不是。