Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Payment Links API #177

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/payment-links/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
payment_link = Mollie::PaymentLink.create(
description: "Bicycle tires",
amount: { currency: "EUR", value: "24.95" }
)

1 change: 1 addition & 0 deletions examples/payment-links/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mollie::PaymentLink.delete("pl_4Y0eZitmBnQ6IDoMqZQKh")
1 change: 1 addition & 0 deletions examples/payment-links/get.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
payment_link = Mollie::PaymentLink.get("pl_4Y0eZitmBnQ6IDoMqZQKh")
2 changes: 2 additions & 0 deletions examples/payment-links/list-payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
payment_link = Mollie::PaymentLink.get("pl_4Y0eZitmBnQ6IDoMqZQKh")
payments = payment_link.payments
1 change: 1 addition & 0 deletions examples/payment-links/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
payment_links = Mollie::PaymentLink.all
5 changes: 5 additions & 0 deletions examples/payment-links/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
payment_link = Mollie::PaymentLink.update(
"pl_4Y0eZitmBnQ6IDoMqZQKh",
description: "Bicycle tires",
archived: true
)
1 change: 1 addition & 0 deletions lib/mollie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Mollie
require 'mollie/organization'
require 'mollie/partner'
require 'mollie/payment'
require 'mollie/payment_link'
require 'mollie/permission'
require 'mollie/profile'
require 'mollie/refund'
Expand Down
2 changes: 2 additions & 0 deletions lib/mollie/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def resource_name(parent_id = nil)
path.last
end
end

alias_method :embedded_resource_name, :resource_name
end

def update(data = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/mollie/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(list_attributes, klass)
@klass = klass

list_attributes['items'] ||= if list_attributes['_embedded']
list_attributes['_embedded'].fetch(klass.resource_name, [])
list_attributes['_embedded'].fetch(klass.embedded_resource_name, [])
else
[]
end
Expand Down
70 changes: 70 additions & 0 deletions lib/mollie/payment_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Mollie
class PaymentLink < Base
attr_accessor :id,
:mode,
:description,
:archived,
:redirect_url,
:webhook_url,
:profile_id,
:_links

attr_reader :amount,
:created_at,
:paid_at,
:updated_at,
:expires_at

alias_method :links, :_links

def self.embedded_resource_name(_parent_id = nil)
"payment_links"
end

def self.resource_name(_parent_id = nil)
"payment-links"
end

def amount=(amount)
@amount = Mollie::Amount.new(amount)
end

def archived?
archived
end

def created_at=(created_at)
@created_at = Time.parse(created_at.to_s)
end

def paid_at=(paid_at)
@paid_at = Time.parse(paid_at.to_s)
rescue
nil
end

def updated_at=(updated_at)
@updated_at = Time.parse(updated_at.to_s)
rescue
nil
end

def expires_at=(expires_at)
@expires_at = Time.parse(expires_at.to_s)
rescue
nil
end

def payment_link
Util.extract_url(links, "payment_link")
end

def payments(options = {})
resource_url = Util.extract_url(links, "self")
payments_url = File.join(resource_url, "/payments")

response = Mollie::Client.instance.perform_http_call("GET", payments_url, nil, {}, options)
Mollie::List.new(response, Mollie::Payment)
end
end
end
7 changes: 7 additions & 0 deletions test/fixtures/payment_links/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "Bicycle tires",
"amount": {
"currency": "EUR",
"value": "24.95"
}
}
32 changes: 32 additions & 0 deletions test/fixtures/payment_links/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"resource": "payment-link",
"id": "pl_4Y0eZitmBnQ6IDoMqZQKh",
"mode": "test",
"description": "Bicycle tires",
"archived": false,
"profile_id": "pfl_QkEhN94Ba",
"amount": {
"currency": "EUR",
"value": "24.95"
},
"webhook_url": "https://webshop.example.org/payment-links/webhook",
"redirect_url": "https://webshop.example.org/thanks",
"created_at": "2024-09-24T12:16:44+00:00",
"paid_at": null,
"updated_at": null,
"expires_at": null,
"_links": {
"self": {
"href": "https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh",
"type": "application/hal+json"
},
"paymentLink": {
"href": "https://paymentlink.mollie.com/payment/4Y0eZitmBnQ6IDoMqZQKh",
"type": "text/html"
},
"documentation": {
"href": "https://docs.mollie.com/reference/v2/payment-links-api/get-payment-link",
"type": "text/html"
}
}
}
31 changes: 31 additions & 0 deletions test/fixtures/payment_links/list-payments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"count": 3,
"_embedded": {
"payments": [
{
"resource": "payment",
"id": "tr_one"
},
{
"resource": "payment",
"id": "tr_two"
},
{
"resource": "payment",
"id": "tr_three"
}
]
},
"_links": {
"self": {
"href": "https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh/payments?limit=50",
"type": "application/hal+json"
},
"previous": null,
"next": null,
"documentation": {
"href": "https://docs.mollie.com/reference/get-payment-link-payments",
"type": "text/html"
}
}
}
31 changes: 31 additions & 0 deletions test/fixtures/payment_links/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"count": 3,
"_embedded": {
"payment_links": [
{
"resource": "payment-link",
"id": "pl_one"
},
{
"resource": "payment-link",
"id": "pl_two"
},
{
"resource": "payment-link",
"id": "pl_three"
}
]
},
"_links": {
"self": {
"href": "https://api.mollie.com/v2/payment-links?limit=50",
"type": "application/hal+json"
},
"previous": null,
"next": null,
"documentation": {
"href": "https://docs.mollie.com/reference/v2/payment-links-api/list-payment-links",
"type": "text/html"
}
}
}
4 changes: 4 additions & 0 deletions test/fixtures/payment_links/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "Car tires",
"archived": true
}
100 changes: 100 additions & 0 deletions test/mollie/payment_link_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
require "helper"

