rocket-ruby-sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,108 @@
1
+ # Rocket::Ruby::Sdk
2
+
3
+ Ruby SDK to Rocket API, with this SDK you can create invoices and retrieve invoice status from out Simple Checkout System, you can make API calls when your plan have it.
4
+ This gem is based at [SDK PHP](https://github.com/Rocket-System/php-sdk)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rocket-ruby-sdk'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rocket-ruby-sdk
21
+
22
+ ## Usage
23
+
24
+ ### Send a new order:
25
+ ``` ruby
26
+ checkout = Rocket::Checkout::Checkout.new 'INFORM YOU SIMPLE CHECKOUT KEY'
27
+
28
+ invoice = Rocket::Invoice::Invoice.new
29
+ invoice.invoice_number = 1
30
+ invoice.invoice_description = 'Test Description'
31
+ invoice.cancel_url = 'http://example.com/cancel'
32
+ invoice.success_url = 'http://example.com/success'
33
+ invoice.customer_email = 'john@doe.com'
34
+ invoice.customer_name = 'John Doe'
35
+
36
+ product = Rocket::Invoice::InvoiceProduct.new
37
+ product.name = 'Test Product'
38
+ product.description = "Test Product Description"
39
+ product.quantity = 10
40
+ product.unity_price = BigDecimal.new('9.90')
41
+
42
+ product2 = Rocket::Invoice::InvoiceProduct.new
43
+ product2.name= "Test Product"
44
+ product2.description = "Test Product Description"
45
+ product2.quantity = 1
46
+ product2.unity_price = BigDecimal.new('1000.00')
47
+
48
+ invoice.add_products(product)
49
+ invoice.add_products(product2)
50
+
51
+ checkout.create_invoice(invoice)
52
+
53
+ return_url = checkout.pay_invoice_url
54
+ invoice_unique_code = checkout.invoice_token
55
+ status = checkout.invoice_status
56
+ ```
57
+
58
+ ### Query an order:
59
+
60
+ ``` ruby
61
+ checkout = Rocket::Checkout::Checkout.new 'INFORM YOU SIMPLE CHECKOUT KEY'
62
+ invoice = checkout.check_invoice 'INVOICE UNIQUE ID'
63
+ status = checkout.invoice_status
64
+ ```
65
+
66
+ ### Check if a user exists:
67
+
68
+ ``` ruby
69
+ transfer = Rocket::Payment::Transfer.new 'INFORM YOU SIMPLE CHECKOUT KEY'
70
+ send = transfer.check_user 'user@email.com'
71
+ send.status
72
+ ```
73
+
74
+ ### Get your actual Balance
75
+
76
+ You can check your actual balance for all your accounts:
77
+
78
+ default = Your default money account
79
+ bitcoin = You bitcoin Wallet
80
+ blocked = Your blocked funds wallet
81
+
82
+ ``` ruby
83
+ transfer = Rocket::Payment::Transfer.new 'INFORM YOU SIMPLE CHECKOUT KEY'
84
+ send = $transfer.check_balance 'default'
85
+ send.status
86
+ ```
87
+
88
+ ### Transfer Money:
89
+
90
+ ``` ruby
91
+ transfer = Rocket::Payment::Transfer.new 'INFORM YOU SIMPLE CHECKOUT KEY'
92
+ send = transfer.send_transfer 'user@email.com', BigDecimal.new('20.00'), 'USD',
93
+ 'Your Custom Message'
94
+ send.status
95
+ ```
96
+
97
+ ## Known issues
98
+
99
+ * Cover the gem with tests (I believe not something from another world)
100
+ * Improve DSL SDK (It was based on a [php code](https://github.com/Rocket-System/php-sdk))
101
+
102
+ So ...
103
+
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jonathanccalixto/rocket-ruby-sdk.
108
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rocket-ruby-sdk"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require 'rocket'
@@ -0,0 +1,35 @@
1
+ require "httparty"
2
+ require "recursive-open-struct"
3
+ require "active_support/all"
4
+
5
+ require "rocket/ruby/sdk/version"
6
+
7
+ require 'rocket/response'
8
+ require 'rocket/core'
9
+ require 'rocket/checkout'
10
+ require 'rocket/invoice'
11
+ require 'rocket/payment'
12
+ require 'rocket/subscription'
13
+ require 'rocket/system'
14
+
15
+ module Rocket
16
+ mattr_accessor :production_url
17
+ @@production_url = "http://dashboard.rocketpays.com"
18
+
19
+ mattr_accessor :sandbox_url
20
+ @@sandbox_url = "http://dashboard.sandbox.rocketpays.com"
21
+
22
+ mattr_accessor :development_url
23
+ @@development_url = "http://dashboard.rocketpays.dev"
24
+
25
+ mattr_accessor :environment
26
+ @@environment = :production
27
+
28
+ def self.setup
29
+ yield self
30
+ nil
31
+ end
32
+ end
33
+
34
+ class RocketException < Exception
35
+ end
@@ -0,0 +1,6 @@
1
+ require 'rocket/checkout/checkout'
2
+
3
+ module Rocket
4
+ module Checkout
5
+ end
6
+ end
@@ -0,0 +1,63 @@
1
+ module Rocket
2
+ module Checkout
3
+ class Checkout < Rocket::Core::Function
4
+ attr_accessor :invoice_token, :pay_invoice_url, :client_pass
5
+ attr_accessor :invoice_status, :merchant_id
6
+
7
+ def initialize(token, env = Rocket.environment, debug = false)
8
+ super(debug)
9
+
10
+ raise RocketException.new "Please Provide a Valid Token" if token.blank?
11
+
12
+ self.token = token
13
+
14
+ case env.to_sym
15
+ when :production then self.prepare_sdk_checkout
16
+ when :sandbox then self.prepare_sdk_sandbox_checkout
17
+ else self.prepare_sdk_development_checkout
18
+ end
19
+ end
20
+
21
+ def create_invoice(invoice)
22
+ unless invoice.is_a?(Rocket::Invoice::Invoice)
23
+ raise RocketException.new "Invalid Invoice Data"
24
+ end
25
+
26
+ self.data_send = self.make_json(invoice)
27
+ self.method_send = "put-invoice/#{self.token}"
28
+ self.curl_send
29
+
30
+ retorno = self.json_array(self.return_data)
31
+
32
+ raise RocketException.new retorno.message unless retorno.success
33
+
34
+ self.invoice_token = retorno.invoice_code
35
+ self.client_pass = false
36
+ self.pay_invoice_url = retorno.return_url
37
+ self.invoice_status = retorno.invoice_status
38
+
39
+ if retorno.respond_to?(:user) && retorno.user && retorno.user.new.blank?
40
+ self.client_pass = retorno.user.user_password
41
+ end
42
+
43
+ retorno
44
+ end
45
+
46
+ def check_invoice(invoice)
47
+ self.data_send = self.make_json(:invoice => invoice)
48
+ self.method_send = "checkout/invoice/query/token/#{token}"
49
+ self.curl_send
50
+
51
+ retorno = self.json_array(self.return_data)
52
+
53
+ raise RocketException.new retorno.message unless retorno.success
54
+
55
+ self.invoice_status = retorno.invoice_status
56
+ self.invoice_token = retorno.invoice_id
57
+ self.merchant_id = retorno.merchant_id
58
+
59
+ retorno
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,6 @@
1
+ require 'rocket/core/function'
2
+
3
+ module Rocket
4
+ module Core
5
+ end
6
+ end
@@ -0,0 +1,123 @@
1
+ require 'httparty'
2
+
3
+ module Rocket
4
+ module Core
5
+ class Function
6
+ include HTTParty
7
+
8
+ attr_accessor :debug
9
+
10
+ def initialize(debug = false)
11
+ self.debug = debug
12
+ end
13
+
14
+ def debug?
15
+ !!self.debug
16
+ end
17
+
18
+ protected
19
+
20
+ attr_accessor :resource_url, :token, :data_send, :return_data, :method_send
21
+
22
+ def method_send=(method)
23
+ @method_send = "/#{method}"
24
+ end
25
+
26
+ def make_json(data)
27
+ unless data.respond_to?(:to_json) && data.respond_to?(:as_json)
28
+ raise RocketException.new 'Please provide a array data'
29
+ end
30
+
31
+ to_normalize(data.as_json).to_json
32
+ end
33
+
34
+ def json_array(json)
35
+ Rocket::Response.new json, to_denormalize(json.parsed_response)
36
+ end
37
+
38
+ def prepare_sdk_checkout
39
+ self.resource_url = Rocket.production_url
40
+ end
41
+
42
+ def prepare_sdk_sandbox_checkout
43
+ self.resource_url = Rocket.sandbox_url
44
+ end
45
+
46
+ def prepare_sdk_development_checkout
47
+ self.resource_url = Rocket.development_url
48
+ end
49
+
50
+ def send_request(to)
51
+ true
52
+ end
53
+
54
+ def opts
55
+ @opts ||= {}
56
+ @opts[:headers] ||= {}
57
+
58
+ @opts[:headers].merge!(
59
+ {
60
+ #"Content-Type" => "application/json",
61
+ "token" => self.token
62
+ }
63
+ )
64
+
65
+ @opts
66
+ end
67
+
68
+ def curl_send
69
+ uri = "#{self.resource_url}#{self.method_send}"
70
+ options = self.opts.merge(
71
+ :body => self.data_send,
72
+ :format => :json
73
+ )
74
+
75
+ curl_response = self.class.post uri, options
76
+
77
+ raise curl_response.inspect if self.debug?
78
+
79
+ self.return_data = curl_response
80
+
81
+ true
82
+ end
83
+
84
+ private
85
+
86
+ def to_normalize(data)
87
+ case data
88
+ when BigDecimal then
89
+ data.to_f
90
+ when Hash then
91
+ data.dup.inject({}) do |hash, (key, value)|
92
+ hash["#{key}".camelcase(:lower).to_sym] = to_normalize(value)
93
+ hash
94
+ end
95
+ when Array then
96
+ data.collect do |value|
97
+ to_normalize(value)
98
+ end
99
+ else
100
+ data
101
+ end
102
+ end
103
+
104
+ def to_denormalize(data)
105
+ case data
106
+ when Float then
107
+ BigDecimal.new data.to_s
108
+ when Hash then
109
+ data.dup.inject({}) do |hash, (key, value)|
110
+ hash["#{key}".underscore.to_sym] = to_denormalize(value)
111
+ hash
112
+ end
113
+ when Array then
114
+ data.collect do |value|
115
+ to_denormalize(value)
116
+ end
117
+ else
118
+ data
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,7 @@
1
+ require 'rocket/invoice/invoice'
2
+ require 'rocket/invoice/invoice_product'
3
+
4
+ module Rocket
5
+ module Invoice
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module Rocket
2
+ module Invoice
3
+ class Invoice
4
+ attr_accessor :invoice_number, :invoice_description, :invoice_currency
5
+ attr_accessor :customer_name, :customer_email, :invoice_discounts
6
+ attr_accessor :invoice_delivery_fee, :success_url, :cancel_url
7
+ attr_accessor :invoice_products
8
+
9
+ def initialize
10
+ self.invoice_currency = 'USD'
11
+ self.invoice_discounts = BigDecimal.new('0.0')
12
+ self.invoice_delivery_fee = BigDecimal.new('0.0')
13
+ self.invoice_products = []
14
+ end
15
+
16
+ def add_products(product)
17
+ unless product.is_a? InvoiceProduct
18
+ raise RocketException.new 'Invalid Product Class'
19
+ end
20
+
21
+ self.invoice_products << product
22
+
23
+ true
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ module Rocket
2
+ module Invoice
3
+ class InvoiceProduct
4
+ attr_accessor :name, :description, :unity_price, :quantity
5
+
6
+ def initialize
7
+ self.name = ''
8
+ self.description = ''
9
+ self.unity_price = BigDecimal.new('0.0')
10
+ self.quantity = BigDecimal.new('0.0')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ require 'rocket/payment/transfer'
2
+
3
+ module Rocket
4
+ module Payment
5
+ end
6
+ end
@@ -0,0 +1,68 @@
1
+ module Rocket
2
+ module Payment
3
+ class Transfer < Rocket::Core::Function
4
+ def initialize(token, env = Rocket.environment, debug = false)
5
+ super(debug)
6
+
7
+ raise RocketException.new "Please Provide a Valid Token" if token.blank?
8
+
9
+ self.token = token
10
+
11
+ case env.to_sym
12
+ when :production then self.prepare_sdk_checkout
13
+ when :sandbox then self.prepare_sdk_sandbox_checkout
14
+ else self.prepare_sdk_development_checkout
15
+ end
16
+ end
17
+
18
+ def check_user(email)
19
+ self.method_send = "checkout/user/check/token/#{self.token}"
20
+ self.data_send = self.make_json(:emailUser => email)
21
+ self.curl_send
22
+
23
+ retorno = self.json_array(self.return_data)
24
+
25
+ raise RocketException.new retorno.message unless retorno.success
26
+
27
+ self.status = retorno
28
+
29
+ retorno
30
+ end
31
+
32
+ def check_balance(account = 'default')
33
+ self.method_send = "checkout/user/check-balance/token/#{self.token}"
34
+ self.data_send = self.make_json(:account => account)
35
+ self.curl_send
36
+
37
+ retorno = self.json_array(self.return_data)
38
+
39
+ raise RocketException.new retorno.message unless retorno.success
40
+
41
+ self.status = retorno
42
+
43
+ retorno
44
+ end
45
+
46
+ def send_transfer(user, value, currency = 'USD', description = '')
47
+ self.send_method = "checkout/user/send-transfer/token/#{self.token}"
48
+ self.data_send = self.make_json(
49
+ :userTo => user, :value => value, :description => description,
50
+ :currency => currency
51
+ )
52
+ self.curl_send
53
+
54
+ retorno = self.json_array(self.return_data)
55
+
56
+ raise RocketException.new retorno.message unless retorno.success
57
+
58
+ self.status = retorno
59
+
60
+ retorno
61
+ end
62
+
63
+ private
64
+
65
+ attr_accessor :status
66
+ end
67
+ end
68
+ end