I am trying to send a post request to a separate app where I need to send a matching signature that is not json encoded and the data, which is json encoded. I keep getting errors when I try to do this, so I'm just not understanding how to format it, and I can't find any examples on SO or their documentation (or anywhere else on the web).
signature = create_signature
result = HTTParty.post("weburl.com/file/to/post_to.php",
:body => {[ signature,
:data => { :timestamp => #message.created_at,
:url => company.request_host,
:name => #message.name,
:email => #message.email,
:phone => #message.phone,
:product => #message.service,
:comments => #message.message
}.to_json
]},
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
:verify => false )
The error I'm getting currently is syntax error, unexpected '}', expecting => ]}, ^
I've also tried without the arrays, without the data array, etc.
How do I format this to properly submit this info?
I think you need to move your .to_json to the end of the object.
:body => {[ signature,
:data => { :timestamp => #message.created_at,
:url => company.request_host,
:name => #message.name,
:email => #message.email,
:phone => #message.phone,
:product => #message.service,
:comments => #message.message
}
].to_json},
Related
I have my site in ruby on rails and for subscription payments I am using the paypal paypal-sdk-rest gem. But I need the first payment to have a lower value and then the next payment to have the normal value so that users can subscribe and test the subscription. I have been reading in the paypal api and a trial with a lower value can be applied. Is it possible to apply trial to the paypal gem paypal-sdk-rest? this is my code of how I create the plans:
def creacion_planes
plan = Plan.new({
:name => 'Monthly $20',
:description => '$20 plan (monthly charge)',
:type => 'infinite',
:payment_definitions => [{
:name => 'Plan monthly $20',
:type => 'REGULAR',
:frequency_interval => '1',
:frequency => 'MONTH', #WEEK, DAY, YEAR, MONTH.
:cycles => '0',
:amount => {
:currency => 'USD',
:value => '20.00'
}
}],
:merchant_preferences => {
:return_url => complete_paypal_checkouts_planes_url,
:cancel_url => update_plan_url,
:max_fail_attempts => '0',
:auto_bill_amount => 'YES',
:initial_fail_amount_action => 'CONTINUE'
}
})
# Create plan
if plan.create
# Plan update activation object
plan_update = {
:op => 'replace',
:path => '/',
:value => {
:state => 'ACTIVE'
}
}
# Activate plan
if plan.update(plan_update)
puts("Billing plan activated with ID [#{plan.id}]")
redirect_to success_payment_path(:plan_id => plan.id, :plan => "plan_mensual20")
else
logger.error payment.error.inspect
end
else
logger.error payment.error.inspect
end
end
And this is my code of how I create the agreement:
def create
agreement = Agreement.new({
:name => params[:name],
:description => params[:description],
:start_date => (Time.now + 20*60).utc.iso8601, #'2020-04-26T23:00:04Z' (Time.now + 1.days).utc.iso8601
:plan => {
:id => params[:plan_id]
},
:payer => {
:payment_method => 'paypal'
}
})
if agreement.create
redirect = agreement.links.find{|v| v.rel == "approval_url" }.href
redirect_to redirect
else
logger.error agreement.error.inspect
end
end
Do not use that old paypal-ruby-sdk. It is obsolete and uses an old version of billing plans, which is not compatible with the current subscriptions API.
Instead, integrate the subscriptions API via direct HTTPS calls. Here is the guide: https://developer.paypal.com/docs/subscriptions/
An example with a trial period is provided.
I made a post request with RestClient::Request.execute, which works, but sometimes it ended with a 422(Unprocessable Entity).
Afterwards I tried out RestClient.post which didnĀ“t gave me the 422 and worked all the time like a charm.
What is the difference between the two Calls?
I know that with RestClient::Request there are more possibilities for using parameters than with RestClient.post. I do not understand why i get a 422 with one method and not with the other.
Here I used json:
response = RestClient::Request.execute(
:method => :post,
:url => 'http://localhost:3000',
:timeout => 30,
:open_timeout => 2,
:payload => payload.to_json,
:headers => {
:content_type => :json,
:accept => :json
}
)
vs.
response = RestClient.post('http://localhost:3000',
:param1 => 'abc',
:param2 => "def")
They are the same - RestClient.post is a syntax sugar for execute, see restclient's sources, restclient.rb:
def self.post(url, payload, headers={}, &block)
Request.execute(:method => :post,
:url => url,
:payload => payload,
:headers => headers, &block)
end
The 422 is caused by something else
I've tested my controller and have got strange errors like this:
expected: ("376")
got: (376)
Please stub a default value first if message might be received with other args as well.
This is my spec:
it 'should send confirm-email if information is good' do
sign_in user
allow(Order).to receive(:find).with(order.id.to_s).and_return(order)
allow(order).to receive(:finalize) {order}
allow(order.errors).to receive(:empty?) {true}
expect(OrderMailer).to receive_message_chain(:send_finish_notification, :deliver)
patch :save_order, {:id => order.id , :order => {:street_address => 'Baker street', :apt => '123#', :zip_id => zip.id, :frequency_id => frequency.id, :amount_per_hour => '5',
:extras_ids => '', :phone_number => '3213', :credit_card_number => '4242424242424242', :credit_card_cvv => '777',
:credit_card_expiration => '12/20', :source_information => ''}}
end
And I've got this error in some logically close specs. But some tests passes, like this one:
it 'should not update user data if order errors is not empty' do
sign_in user
allow(Order).to receive(:find).with(order.id.to_s).and_return(order)
allow(order).to receive(:finalize) {order}
allow(order.errors).to receive(:empty?) {false}
expect(User).to_not receive(:update_user_data)
patch :save_order, {:id => order.id, :order => {:street_address => 'Baker street', :apt => '123#', :zip_id => zip.id, :frequency_id => frequency.id, :amount_per_hour => '5',
:extras_ids => '', :phone_number => '3213', :credit_card_number => '4242424242424242', :credit_card_cvv => '777',
:credit_card_expiration => '12/20', :source_information => ''}}
end
to_s or to_i doesn't help. The error line in controller -
#order = Order.find(params[:id]
So what could be in that case ? 'Cause it looks like some specs passes, but similar to them don't. Any suggestions ?
I have been using a RestClient request as such:
response = RestClient.post server_url, post_params, accept: :json
Which has been working fine. But I need to increase the timeout as it's not completing every now and then while the server is performing the upload.
I have researched and found that the only solution is to change the syntax to something like:
response = RestClient::Request.execute(:method => :post, :url => server_url, post_params, :timeout => 9000000000)
however, I don't seem to be able to pass the hashmap of parameters ('post_params') like i was able to in the previous call. how should I write the request so that 'post_params' is included. It's a complex hashmap, so i can't augment it or get rid of it.
Help is much appreciated.
The data you send is called a payload, so you need do specify it as payload:
response = RestClient::Request.execute(:method => :post, :url => server_url, :payload => post_params, :timeout => 9000000, :headers => {:accept => :json})
Also, you may want to use a shorter timeout, otherwise there is a chance you get a Errno::EINVAL: Invalid argument.
the data you send is in payload when we try to use rest_client.post or any method like get,put what rest_client do is
def self.post(url, payload, headers={}, &block)
Request.execute(:method => :post, :url => url, :payload => payload,
:headers => headers, &block)
end
so like we want to execute
response = RestClient.post api_url,
{:file => file, :multipart => true },
{ :params =>{:foo => 'foo'} #query params
so in the execute command will take take {:file => file, :multipart => true } as payload and { :params =>{:foo => 'foo' } } as header so
for passing all these you need
response= RestClient::Request.execute(:method => :post,
:url => api_url,
:payload => {:file => file, :multipart => true },
:headers => { :params =>{:foo => 'foo'}},
:timeout => 90000000)
this should do
The following code:
require 'csv'
desc "Import Voters from CSV File"
task :import => [:environment] do
file ="db/GOTV.csv"
CSV.foreach(file, :headers => true) do |row|
Voter.create({
:last_name => row[0],
:first_name => row[1],
:middle_name => row[2],
:name_suffix => row[3],
:primary_address => row[4],
:primary_city => row[5],
:primary_state => row[6],
:primary_zip => row[7],
:primary_zip4 => row[8],
:primary_unit => row[9],
:primary_unit_number => row[10],
:phone_number => row[11],
:phone_code => row[12],
:gender => row[13],
:party_code => row[14],
:voter_score => row[15],
:congressional_district => row[16],
:house_district => row[17],
:senate_district => row[18],
:county_name => row[19],
:voter_key => row[20],
:household_id => row[21],
:client_id => row[22],
:state_voter_id => row[23]
})
end
...is throwing the following error:
/Users/ecumbee/Desktop/cloudvoters/lib/tasks/import.rake:35: syntax error, unexpected $end, expecting kEND
end
^
I've tried removing the end, which throws the same error I've tried adding another end but it results in a can not compile error.
Edit:
error when adding a second end statement
Don't know how to build task 'db:import'
In the error message, $end refers to the end of the input file, while kEND refers to the end keyword, so it's complaining about a missing end, not an extra one.
If you still get a syntax error after adding another end, that's something unrelated to this error.
The end in your code is for the CSV.foreach ... do block. You're missing another end for the task ... to block.
If that still gives you a syntax error, edit your question and post that error instead.
I know you said you tried to add another end and it didn't help, but the problem with your file is that it's missing the end keyword that will end the task
task :import => [:environment] do
Then can you give more information about the error you're getting once you add the missing end ?