module Mollie
class PaymentLinkTest < Test::Unit::TestCase
CREATE_PAYMENT_LINK = read_fixture("payment_links/create.json")
GET_PAYMENT_LINK = read_fixture("payment_links/get.json")
UPDATE_PAYMENT_LINK = read_fixture("payment_links/update.json")
LIST_PAYMENT_LINKS = read_fixture("payment_links/list.json")
LIST_PAYMENT_LINK_PAYMENTS = read_fixture("payment_links/list-payments.json")

def test_archived?
assert PaymentLink.new(archived: true).archived?
assert_false PaymentLink.new(archived: false).archived?
end

def test_create_payment
minified_body = JSON.parse(CREATE_PAYMENT_LINK).to_json
stub_request(:post, "https://api.mollie.com/v2/payment-links")
.with(body: minified_body)
.to_return(status: 201, body: GET_PAYMENT_LINK, headers: {})

payment_link = PaymentLink.create(
description: "Bicycle tires",
amount: {currency: "EUR", value: "24.95"}
)

assert_kind_of PaymentLink, payment_link
assert_equal "pl_4Y0eZitmBnQ6IDoMqZQKh", payment_link.id
end

def test_get_payment_link
stub_request(:get, "https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh")
.to_return(status: 200, body: GET_PAYMENT_LINK, headers: {})

payment_link = PaymentLink.get("pl_4Y0eZitmBnQ6IDoMqZQKh")

assert_equal "pl_4Y0eZitmBnQ6IDoMqZQKh", payment_link.id
assert_equal "test", payment_link.mode
assert_equal "Bicycle tires", payment_link.description
assert_false payment_link.archived
assert_equal "pfl_QkEhN94Ba", payment_link.profile_id
assert_equal 24.95, payment_link.amount.value
assert_equal "EUR", payment_link.amount.currency
assert_equal "https://webshop.example.org/payment-links/webhook", payment_link.webhook_url
assert_equal "https://webshop.example.org/thanks", payment_link.redirect_url
assert_equal Time.parse("2024-09-24T12:16:44+00:00"), payment_link.created_at
assert_equal "https://paymentlink.mollie.com/payment/4Y0eZitmBnQ6IDoMqZQKh", payment_link.payment_link
assert_nil payment_link.paid_at
assert_nil payment_link.updated_at
assert_nil payment_link.expires_at
end

def test_update_payment_link
minified_body = JSON.parse(UPDATE_PAYMENT_LINK).to_json
stub_request(:patch, 'https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh')
.with(body: minified_body)
.to_return(status: 200, body: GET_PAYMENT_LINK, headers: {})

payment_link = PaymentLink.update("pl_4Y0eZitmBnQ6IDoMqZQKh", JSON.parse(UPDATE_PAYMENT_LINK))

assert_kind_of PaymentLink, payment_link
assert_equal "pl_4Y0eZitmBnQ6IDoMqZQKh", payment_link.id
end

def test_delete_payment_link
stub_request(:delete, 'https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh')
.to_return(status: 204, body: GET_PAYMENT_LINK, headers: {})

payment_link = PaymentLink.delete("pl_4Y0eZitmBnQ6IDoMqZQKh")
assert_nil payment_link
end

def test_list_payment_links
stub_request(:get, "https://api.mollie.com/v2/payment-links")
.to_return(status: 200, body: LIST_PAYMENT_LINKS, headers: {})

payment_links = PaymentLink.all
assert_equal 3, payment_links.size
assert_equal "pl_one", payment_links[0].id
assert_equal "pl_two", payment_links[1].id
assert_equal "pl_three", payment_links[2].id
end

def test_list_payment_link_payments
stub_request(:get, "https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh")
.to_return(status: 200, body: GET_PAYMENT_LINK, headers: {})

stub_request(:get, "https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh/payments")
.to_return(status: 200, body: LIST_PAYMENT_LINK_PAYMENTS, headers: {})

payment_link = PaymentLink.get("pl_4Y0eZitmBnQ6IDoMqZQKh")
payments = payment_link.payments

assert_equal 3, payments.size
assert_equal "tr_one", payments[0].id
assert_equal "tr_two", payments[1].id
assert_equal "tr_three", payments[2].id
end
end
end
Loading