質問

I've posted a few related questions to this, but I'm still a bit lost in getting the implementation.

Essentially I need to translate this line from the Venmo API for making payments:

curl https://api.venmo.com/v1/payments -d access_token=4e4sw1111111111t8an8dektggtcbb45 -d email="someemail@gmail.com" -d amount=5 -d note="Delivery."

into my Rails site in a POST request form/format.

I have the following in my View right now for the form:

<!-- Venmo box -->
                    <div class="box box-warning">
                        <div class="box-header">
                            <%= form_tag ('/make_payment_path') do %>
                            <%= hidden_field_tag "access_token", @access_token %>
                          <h3 class="box-title">Pay Bill using Venmo Account</h3>
                            <div id="payment_note_input">
                            <%= text_field_tag "payment_note", nil, class:"form-control", placeholder: "add a note to your payment" %>
                            </div>
                            <div id="payment_target_input">
                                <%= text_field_tag "payment_target", nil, class:"form-control", placeholder: "pay an email address" %>
                            </div>
                            <div id="payment_amount_input">
                                <%= text_field_tag "payment_amount", nil, class:"form-control", placeholder: "enter the payment amount!"%>
                            </div>
                                </br>
                        <%= submit_tag "Make payment", class: "btn btn-danger" %>
                        <% end %>
                    </div>

And the following in my Dashboard controller that corresponds to the view:

def make_payment_path
if !params[:access_token].nil?

    uri = URI.parse("https://api.venmo.com/v1/payments")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri)
    request.set_form_data({"access_token" => params[:access_token], "email" => params[:payment_target],
     "amount" => params[:payment_amount], "note" => params[:payment_note]} )

    response = http.request(request)
end
end

Can anyone help point me in the right direction here? Once this POST request is processed through Venmo, it should return a JSON response, as seen here

役に立ちましたか?

解決

You don't specify what the error message is. :-)

I ran your code and the response.body had "400 The plain HTTP request was sent to HTTPS port"

Looks like you need to force SSL with:

http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER 

Example:

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri)

New code gets a json response:

{"error": {"message": "You did not pass a valid OAuth access token.", "code": 261}}

Assume adding in the correct access token will resolve the issue.

As a side note, you might also want to move the controller code into a Model

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top