postfinancecheckout-rails 0.1.0

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
+ SHA256:
3
+ metadata.gz: 4350f23c542a383696267a7d8e0543fe3b49a860a219829524b54b3cad896b19
4
+ data.tar.gz: c18ac8b2c1f7d121561fd294b70f8827a593d042ee3c18849bf3a774a955a9e7
5
+ SHA512:
6
+ metadata.gz: f61316645b66338e0e4d394393e71b2573180b890ca21547a61575a4466a1294136f540df5daedad8b67ad2482773021744ed0502e90175cd4468aaf89cd326f
7
+ data.tar.gz: 2c74972d00713039eb5acb6d5f2c5d1ca60220dd6a4f903ce66402c9013daffef6fbe57d63c7f5a9e6db093e3abd4f042934c3cd70b54b2bc5e298c0b7ea0d64
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Lukas_Skywalker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # Postfinance Checkout
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+
6
+ Add a `transaction_id` field to your orders model:
7
+
8
+ ```bash
9
+ rails generate migration AddTransactionIdToOrders transaction_id:string
10
+ ```
11
+
12
+ Configure the gem and set the callbacks:
13
+
14
+ ```ruby
15
+ Postfinancecheckout.configure do |config|
16
+ config.space_id = 12345
17
+ config.app_user_id = 56789
18
+ config.app_user_key = "my_secret_key"
19
+
20
+ config.on_success = -> (transaction_id) do
21
+ order = Order.find_by(transaction_id: transaction_id)
22
+ order.update(status: :paid)
23
+
24
+ PaymentSuccessMailer.deliver_later(order)
25
+ end
26
+
27
+ config.on_failure = -> (transaction_id) do
28
+ order = Order.find_by(transaction_id: transaction_id)
29
+ order.update(status: :failed)
30
+
31
+ PaymentFailedMailer.deliver_later(order)
32
+ end
33
+ end
34
+ ```
35
+
36
+ Create a transaction and store the transaction ID:
37
+
38
+ ```ruby
39
+ transaction = Postfinancecheckout::Transaction.new(
40
+ amount: 34.5,
41
+ order_number: '1234',
42
+ email: 'test@example.com',
43
+ address: {
44
+ first_name: 'Testbenutzer',
45
+ last_name: 'Muster',
46
+ street: 'Hauptstrasse 123',
47
+ post_code: 3019,
48
+ city: 'Chäs und Brot',
49
+ state: 'BE',
50
+ country_code: 'CH'
51
+ }
52
+ )
53
+ transaction.save
54
+
55
+ @order.update(transaction_id: transaction.id)
56
+ ```
57
+
58
+ Once you have saved the transaction, generate the payment URL and redirect the user:
59
+
60
+ ```ruby
61
+ redirect_to transaction.payment_url
62
+ ```
63
+
64
+ ## Installation
65
+
66
+ Add this line to your application's Gemfile:
67
+
68
+ ```ruby
69
+ gem "postfinancecheckout-rails"
70
+ ```
71
+
72
+ And then execute:
73
+ ```bash
74
+ $ bundle
75
+ ```
76
+
77
+ Or install it yourself as:
78
+ ```bash
79
+ $ gem install postfinancecheckout-rails
80
+ ```
81
+
82
+ ## Development
83
+
84
+ Build the gem:
85
+
86
+ ```bash
87
+ rake build
88
+ ```
89
+
90
+ Push it to rubygems.org:
91
+
92
+ ```bash
93
+ rake release
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ Contribution directions go here.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/postfinancecheckout .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module Postfinancecheckout
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ module Postfinancecheckout
2
+ class WebhooksController < ApplicationController
3
+ skip_before_action :verify_authenticity_token
4
+
5
+ def create
6
+ transaction_id = params[:entityId]
7
+
8
+ transaction = Postfinancecheckout::Transaction.find(transaction_id)
9
+ if transaction.status == 'FULFILL'
10
+ Postfinancecheckout.config.on_success.call(transaction_id)
11
+ elsif transaction.status == 'FAILED' || transaction.status == 'VOIDED' || transaction.status == 'DECLINE'
12
+ Postfinancecheckout.config.on_failure.call(transaction_id)
13
+ end
14
+
15
+ head :ok
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Postfinancecheckout
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Postfinancecheckout
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Postfinancecheckout
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Postfinancecheckout
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Postfinancecheckout</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "postfinancecheckout/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Postfinancecheckout::Engine.routes.draw do
2
+ post :webhook, to: 'webhooks#create'
3
+ end
@@ -0,0 +1,5 @@
1
+ module Postfinancecheckout
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Postfinancecheckout
4
+ end
5
+ end
@@ -0,0 +1,142 @@
1
+ require 'postfinancecheckout-ruby-sdk'
2
+
3
+ module Postfinancecheckout
4
+ class Transaction
5
+ attr_reader :id
6
+
7
+ def initialize(amount: 0, email: '', address: {}, order_number: '', id: nil, success_url: '', failure_url: '')
8
+ @amount = amount
9
+ @email = email
10
+ @address = address
11
+ @order_number = order_number
12
+ @id = id
13
+ @success_url = success_url
14
+ @failure_url = failure_url
15
+
16
+ PostFinanceCheckout.configure do |config|
17
+ config.user_id = app_user_id
18
+ config.authentication_key = app_user_key
19
+ end
20
+ end
21
+
22
+ def save
23
+ existing = transaction_service.search(space_id, {
24
+ filter: {
25
+ fieldName: "merchantReference",
26
+ operator: "EQUALS",
27
+ type: "LEAF",
28
+ value: "order-#{@order_number}"
29
+ }
30
+ })
31
+ if existing.any?
32
+ @id = existing.first.id
33
+ else
34
+ tx = transaction_service.create(space_id, transaction_object)
35
+ @id = tx.id
36
+ end
37
+ self
38
+ end
39
+
40
+ def self.create(amount: 0, email: '', address: {}, order_number: '')
41
+ transaction = self.new(amount: amount, email: email, address: address, order_number: order_number)
42
+ transaction.save
43
+ transaction
44
+ end
45
+
46
+ def self.find(transaction_id)
47
+ transaction = self.new(amount: nil, email: nil, address: {}, order_number: nil, id: transaction_id)
48
+ transaction
49
+ end
50
+
51
+ def payment_url
52
+ ensure_id!
53
+ transaction_payment_page_service.payment_page_url(space_id, @id)
54
+ end
55
+
56
+ def status
57
+ ensure_id!
58
+ transaction = transaction_service.read(space_id, @id)
59
+ transaction.state
60
+ end
61
+
62
+ private
63
+
64
+ def space_id
65
+ Postfinancecheckout.config.space_id
66
+ end
67
+
68
+ def app_user_id
69
+ Postfinancecheckout.config.app_user_id
70
+ end
71
+
72
+ def app_user_key
73
+ Postfinancecheckout.config.app_user_key
74
+ end
75
+
76
+ def ensure_id!
77
+ raise StandardError, "Transaction must be saved" if @id.nil?
78
+ end
79
+
80
+ def ensure_not_id!
81
+ raise StandardError, "Transaction already saved" if @id
82
+ end
83
+
84
+ def address_object
85
+ PostFinanceCheckout::AddressCreate.new({
86
+ emailAddress: @email,
87
+ givenName: @address[:first_name],
88
+ familyName: @address[:last_name],
89
+ street: @address[:street],
90
+ postCode: @address[:post_code],
91
+ city: @address[:city],
92
+ postalState: @address[:state],
93
+ country: @address[:country_code],
94
+ })
95
+ end
96
+
97
+ def line_item_object
98
+ PostFinanceCheckout::LineItemCreate.new({
99
+ amountIncludingTax: @amount,
100
+ name: "Bestellung #{@order_number}",
101
+ quantity: 1,
102
+ shippingRequired: true,
103
+ #sku: "test-product",
104
+ #taxes: [
105
+ # PostFinanceCheckout::TaxCreate.new({
106
+ # rate: 8,
107
+ # title: "VAT"
108
+ # })
109
+ #],
110
+ type: PostFinanceCheckout::LineItemType::PRODUCT,
111
+ uniqueId: "order-#{@order_number}",
112
+ })
113
+ end
114
+
115
+ def transaction_object
116
+ PostFinanceCheckout::TransactionCreate.new({
117
+ billingAddress: address_object,
118
+ currency: 'CHF',
119
+ customerEmailAddress: @email,
120
+ customerPresence: PostFinanceCheckout::CustomersPresence::VIRTUAL_PRESENT,
121
+ invoiceMerchantReference: "Bestellung-#{@order_number}",
122
+ language: "de_CH",
123
+ lineItems: [
124
+ line_item_object
125
+ ],
126
+ merchantReference: "order-#{@order_number}",
127
+ shippingAddress: address_object,
128
+ #shippingMethod: "Test Shipping",
129
+ successUrl: @success_url,
130
+ failedUrl: @failure_url,
131
+ })
132
+ end
133
+
134
+ def transaction_service
135
+ @transaction_service ||= PostFinanceCheckout::TransactionService.new
136
+ end
137
+
138
+ def transaction_payment_page_service
139
+ @transaction_payment_page_service ||= PostFinanceCheckout::TransactionPaymentPageService.new
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ module Postfinancecheckout
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'dry-configurable'
2
+ require "postfinancecheckout/version"
3
+ require "postfinancecheckout/engine"
4
+ require "postfinancecheckout/transaction"
5
+
6
+ module Postfinancecheckout
7
+ extend Dry::Configurable
8
+
9
+ setting :on_success
10
+ setting :on_failure
11
+
12
+ setting :space_id
13
+ setting :app_user_id
14
+ setting :app_user_key
15
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :postfinancecheckout do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postfinancecheckout-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lukas_Skywalker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: postfinancecheckout-ruby-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-configurable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.0
55
+ description: Simple integration of Postfinance Checkout for Rails applications
56
+ email:
57
+ - lukas.diener@hotmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/assets/config/postfinancecheckout_manifest.js
66
+ - app/assets/stylesheets/postfinancecheckout/application.css
67
+ - app/controllers/postfinancecheckout/application_controller.rb
68
+ - app/controllers/postfinancecheckout/webhooks_controller.rb
69
+ - app/helpers/postfinancecheckout/application_helper.rb
70
+ - app/jobs/postfinancecheckout/application_job.rb
71
+ - app/mailers/postfinancecheckout/application_mailer.rb
72
+ - app/models/postfinancecheckout/application_record.rb
73
+ - app/views/layouts/postfinancecheckout/application.html.erb
74
+ - config/routes.rb
75
+ - lib/postfinancecheckout.rb
76
+ - lib/postfinancecheckout/engine.rb
77
+ - lib/postfinancecheckout/transaction.rb
78
+ - lib/postfinancecheckout/version.rb
79
+ - lib/tasks/postfinancecheckout_tasks.rake
80
+ homepage: https://github.com/code-fabrik/postfinancecheckout
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ homepage_uri: https://github.com/code-fabrik/postfinancecheckout
86
+ source_code_uri: https://github.com/code-fabrik/postfinancecheckout
87
+ changelog_uri: https://raw.githubusercontent.com/code-fabrik/postfinancecheckout/master/CHANGELOG.md
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.4.10
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Postfinance Checkout for Rails
107
+ test_files: []