2017-01-31 5 views
0

ストライプ充電を行うために、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コードを変更する必要がありますか?

+0

@Selvin私が間違っていると私を修正しますが、 'getBody()'はサーバーからの応答を解析するためのVolley.Requestのメソッドです。私はこれが私が得ている404の応答を説明するとは思わない。今は、応答を解析する前にサーバーに正しくポストしていることを確認したいだけです。 – beckah

+0

あなたは間違っています、要求は解析されていません...それは、まあ、体を送信します.... – Selvin

+1

Volleyは、そのURLをヒットしようとすると、Railsが戻ってきたものである404を取得しようとしています。したがって、あなたの料金コントローラのエンドポイントへの正しい経路を打ち負かしてはいけません。 –

答えて

1

物事のカップル:

  • まず、あなたのAndroidアプリは、あなたがチェックRailsのフォームの信憑性を無効にする必要があります当たっている。このようなAPIエンドポイントのため。 skip_before_action :verify_authenticity_token

  • VolleyさんはRailsアプリからHTTP 404を取得しているので、Androidアプリ自体とはまったく関係ありません。あなたのAPIエンドポイントへの間違ったURL /パスを打つことになります

  • デフォルトでは、RailsはHTTP POSTを受信したときにコントローラー上で自動的にcreateアクションを呼び出します - 実際には実行したいと思います

https://agile-chamber-46872.herokuapp.com/charges

とレールに対してHTTPのPOSTは、ルート内部createアクションになること - あなたは明示的にAndroidのHTTPリクエストでこれを指定する必要はありません。

は、私はそれがカールを経由して、あなたのAPIを叩くあなたのHTTP 404

の真の源であるこの後のポイントだと考えています。HTTP 500で

$ curl -XPOST "https://agile-chamber-46872.herokuapp.com/charges?stripeToken=tok_abc&[email protected]" 

結果 - 404ではないので、我々どこかに行くよ。 Railのログでエラーの原因を確認してください。 PapertrailのようなHerokuアドオンを有効にすることを検討してください。

+0

これはまさにそれでした! Charges#create viewのHTMLを取得できました。私はストライプから得ているレスポンスを得ることができるかどうか調べるために打ち続けるつもりですが、これは非常に役に立ちました。ありがとうございました! – beckah

関連する問題