yellow-sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ Yellow Python SDK
2
+ =====================
3
+ This is the Yellow Ruby SDK. This simple SDK contains couple of Ruby methods that makes it easy to integrate with the Yellow API. To get started just:
4
+ ```
5
+ pip install yellow-sdk-python
6
+ ```
7
+
8
+ Examples
9
+ ---------
10
+ ```
11
+ import json
12
+ import yellow
13
+
14
+
15
+ # You can get your api key/secret from your merchant dashboard.
16
+
17
+ api_key = "YOUR_API_KEY" # store it in environment variable for better security
18
+ api_secret = "YOUR_API_SECRET" # store it in environment variable for better security
19
+ base_ccy = "USD" # Required: A 3-letter currency code
20
+ base_price = "0.05" # Required: The invoice price in the above currency
21
+ callback = "https://example.com" # Optional: URL for Yellow to POST to as a callback
22
+
23
+ created_invoice = yellow.create_invoice(api_key, api_secret, base_ccy, base_price, callback)
24
+
25
+ # Print the result beautifully!
26
+ print json.dumps(created_invoice.json(), sort_keys=True, indent=4)
27
+ ```
28
+ You should see something similar to the following in your terminal:
29
+ ```
30
+ {
31
+ "address": "155xsayoDXxRFP9rxDoecmpVUo7y5xKtc7", # Invoice Address
32
+ "base_ccy": "USD",
33
+ "base_price": "0.05",
34
+ "callback": "https://example.com",
35
+ "expiration": "2015-03-10T18:17:51.248Z", # Each invoice expires after 10 minutes of creation
36
+ "id": "6dd264975861fddbfe4404ed995f1ca4", # Invoice ID (to query the invoice later if you need to!)
37
+ "invoice_ccy": "BTC",
38
+ "invoice_price": "0.00017070",
39
+ "received": "0",
40
+ "redirect": null,
41
+ "remaining": "0.00017070",
42
+ "server_time": "2015-03-10T18:07:51.454Z",
43
+ "status": "new", # Status of the invoice. Other values are "authorizing" for unconfirmed transactions, and "paid" for confirmed transactions
44
+ "url": "https://cdn.yellowpay.co/invoice.5f0d082e.html?invoiceId=6dd264975861fddbfe4404ed995f1ca4" # Direct URL for the invoice. You can use it to embed the invoice widget in an iframe on your website.
45
+ }
46
+
47
+ ```
48
+ To query an invoice that you created, just pass in the `invoice_id` to the `query_invoice` function:
49
+ ```
50
+ invoice_id = "..." # invoice ID you received when you created the invoice.
51
+ invoice = yellow.query_invoice(api_key, api_secret, invoice_id)
52
+ print json.dumps(invoice.json(), sort_keys=True, indent=4)
53
+ ```
54
+ You should see exactly the same returned data you got from `create_invoice` above!
55
+
56
+ Verifying Yellow POST requests
57
+ ---------------------------
58
+ To verify that the request you just receive really is from us, we created a helper function that checks the signature of the request. Just pass in your api_secret, the callback URL you passed when you created the invoice, and the request object.
59
+
60
+ This function will return True if the signature matches (verified), or False if it doesn't match (not verified).
61
+ ```
62
+ is_verified = yellow.verify_ipn(api_secret, host_url, request)
63
+ ```
@@ -0,0 +1,105 @@
1
+ module Yellow
2
+
3
+ VERSION = "0.0.1"
4
+ YELLOW_SERVER = "https://" + (ENV["YELLOW_SERVER"] || "api.yellowpay.co")
5
+
6
+ class YellowApiError < Exception ; end
7
+
8
+ class Client
9
+ def initialize(api_key, api_secret)
10
+ @api_key = api_key
11
+ @api_secret = api_secret
12
+ end
13
+
14
+ def create_invoice(hash={})
15
+ ####
16
+ # This method creates a yellow invoices based on the following options:
17
+ #
18
+ # #param hash hash hash keys matching the expected
19
+ # HTTP arguments described here:
20
+ # http://yellowpay.co/docs/api/#creating-invoices
21
+ # #returns JSON parsed data
22
+ ####
23
+
24
+ endpoint = "/v1/invoice/"
25
+
26
+ make_request('POST', endpoint, hash.to_json)
27
+ end
28
+
29
+ def query_invoice(invoice_id)
30
+ ####
31
+ # Use this method to query an invoice you created earlier using its ID
32
+ #
33
+ # #param str invoice_id the ID of the invoice you're querying
34
+ #
35
+ # #returns JSON parsed data
36
+ ####
37
+
38
+ endpoint = "/v1/invoice/#{invoice_id}/"
39
+ make_request('GET', endpoint, "")
40
+ end
41
+
42
+ def verify_ipn(host_url, request_signature, request_nonce, request_body)
43
+ ####
44
+ # This is a helper method to verify that the request you just received really is from Yellow:
45
+ #
46
+ # #param str host_url the callback URL you set when you created the invoice
47
+ # #param str request_signature the signature header of the request
48
+ # #param str request_nonce the nonce header of the request
49
+ # #param str request_body the body of the request
50
+ #
51
+ # #returns boolean
52
+ ####
53
+
54
+ signature = get_signature(host_url, request_body, request_nonce, @api_secret)
55
+ signature == request_signature ? return true : return false
56
+ end
57
+
58
+ private
59
+ def make_request(method, endpoint, body)
60
+ ####
61
+ # A method used by our SDK to make requests to our server.
62
+ ####
63
+
64
+ url = YELLOW_SERVER + endpoint
65
+
66
+ nonce = Time.now.to_i * 1000
67
+ signature = get_signature(url, body, nonce, @api_secret)
68
+
69
+ uri = URI.parse(YELLOW_SERVER)
70
+ http = Net::HTTP.new(uri.host, uri.port)
71
+ http.use_ssl = true
72
+
73
+ if method == 'GET'
74
+ request = Net::HTTP::Get.new(endpoint)
75
+ elsif method == 'POST'
76
+ request = Net::HTTP::Post.new(endpoint)
77
+ end
78
+
79
+ request.add_field('Content-Type', 'application/json')
80
+ request.add_field('API-Key', @api_key)
81
+ request.add_field('API-Nonce', nonce)
82
+ request.add_field('API-Sign', signature)
83
+ request.add_field('API-Platform', "#{RUBY_PLATFORM} - Ruby #{RUBY_VERSION}")
84
+ request.add_field('API-Version', VERSION)
85
+ request.body = body
86
+ response = http.request(request)
87
+
88
+ if response.code == '200'
89
+ return JSON.parse(response.body)
90
+ else
91
+ raise YellowApiError.new(response.body)
92
+ end
93
+ end
94
+
95
+ private
96
+ def get_signature(url, body, nonce, api_secret)
97
+ ####
98
+ # A tiny method used by our SDK to sign and verify requests.
99
+ ####
100
+
101
+ message = nonce.to_s + url + body
102
+ hash = OpenSSL::HMAC.hexdigest('sha256', api_secret, message)
103
+ end
104
+ end
105
+ end
data/lib/yellow-sdk.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'yellow-sdk/client'
2
+ require 'json'
3
+ require 'uri'
4
+ require 'net/http'
5
+ require "openssl"
6
+ require "always_verify_ssl_certificates"
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yellow-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eslam A. Hefnawy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-04-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Yellow SDK. A ruby module to easily integrate with the Yellow bitcoin
15
+ payments API.
16
+ email: eslam@yellowpay.co
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/yellow-sdk.rb
22
+ - lib/yellow-sdk/client.rb
23
+ - README.md
24
+ homepage: http://yellowpay.co
25
+ licenses:
26
+ - Apache 2.0
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.23
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Yellow SDK. A ruby module to easily integrate with the Yellow bitcoin payments
49
+ API.
50
+ test_files: []