mollie-api-ruby 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00c764099a49ead8514278cfa6db983f644c35f0
4
+ data.tar.gz: 04224d5226ac3b33340225fb2a4126f0f058ff8e
5
+ SHA512:
6
+ metadata.gz: e1f1fb6c5e98c36fec4de50dcb300e45529373c62b11bae41ed2c2febf9e54e529dc3ab85dfadf7d2224e2c999b5b634b895cfad50e8cc2c57f2ba11fa97ca47
7
+ data.tar.gz: c7fe28ba206cdea937e4e1d2300dd10501c7738d3cebc6058433a877d12c4cdc6e96d6e711e9cb843b517e5a7b33d477b4a431603c8c48133addbe966451c5da
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ /.idea
2
+ .DS_Store
3
+ mollie-api-ruby*.gem
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2014, Mollie B.V.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.mdown ADDED
@@ -0,0 +1,92 @@
1
+ ![Mollie](http://www.mollie.nl/files/Mollie-Logo-Style-Small.png)
2
+
3
+ # Mollie API client for Ruby #
4
+
5
+ Accepting [iDEAL](https://www.mollie.nl/betaaldiensten/ideal/), [Mister Cash](https://www.mollie.nl/betaaldiensten/mistercash/), [Creditcard](https://www.mollie.nl/betaaldiensten/creditcard/), [PayPal](https://www.mollie.nl/betaaldiensten/paypal/), and [paysafecard](https://www.mollie.nl/betaaldiensten/paysafecard/) online payments without fixed monthly costs or any punishing registration procedures. Just use the Mollie API to receive payments directly on your website.
6
+
7
+ ## Requirements ##
8
+ To use the Mollie API client, the following things are required:
9
+
10
+ + Get yourself a free [Mollie account](https://www.mollie.nl/aanmelden). No sign up costs.
11
+ + Create a new [Website profile](https://www.mollie.nl/beheer/account/profielen/) to generate API keys (live and test mode) and setup your webhook.
12
+ + Now you're ready to use the Mollie API client in test mode.
13
+ + In order to accept payments in live mode, payment methods must be activated in your account. Follow [a few of steps](https://www.mollie.nl/beheer/diensten), and let us handle the rest.
14
+ + This API client requires the [REST Client](https://github.com/rest-client/rest-client) and [JSON](https://rubygems.org/gems/json) gems.
15
+
16
+ ## Installation ##
17
+
18
+ By far the easiest way to install the Mollie API client is to install it with [gem](http://rubygems.org/).
19
+
20
+ ```
21
+ $ gem install mollie-api-ruby
22
+ ```
23
+
24
+ You may also git checkout or [download all the files](https://github.com/mollie/mollie-api-ruby/archive/master.zip), and include the Mollie API client manually.
25
+
26
+ ## How to receive payments ##
27
+
28
+ To successfully receive a payment, these steps should be implemented:
29
+
30
+ 1. Use the Mollie API client to create a payment with the requested amount, description and optionally, a payment method. It is important to specify a unique redirect URL where the customer is supposed to return to after the payment is completed.
31
+
32
+ 2. Immediately after the payment is completed, our platform will send an asynchronous request to the configured webhook to allow the payment details to be retrieved, so you know when exactly to start processing the customer's order.
33
+
34
+ 3. The customer returns, and should be satisfied to see that the order was paid and is now being processed.
35
+
36
+ ## Getting started ##
37
+
38
+ Requiring the Mollie API Client.
39
+
40
+ ```ruby
41
+ require "Mollie/API/Client"
42
+ ```
43
+
44
+ Initializing the Mollie API client, and setting your API key.
45
+
46
+ ```ruby
47
+ mollie = Mollie::API::Client.new
48
+ mollie.setApiKey "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM"
49
+ ```
50
+
51
+ Creating a new payment.
52
+
53
+ ```ruby
54
+ payment = mollie.payments.create \
55
+ :amount => 10.00,
56
+ :description => "My first API payment",
57
+ :redirectUrl => "https://webshop.example.org/order/12345/"
58
+ ```
59
+
60
+ Retrieving a payment.
61
+
62
+ ```ruby
63
+ payment = mollie.payments.get payment.id
64
+
65
+ if payment.paid?
66
+ puts "Payment received."
67
+ end
68
+ ```
69
+
70
+ ## Examples ##
71
+
72
+ The examples require [Sinatra](http://rubygems.org/gems/sinatra) so you will need to install that gem first. Afterwards simply run:
73
+
74
+ ```
75
+ $ cd mollie-api-ruby
76
+ $ ruby examples/app.rb
77
+ ```
78
+
79
+ ## License ##
80
+ [BSD (Berkeley Software Distribution) License](http://www.opensource.org/licenses/bsd-license.php).
81
+ Copyright (c) 2014, Mollie B.V.
82
+
83
+ ## Support ##
84
+ Contact: [www.mollie.nl](http://www.mollie.nl) — info@mollie.nl — +31 20-612 88 55
85
+
86
+ + [More information about iDEAL via Mollie](https://www.mollie.nl/betaaldiensten/ideal/)
87
+ + [More information about Creditcard via Mollie](https://www.mollie.nl/betaaldiensten/creditcard/)
88
+ + [More information about Mister Cash via Mollie](https://www.mollie.nl/betaaldiensten/mistercash/)
89
+ + [More information about PayPal via Mollie](https://www.mollie.nl/betaaldiensten/paypal/)
90
+ + [More information about paysafecard via Mollie](https://www.mollie.nl/betaaldiensten/paysafecard/)
91
+
92
+ ![Powered By Mollie](http://www.mollie.nl/images/badge-betaling-medium.png)
@@ -0,0 +1,55 @@
1
+ #
2
+ # Example 1 - How to prepare a new payment with the Mollie API.
3
+ #
4
+ require File.expand_path "../lib/Mollie/API/Client", __dir__
5
+
6
+ begin
7
+ #
8
+ # Initialize the Mollie API library with your API key.
9
+ #
10
+ # See: https://www.mollie.nl/beheer/account/profielen/
11
+ #
12
+ mollie = Mollie::API::Client.new
13
+ mollie.setApiKey "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM"
14
+
15
+ #
16
+ # Generate a unique order id for this example. It is important to include this unique attribute
17
+ # in the redirectUrl (below) so a proper return page can be shown to the customer.
18
+ #
19
+ order_id = Time.now.to_i
20
+
21
+ #
22
+ # Determine the url parts to these example files.
23
+ #
24
+ protocol = $request.secure? && "https" || "http"
25
+ hostname = $request.host || "localhost"
26
+ port = $request.port || 80
27
+ path = $request.script_name || ""
28
+
29
+ #
30
+ # Payment parameters:
31
+ # amount Amount in EUROs. This example creates a € 10,- payment.
32
+ # description Description of the payment.
33
+ # redirectUrl Redirect location. The customer will be redirected there after the payment.
34
+ # metadata Custom metadata that is stored with the payment.
35
+ #
36
+ payment = mollie.payments.create \
37
+ :amount => 10.00,
38
+ :description => "My first API payment",
39
+ :redirectUrl => "#{protocol}://#{hostname}:#{port}#{path}/3-return-page?order_id=#{order_id}",
40
+ :metadata => {
41
+ :order_id => order_id
42
+ }
43
+
44
+ #
45
+ # In this example we store the order with its payment status in a database.
46
+ #
47
+ database_write order_id, payment.status
48
+
49
+ #
50
+ # Send the customer off to complete the payment.
51
+ #
52
+ $response.redirect payment.getPaymentUrl
53
+ rescue Mollie::API::Exception => e
54
+ $response.body << "API call failed: " << (CGI.escapeHTML e.message)
55
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Example 2 - How to verify Mollie API Payments in a webhook.
3
+ #
4
+ require File.expand_path "../lib/Mollie/API/Client", __dir__
5
+
6
+ begin
7
+ #
8
+ # Initialize the Mollie API library with your API key.
9
+ #
10
+ # See: https://www.mollie.nl/beheer/account/profielen/
11
+ #
12
+ mollie = Mollie::API::Client.new
13
+ mollie.setApiKey "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM"
14
+
15
+ #
16
+ # Retrieve the payment's current state.
17
+ #
18
+ payment = mollie.payments.get $request.params['id']
19
+ order_id = payment.metadata.order_id
20
+
21
+ #
22
+ # Update the order in the database.
23
+ #
24
+ database_write order_id, payment.status
25
+
26
+ if payment.paid?
27
+ #
28
+ # At this point you'd probably want to start the process of delivering the product to the customer.
29
+ #
30
+ $response.body << "Paid"
31
+ elsif payment.open? == false
32
+ #
33
+ # The payment isn't paid and isn't open anymore. We can assume it was aborted.
34
+ #
35
+ $response.body << "Cancelled"
36
+ else
37
+ $response.body << "In progress"
38
+ end
39
+ rescue Mollie::API::Exception => e
40
+ $response.body << "API call failed: " << (CGI.escapeHTML e.message)
41
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Example 3 - How to show a return page to the customer.
3
+ #
4
+ # In this example we retrieve the order stored in the database.
5
+ # Here, it's unnecessary to use the Mollie API Client.
6
+ #
7
+ if $request.params.empty?
8
+ $response.body << "No order"
9
+ else
10
+ status = database_read $request.params['order_id']
11
+
12
+ #
13
+ # Determine the url parts to these example files.
14
+ #
15
+ path = $request.script_name || ""
16
+
17
+ $response.body << "<p>Your payment status is '" << (CGI.escapeHTML status) << "'.</p>"
18
+ $response.body << "<p>"
19
+ $response.body << '<a href="' << path << '/">Back to examples</a><br>'
20
+ $response.body << "</p>"
21
+ end
@@ -0,0 +1,77 @@
1
+ #
2
+ # Example 4 - How to prepare an iDEAL payment with the Mollie API.
3
+ #
4
+ require File.expand_path "../lib/Mollie/API/Client", __dir__
5
+
6
+ begin
7
+ #
8
+ # Initialize the Mollie API library with your API key.
9
+ #
10
+ # See: https://www.mollie.nl/beheer/account/profielen/
11
+ #
12
+ mollie = Mollie::API::Client.new
13
+ mollie.setApiKey "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM"
14
+
15
+ #
16
+ # First, let the customer pick the bank in a simple HTML form. This step is actually optional.
17
+ #
18
+ if $request.params.empty?
19
+ issuers = mollie.issuers.all
20
+
21
+ $response.body << '<form method="post">Select your bank: <select name="issuer">'
22
+
23
+ issuers.each { |issuer|
24
+ if issuer.method == Mollie::API::Object::Method::IDEAL
25
+ $response.body << '<option value=' << (CGI.escapeHTML issuer.id) << '>' << (CGI.escapeHTML issuer.name)
26
+ end
27
+ }
28
+
29
+ $response.body << '<option value="">or select later</option>'
30
+ $response.body << '</select><button>OK</button></form>'
31
+ else
32
+ #
33
+ # Generate a unique order id for this example. It is important to include this unique attribute
34
+ # in the redirectUrl (below) so a proper return page can be shown to the customer.
35
+ #
36
+ order_id = Time.now.to_i
37
+
38
+ #
39
+ # Determine the url parts to these example files.
40
+ #
41
+ protocol = $request.secure? && "https" || "http"
42
+ hostname = $request.host || "localhost"
43
+ port = $request.port || 80
44
+ path = $request.script_name || ""
45
+
46
+ #
47
+ # Payment parameters:
48
+ # amount Amount in EUROs. This example creates a € 27,50 payment.
49
+ # description Description of the payment.
50
+ # redirectUrl Redirect location. The customer will be redirected there after the payment.
51
+ # metadata Custom metadata that is stored with the payment.
52
+ # method Payment method "ideal".
53
+ # issuer The customer's bank. If empty the customer can select it later.
54
+ #
55
+ payment = mollie.payments.create \
56
+ :amount => 27.50,
57
+ :description => "My first API payment",
58
+ :redirectUrl => "#{protocol}://#{hostname}:#{port}#{path}/3-return-page?order_id=#{order_id}",
59
+ :metadata => {
60
+ :order_id => order_id
61
+ },
62
+ :method => Mollie::API::Object::Method::IDEAL,
63
+ :issuer => !$request.params['issuer'].empty? ? $request.params['issuer'] : nil
64
+
65
+ #
66
+ # In this example we store the order with its payment status in a database.
67
+ #
68
+ database_write order_id, payment.status
69
+
70
+ #
71
+ # Send the customer off to complete the payment.
72
+ #
73
+ $response.redirect payment.getPaymentUrl
74
+ end
75
+ rescue Mollie::API::Exception => e
76
+ $response.body << "API call failed: " << (CGI.escapeHTML e.message)
77
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # Example 5 - How to retrieve your payments history.
3
+ #
4
+ require File.expand_path "../lib/Mollie/API/Client", __dir__
5
+
6
+ begin
7
+ #
8
+ # Initialize the Mollie API library with your API key.
9
+ #
10
+ # See: https://www.mollie.nl/beheer/account/profielen/
11
+ #
12
+ mollie = Mollie::API::Client.new
13
+ mollie.setApiKey "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM"
14
+
15
+ #
16
+ # Get the all payments for this API key ordered by newest.
17
+ #
18
+ payments = mollie.payments.all
19
+
20
+ $response.body = "Your API key has #{payments.totalCount} payments, last #{payments.count}:<br>"
21
+
22
+ payments.each { |payment|
23
+ $response.body << "&euro; #{payment.amount}, status: #{CGI.escapeHTML payment.status}<br>"
24
+ }
25
+ rescue Mollie::API::Exception => e
26
+ $response.body << "API call failed: " << (CGI.escapeHTML e.message)
27
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # Example 6 - How to get the currently activated payment methods.
3
+ #
4
+ require File.expand_path "../lib/Mollie/API/Client", __dir__
5
+
6
+ begin
7
+ #
8
+ # Initialize the Mollie API library with your API key.
9
+ #
10
+ # See: https://www.mollie.nl/beheer/account/profielen/
11
+ #
12
+ mollie = Mollie::API::Client.new
13
+ mollie.setApiKey "test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM"
14
+
15
+ #
16
+ # Get the all the activated methods for this API key.
17
+ #
18
+ methods = mollie.methods.all
19
+
20
+ $response.body << "Your API key has #{methods.totalCount} activated payment methods:<br>"
21
+
22
+ methods.each { |method|
23
+ $response.body << '<div style="line-height:40px; vertical-align:top">'
24
+ $response.body << '<img src="' << (CGI.escapeHTML method.image.normal) << '"> '
25
+ $response.body << (CGI.escapeHTML method.description) << ' (' << (CGI.escapeHTML method.id) << ')'
26
+ $response.body << '</div>'
27
+ }
28
+ rescue Mollie::API::Exception => e
29
+ $response.body << "API call failed: " << (CGI.escapeHTML e.message)
30
+ end
data/examples/app.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'sinatra'
2
+
3
+ #
4
+ # Show all examples as links.
5
+ #
6
+ examples = [
7
+ '1-new-payment',
8
+ '2-webhook-verification',
9
+ '3-return-page',
10
+ '4-ideal-payment',
11
+ '5-payments-history',
12
+ '6-list-activated-methods'
13
+ ]
14
+
15
+ get "/" do
16
+ index = ""
17
+
18
+ examples.each { |example|
19
+ index << "<a href='/#{example}'>#{example}</a><br>"
20
+ }
21
+
22
+ index
23
+ end
24
+
25
+ #
26
+ # Register all examples as pages.
27
+ #
28
+ examples.each { |example|
29
+ get "/#{example}" do
30
+ $request = request
31
+ $response = response
32
+ load File.expand_path "#{example}.rb", __dir__
33
+ end
34
+
35
+ post "/#{example}" do
36
+ $request = request
37
+ $response = response
38
+ load File.expand_path "#{example}.rb", __dir__
39
+ end
40
+ }
41
+
42
+ #
43
+ # NOTE: This example uses a text file as a database. Please use a real database like MySQL in production code.
44
+ #
45
+ def database_write (order_id, status)
46
+ order_id = order_id.to_i
47
+ database = File.expand_path "orders/order-#{order_id}.txt", __dir__
48
+
49
+ File.open(database, 'w') { |file| file.write status }
50
+ end
51
+
52
+ def database_read (order_id)
53
+ order_id = order_id.to_i
54
+ database = File.expand_path "orders/order-#{order_id}.txt", __dir__
55
+
56
+ status = File.read(database) || "unknown order"
57
+ end
@@ -0,0 +1 @@
1
+ *.txt
@@ -0,0 +1,102 @@
1
+ require "json"
2
+ require "rest_client"
3
+
4
+ ["Exception",
5
+ "Resource/Base",
6
+ "Resource/Payments",
7
+ "Resource/Issuers",
8
+ "Resource/Methods",
9
+ "Object/Base",
10
+ "Object/List",
11
+ "Object/Payment",
12
+ "Object/Issuer",
13
+ "Object/Method"].each {|file| require File.expand_path file, __dir__ }
14
+
15
+ module Mollie
16
+ module API
17
+ class Client
18
+ CLIENT_VERSION = "1.0.1"
19
+ API_ENDPOINT = "https://api.mollie.nl"
20
+ API_VERSION = "v1"
21
+
22
+ attr_reader :payments, :issuers, :methods
23
+
24
+ def initialize ()
25
+ @payments = Mollie::API::Resource::Payments.new self
26
+ @issuers = Mollie::API::Resource::Issuers.new self
27
+ @methods = Mollie::API::Resource::Methods.new self
28
+
29
+ @api_endpoint = API_ENDPOINT
30
+ @api_key = ""
31
+ @version_strings = []
32
+
33
+ addVersionString "Mollie/" << CLIENT_VERSION
34
+ addVersionString "Ruby/" << RUBY_VERSION
35
+ addVersionString OpenSSL::OPENSSL_VERSION.split(" ").slice(0, 2).join "/"
36
+ end
37
+
38
+ def setApiEndpoint (api_endpoint)
39
+ @api_endpoint = api_endpoint.chomp "/"
40
+ end
41
+
42
+ def getApiEndpoint ()
43
+ @api_endpoint
44
+ end
45
+
46
+ def setApiKey (api_key)
47
+ @api_key = api_key
48
+ end
49
+
50
+ def addVersionString (version_string)
51
+ @version_strings << (version_string.gsub /\s+/, "-")
52
+ end
53
+
54
+ def _getRestClient (request_url, request_headers)
55
+ RestClient::Resource.new request_url,
56
+ :headers => request_headers,
57
+ :ssl_ca_file => (File.expand_path "cacert.pem", __dir__),
58
+ :verify_ssl => OpenSSL::SSL::VERIFY_PEER
59
+ end
60
+
61
+ def performHttpCall (http_method, api_method, id = nil, http_body = nil)
62
+ request_headers = {
63
+ :accept => :json,
64
+ :authorization => "Bearer #{@api_key}",
65
+ :user_agent => @version_strings.join(" "),
66
+ "X-Mollie-Client-Info" => getUname
67
+ }
68
+
69
+ if http_body.respond_to? :delete_if
70
+ http_body.delete_if { |k, v| v.nil? }
71
+ end
72
+
73
+ begin
74
+ request_url = "#{@api_endpoint}/#{API_VERSION}/#{api_method}/#{id}".chomp "/"
75
+ rest_client = _getRestClient request_url, request_headers
76
+
77
+ case http_method
78
+ when "GET"
79
+ response = rest_client.get
80
+ when "POST"
81
+ response = rest_client.post http_body
82
+ when "DELETE"
83
+ response = rest_client.delete
84
+ end
85
+ response = JSON.parse response, :symbolize_names => true
86
+ rescue RestClient::ExceptionWithResponse => e
87
+ response = JSON.parse e.response, :symbolize_names => true
88
+ raise e if response[:error].nil?
89
+ end
90
+
91
+ raise Mollie::API::Exception.new response[:error][:message] unless response[:error].nil?
92
+ response
93
+ end
94
+
95
+ def getUname ()
96
+ `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
97
+ rescue Errno::ENOMEM
98
+ "uname lookup failed"
99
+ end
100
+ end
101
+ end
102
+ end