ストライプ充電を行うために、AndroidアプリケーションからサーバーにHTTP-POSTを行うのが難しいです。ストライプ用のAndroidクライアントからサーバーへの課金
tutorial found on the Stripe websiteに従うと、My Stripeサーバーは正常に動作します。私の仕事のアプリケーションcan be found here。私は電荷を作成するために自分のサーバーに自分のトークンを渡すためにしようとしています
class ChargesController < ApplicationController
protect_from_forgery
def new
end
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
:
ちょうど洞察力として、私の料金コントローラは次のようになります。しかし、それは次のようなエラーが生じている:
E/Volley: [714] BasicNetwork.performRequest: Unexpected response code 404 for https://agile-chamber-46872.herokuapp.com/charges/create?stripeToken=tok_*************&[email protected]
これは私が私のサーバーにHTTPのPOSTをやろうとしています私のアンドロイドコードです:
パッケージcom.snapwebdevelopment.scanhappy。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.stripe.android.*;
import com.braintreepayments.cardform.view.CardForm;
import com.stripe.android.exception.AuthenticationException;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.android.volley.Response;
import static java.security.AccessController.getContext;
public class CreditCardActivity extends AppCompatActivity {
private CardForm cardForm;
private Button makePaymentButton;
private Stripe stripe;
private HashMap<String, Object> chargeInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_credit_card);
makePaymentButton = (Button) findViewById(R.id.makePaymentButton);
cardForm = (CardForm) findViewById(R.id.card_form);
cardForm.cardRequired(true)
.expirationRequired(true)
.cvvRequired(true)
.postalCodeRequired(true)
.mobileNumberRequired(true)
.mobileNumberExplanation("SMS is required on this number")
.actionLabel("Purchase")
.setup(this);
makePaymentButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
validateCard();
}
});
}
private void validateCard(){
chargeInfo = new HashMap<>();
Card card = new Card(
cardForm.getCardNumber(),
Integer.valueOf(cardForm.getExpirationMonth()),
Integer.valueOf(cardForm.getExpirationYear()),
cardForm.getCvv()
);
card.validateNumber();
card.validateCVC();
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://agile-chamber-46872.herokuapp.com/charges/create?stripeToken=" + token.getId() + "&[email protected]";
// 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 response) {
// your response
Log.d("HTTP-RESPONSE", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
}
}){
@Override
public byte[] getBody() throws AuthFailureError {
String your_string_json = ""; // put your json
return your_string_json.getBytes();
}
};
// 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();
}
}
private void chargeUser() {
}
}
私はどこかで結婚していると知っています。 /create
でサーバのREST APIに正しくPOSTするためにAndroidコードを変更する必要がありますか?
@Selvin私が間違っていると私を修正しますが、 'getBody()'はサーバーからの応答を解析するためのVolley.Requestのメソッドです。私はこれが私が得ている404の応答を説明するとは思わない。今は、応答を解析する前にサーバーに正しくポストしていることを確認したいだけです。 – beckah
あなたは間違っています、要求は解析されていません...それは、まあ、体を送信します.... – Selvin
Volleyは、そのURLをヒットしようとすると、Railsが戻ってきたものである404を取得しようとしています。したがって、あなたの料金コントローラのエンドポイントへの正しい経路を打ち負かしてはいけません。 –