How do you turn the PDT response from PayPal into a hash? - ruby-on-rails

I'm trying to use the PDT system on PayPal to manage payments on my site. My site is correctly receiving the transaction id which is sent back to paypal in order to receive the transaction data.
This is the code which I am using to post the transaction id to paypal and receive a response.
response = Net::HTTP.post_form(URI.parse("#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?"), values)
puts response.body
I am receiving the correct response where response.body shows this in the terminal.
SUCCESS
transaction_subject=SPORTS+PACKAGE+%281+Week%29
payment_date=23%3A32%3A01+Jul+20%2C+2017+PDT
txn_type=subscr_payment
subscr_id=I-WHFVMBY57NX2
last_name=Lucas
residence_country=US
item_name=SPORTS+PACKAGE+%281+Week%29
payment_gross=
mc_currency=AUD
business=merchant-success%40puntsquad.com
payment_type=instant
protection_eligibility=Ineligible
payer_status=verified
payer_email=buyer-success%40puntsquad.com
txn_id=8M5887849L359363Y
receiver_email=merchant-success%40puntsquad.com
first_name=George
payer_id=667TSNBH7R7X4
receiver_id=WJYE8WGSREP98
payment_status=Completed
payment_fee=
mc_fee=2.00
mc_gross=50.00
charset=windows-1252
My problem is I am unsure of how I am able to access these values.

You can convert the response body to a hash as follows
hsh = CGI::parse(response.body)
puts hsh['transaction_subject']

Related

How to log the Graphql success response in ruby

