payfast 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d41b22cbcd1597b41a852ebf7ea32eb7445fd54cc5c4ba662b8424f1c2756f89
4
- data.tar.gz: 3cdc26d5b850b334e9e2eceac47f5b1ac2dffb89344c0904072604e07f19a047
3
+ metadata.gz: 3a4574ba86a681658b8f23c60ffd4139e6d59d6abc5017a78042f05b302624e4
4
+ data.tar.gz: 47ed239d23fcd5966db19cefdb119ad8b0366e61dfed31c21d80ad848508f4e9
5
5
  SHA512:
6
- metadata.gz: 77077256af0d8da43b3a22573183afa496faca553887464a18c535f05bb1db4831f44ac05c555c510f46a657956368b8f36ac796be0025707f3b9e1bf8d6db3e
7
- data.tar.gz: 5702c504a0bc374cbbb590d59c917652b37c85b68ad5ec151cb16892674fd7847a37787e778ab617e1e3efaa85dae2511466e2d508a880d1de2dd88887c487fb
6
+ metadata.gz: 3398d67e68303c866e80d60bafe730ee3093d9bddb18ab499d0f50017dc5fb3300ac512597fc7f8d49d73b1cbfdd74c362f881778d314e4845ddc47eb8727661
7
+ data.tar.gz: ab136a269460fb4d03de079ea2195a5aa9be3d5bea253c3046f948da648ef2e7992b2aa6849a9ddd4832d9d6a8b5c75477834c2cdf0b448a05c049a0446c2847
@@ -0,0 +1,54 @@
1
+ require "rails/generators"
2
+
3
+ module Payfast
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ def modify_routes
8
+ insert_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
9
+ # Read and insert the contents of your routes template file
10
+ File.read(File.join(__dir__, "templates", "routes.rb"))
11
+ end
12
+ end
13
+
14
+ def create_controller
15
+ template "controller.rb", File.join("app/controllers", "#{controller_file_name}_controller.rb")
16
+ end
17
+
18
+ def create_views
19
+ actions.each do |action|
20
+ template "#{action}.html.erb", File.join("app/views", controller_file_name, "#{action}.html.erb")
21
+ end
22
+ end
23
+
24
+ def create_view_helper
25
+ template "helper.rb", "app/helpers/carts_helper.rb"
26
+ end
27
+
28
+ def create_migration
29
+ template "migration.rb", "db/migrate/#{timestamp}_create_carts.rb"
30
+ end
31
+
32
+ def create_config_file
33
+ template "config.yml", "config/payfast.yml"
34
+ end
35
+
36
+ def create_model
37
+ template "model.rb", "app/models/cart.rb"
38
+ end
39
+
40
+ private
41
+
42
+ def controller_file_name
43
+ "carts"
44
+ end
45
+
46
+ def timestamp
47
+ Time.now.strftime("%Y%m%d%H%M%S")
48
+ end
49
+
50
+ def actions
51
+ %w(index make_payment)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ development:
2
+ merchant_id: <%#= Rails.application.credentials.payfast.merchant_id %>
3
+ merchant_key: <%#= Rails.application.credentials.payfast.merchant_key %>
4
+ passphrase: <%#= Rails.application.credentials.payfast.passphrase %>
5
+ base_url: https://sandbox.payfast.co.za/onsite/process
6
+ # Replace with your own urls
7
+ return_url: https://1c8e-102-69-134-39.ngrok-free.app/carts/return_url
8
+ cancel_url: https://1c8e-102-69-134-39.ngrok-free.app/carts/cancel
9
+ notify_url: https://1c8e-102-69-134-39.ngrok-free.app/carts/notify
10
+ js_bundle: https://sandbox.payfast.co.za/onsite/engine.js
11
+ production:
12
+ merchant_id: <%#= Rails.application.credentials.payfast_merchant_id %>
13
+ merchant_key: <%#= Rails.application.credentials.payfast_merchant_key %>
14
+ passphrase: <%#= Rails.application.credentials.passphrase %>
15
+ base_url: https://www.payfast.co.za/onsite/process
16
+ # Replace with your own urls
17
+ return_url: https://1c8e-102-69-134-39.ngrok-free.app/carts/return_url
18
+ cancel_url: https://1c8e-102-69-134-39.ngrok-free.app/carts/cancel
19
+ notify_url: https://1c8e-102-69-134-39.ngrok-free.app/carts/notify
20
+ js_bundle: https://www.payfast.co.za/onsite/engine.js
@@ -0,0 +1,47 @@
1
+ class CartsController < ApplicationController
2
+ def index
3
+ end
4
+
5
+ def new
6
+ @cart = Cart.new
7
+ end
8
+
9
+ def create
10
+ payload = {
11
+ email_address: cart_params[:email_address],
12
+ amount: cart_params[:amount],
13
+ item_name: cart_params[:item_name],
14
+ }
15
+
16
+ payment_identifier = Payfast::OnsitePayments.requestPayment(payload)
17
+
18
+ @cart = Cart.new(cart_params)
19
+ @cart.payment_uuid = payment_identifier["uuid"]
20
+
21
+ if @cart.save
22
+ redirect_to make_payment_cart_path(@cart)
23
+ else
24
+ render :new, status: :unprocessable_entity
25
+ end
26
+ end
27
+
28
+ def make_payment
29
+ @cart = Cart.find(params[:id])
30
+ end
31
+
32
+ def success
33
+ @cart = Cart.find(params[:id])
34
+ @cart.is_paid = true
35
+ redirect_to root_path, notice: "Transcation Successful"
36
+ end
37
+
38
+ def failure
39
+ redirect_to root_path, notice: "Transcation Canceled"
40
+ end
41
+
42
+ private
43
+
44
+ def cart_params
45
+ params.require(:cart).permit(:item_name, :amount, :email_address)
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ module CartsHelper
2
+ def payfast_script_tag
3
+ script_src = Rails.application.config_for(:payfast).js_bundle
4
+ content_tag(:script, "", src: script_src)
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ <form action="/carts" method="POST">
2
+ <!-- uncomment this and surround this erb tags -->
3
+ <!-- hidden_field_tag :authenticity_token, form_authenticity_token -->
4
+ <input type="hidden" name="authenticity_token" >
5
+ <input type="hidden" name="cart[amount]" value="500">
6
+ <input type="hidden" name="cart[email_address]" value="test@test.com">
7
+ <input type="hidden" name="cart[item_name]" value="test item">
8
+
9
+ <p>Item R500 </p>
10
+ <input type="submit" name="commit" value="Pay" data-disable-with="Pay">
11
+ </form>
@@ -0,0 +1,15 @@
1
+
2
+ <script>
3
+ // set the uuid to uuid = @cart.payment_uuid. surround @carts.payment_uuid with erb tags
4
+ const uuid = ``
5
+ window.payfast_do_onsite_payment({uuid}, function (result) {
6
+ if (result === true) {
7
+ // redirect success_cart_path(@cart)
8
+ window.location.href = ``
9
+ }
10
+ else {
11
+ // Redirect to failure_cart_path(@cart)
12
+ window.location.href = ``
13
+ }
14
+ });
15
+ </script>
@@ -0,0 +1,12 @@
1
+ class CreateCarts < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :carts do |t|
4
+ t.float :amount
5
+ t.string :item_name
6
+ t.string :email_address
7
+ t.string :payment_uuid
8
+ t.boolean :is_paid
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,2 @@
1
+ class Cart < ApplicationRecord
2
+ end
@@ -0,0 +1,7 @@
1
+ resources :carts do
2
+ member do
3
+ get "make-payment", to: "carts#make_payment", as: :make_payment
4
+ get "success", to: "carts#success", as: :success
5
+ get "failure", to: "carts#failure", as: :failure
6
+ end
7
+ end
@@ -0,0 +1,64 @@
1
+ require "net/http"
2
+ require "uri"
3
+
4
+ module Payfast
5
+ class OnsitePayments
6
+ def self.generate_signature(payload)
7
+ passphrase = Rails.application.config_for(:payfast).passphrase
8
+
9
+ if passphrase
10
+ payload[:passphrase] = passphrase
11
+ end
12
+
13
+ url_encoded = data_to_string(payload)
14
+ create_hash(url_encoded, "md5")
15
+ end
16
+
17
+ def self.requestPayment(payload)
18
+ payload_with_config = {
19
+ merchant_id: Rails.application.config_for(:payfast).merchant_id,
20
+ merchant_key: Rails.application.config_for(:payfast).merchant_key,
21
+ return_url: Rails.application.config_for(:payfast).return_url,
22
+ cancel_url: Rails.application.config_for(:payfast).cancel_url,
23
+ notify_url: Rails.application.config_for(:payfast).notify_url,
24
+ }.merge(payload)
25
+
26
+ puts payload_with_config
27
+
28
+ signature = generate_signature(payload_with_config)
29
+ payload_with_config[:signature] = signature
30
+
31
+ pf_param_string = data_to_string(payload_with_config)
32
+ base_url = Rails.application.config_for(:payfast).base_url
33
+
34
+ puts pf_param_string
35
+
36
+ uri = URI.parse(base_url)
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ http.use_ssl = true if uri.scheme == "https"
39
+
40
+ request = Net::HTTP::Post.new(uri.path)
41
+ request.body = pf_param_string
42
+
43
+ begin
44
+ response = http.request(request)
45
+ JSON.parse(response.body)
46
+ rescue StandardError => e
47
+ puts "Error: #{e.message}"
48
+ return false
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def self.data_to_string(payload)
55
+ URI.encode_www_form(payload)
56
+ end
57
+
58
+ def self.create_hash(data, algorithm)
59
+ digest = Digest.const_get(algorithm.upcase).new
60
+ digest.update(data)
61
+ digest.hexdigest
62
+ end
63
+ end
64
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: payfast
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dellan Muchengapadare
@@ -11,25 +11,47 @@ cert_chain: []
11
11
  date: 2023-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.8'
