require “net/https”

require “uri”

data = Hash.new

data[“charge”] = Hash.new

data[“charge”][“amount”] = amount_to_charge

data[“charge”][“memo”] = ‘This is the description of the one time charge.’

or

you can create hash using

data = {

“charge” =>{

“amount”  => amount_to_change,

“memo”    => ‘This is the description of the one time charge.’

              }

}

url = “https://#{Chargify.subdomain}.chargify.com/subscriptions/#{chargify_subscription.id}/charges.json”

#chargify.subdomain  is subdomain name assigned to your api

#chargify_subscription.id is Subscription id

uri = URI.parse(url)

http = Net::HTTP.new(uri.host, uri.port)

http.use_ssl = true

http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(uri.request_uri, initheader = {‘Content-Type’ =>’application/json’})

request.body = data.to_json

request.basic_auth(“#{Chargify.api_key}”, “x”)

response = http.request(request)

if response.code != ‘201’

subscription_errors = response.to_json

error_message = ‘Sorry! We were unable to change your subscription.

flash[:error] = error_message

redirect_to(“/account/finalize”)

return

end

Chargify API : Charge card using Basic authentication and Post Request

Leave a Reply

Your email address will not be published.