I am working on Ruby programming. Getting graphql client call response in the below format.
response is
[#<GraphQL::Client::Response:0x00000001169729a0 #original_hash={"data"=>{"provisioningAddEntitlementToAccounts"=>{"result"=>[{"accountId"=>11571824, "status"=>"SUCCESS"}, {"accountId"=>11571825, "status"=>"SUCCESS"}]}}}, #data=#<#<Module:0x000000011537e220>::RootMutationType provisioningAddEntitlementToAccounts=...>, #errors=#<GraphQL::Client::Errors #messages={} #details={}>, #extensions=nil>]
In some rare cases there is a chance to get "Account Not Found" response for some account Id's and "SUCCESS" for some account Id's.
So I would like to store all the account Id's which are getting "SUCCESS" response only into some variable.
Can you please someone help me on this.

GET https://api.storekit.itunes.apple.com/inApps/v1/history/{originalTransactionId} 404

when i called the apple api: https://api.storekit-sandbox.itunes.apple.com/inApps/v1/history/{originalTransactionId} or https://api.storekit.itunes.apple.com/inApps/v1/history/{originalTransactionId} , http response 404
and when i check the apple document https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history?language=objc , i found that,404 is(AccountNotFoundError | AccountNotFoundRetryableError | AppNotFoundError | AppNotFoundRetryableError | OriginalTransactionIdNotFoundError | OriginalTransactionIdNotFoundRetryableError)
but i only can see 404, not 404xxx ;
thanks for your help;
Is it possible that you have not replaced the {originalTransactionId} with the original transaction ID value provided by Apple during the purchase?
In StoreKit v1, upon a purchase, Apple will return your app a base64 encoded string of receipt data.
Your app will have to pass this string back to you backend service.
Later on, from your backend, you can invoke /verifyReceipt to get decoded transaction information. In the in_app field of the response you will be getting a series of transactions including an original transaction ID for each one.
Now you can invoke https://api.storekit-sandbox.itunes.apple.com/inApps/v1/history/{originalTransactionId} for each transaction in the in_app array in the previous response, by replacing {originalTransactionId} with each of the original transaction IDs you received in the previous call.
Regarding the 404 response you received: 404 is a standard HTTP status code meaning "NOT_FOUND". In order to get more information about the error you need to inspect the HTTP response body. There, according to the documentation, you will receive an error code indicating the exact nature of the error.

Rails API 422 Unprocessable Entity: No verification key available, heroku

I created a Rails API with a JWT authentication system and deployed it to Heroku. When I request the endpoints locally, all seems to be working fine but when I make requests to the live endpoints (i.e the Heroku deployed app) I get a: 422 Unprocessable Entity server error and the response body looks like this:
{
"message": "No verification key available"
}
The class responsible for encoding and decoding the auth token is defined as follows:
class JsonWebToken
# secret to encode and decode token
HMAC_SECRET = Rails.application.secrets.secret_key_base
def self.encode(payload, exp = 24.hours.from_now)
# set expiry to 24 hours from the creation time.
payload[:exp] = exp.to_i
# sign token with application secret
JWT.encode(payload, HMAC_SECRET)
end
def self.decode(token)
# get payload, first index in decoded Array
body = JWT.decode(token, HMAC_SECRET)[0]
HashWithIndifferentAccess.new body
# rescue from all decode errors
rescue JWT::DecodeError => e
# raise custom error to be handled by custom handler
raise ExceptionHandler::InvalidToken, e.message
end
end
I have an endpoint /signup where I can make a POST request to register a new user and POST /todos which is accessible and available only to registered users. Making a registration request works perfectly fine, but when I try to make the POST request to the /todos endpoint it raises an error.
The association between user and suit is 1:m respectively.
Please if you have any idea on how I can fix this, I'll be very grateful, thanks : ).
I finally figured a way out by altering the Rails.application.secrets.secret_key_base to Rails.application.secret_key_base. For a more detailed review on this please check out this link. Hopefully, this will help someone facing a similar issue.
This was also my problem. After checking out my json_web_token.rb file, I figured out that I had written the following line:
HMAC_SECRET = Rails.application.secrets.secret_key_base
There is an extra secrets reference, which is causing the problem. It should be:
HMAC_SECRET = Rails.application.secret_key_base
But as far as I'm concerned, you managed to figure it out yourself!

How to access external api response?

I am building a payment integration from 3rd party api. After I send the request I am getting this response from terminal. What I need is to get the token in form at view. How can I access the token value?
Started GET "/charges/charge" for 127.0.0.1 at 2017-06-09 14:28:48 +0300
Processing by ChargesController#charge as HTML"{\"status\":\"success\",\"systemTime\":1497007730319,\"conversationId\":\"123456789\",\"token\":\"4d4b7364-acf7-45df-9fbc-d680212ab9ba\",\"checkoutFormContent\":\"<script type=\\\"text/javascript\\\">if (typeof pay == 'undefined') {var pay = {token:\\\"4d4b7364-acf7-45df-9fbc-d680212ab9ba\\\",price:1.20,}</script>\
This is the json response which you are getting from payment getway. So you will need to parse it.
You can parse this json response like this
response = "{\"status\":\"success\",\"systemTime\":1497007730319,\"conversationId\":\"123456789\",\"token\":\"4d4b7364-acf7-45df-9fbc-d680212ab9ba\"}"
parsed_response = JSON.parse(response) # This will return you hash
token = parsed_response['token']

How to get the status after sending data to external server rails

In my rails (3.2.13) app I send data to an external server using a form, then the external server process the data I sent and shows that the result is ok or not, I need to save that result or status to my rails app database, but I'm not sure about how to redirect to another page when the process in the external server is done.
I have a function to ask the server if the process of that data went ok using the reference or id that I sent in the first place using the form but as I said I don't know how to redirect after the process is finish...
please help me
You can use some core Ruby libraries to make a subsequent request on the same endpoint to determine the status code of your request. Try the following, cited in whole from Ruby Inside:
# Basic REST.
# Most REST APIs will set semantic values in response.body and response.code.
require "net/http"
http = Net::HTTP.new("api.restsite.com")
request = Net::HTTP::Post.new("/users")
request.set_form_data({"users[login]" => "quentin"})
response = http.request(request)
# Use nokogiri, hpricot, etc to parse response.body.
request = Net::HTTP::Get.new("/users/1")
response = http.request(request)
# As with POST, the data is in response.body.
request = Net::HTTP::Put.new("/users/1")
request.set_form_data({"users[login]" => "changed"})
response = http.request(request)
request = Net::HTTP::Delete.new("/users/1")
response = http.request(request)
Once you've instantiated a response object, you can operate on it in the following manner:
response.code #=> returns HTTP response code

Resources