20
- - - ">="
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
21
25
  - !ruby/object:Gem::Version
22
- version: 2.8.0
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 11.2.2
23
34
  type: :development
24
35
  prerelease: false
25
36
  version_requirements: !ruby/object:Gem::Requirement
26
37
  requirements:
27
38
  - - "~>"
28
39
  - !ruby/object:Gem::Version
29
- version: '2.8'
30
- - - ">="
40
+ version: 11.2.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 7.0.7
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
31
53
  - !ruby/object:Gem::Version
32
- version: 2.8.0
54
+ version: 7.0.7
33
55
  description: ''
34
56
  email: mactunechy@gmail.com
35
57
  executables: []
@@ -38,6 +60,16 @@ extra_rdoc_files: []
38
60
  files:
39
61
  - LICENSE
40
62
  - README.md
63
+ - lib/generators/payfast/install_generator.rb
64
+ - lib/generators/payfast/templates/config.yml
65
+ - lib/generators/payfast/templates/controller.rb
66
+ - lib/generators/payfast/templates/helper.rb
67
+ - lib/generators/payfast/templates/index.html.erb
68
+ - lib/generators/payfast/templates/make_payment.html.erb
69
+ - lib/generators/payfast/templates/migration.rb
70
+ - lib/generators/payfast/templates/model.rb
71
+ - lib/generators/payfast/templates/routes.rb
72
+ - lib/payfast/onsite_payments.rb
41
73
  homepage: https://github.com/mactunechy/payfast-gem
42
74
  licenses:
43
75
  - MIT
@@ -50,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
82
  requirements:
51
83
  - - ">="
52
84
  - !ruby/object:Gem::Version
53
- version: 3.1.4
85
+ version: '0'
54
86
  required_rubygems_version: !ruby/object:Gem::Requirement
55
87
  requirements:
56
88
  - - ">="