payfast 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d41b22cbcd1597b41a852ebf7ea32eb7445fd54cc5c4ba662b8424f1c2756f89
4
- data.tar.gz: 3cdc26d5b850b334e9e2eceac47f5b1ac2dffb89344c0904072604e07f19a047
3
+ metadata.gz: 0d93902540cd8d2052c2c21f4c1ad469be1cbb9532f3c6ba20bcaa4d84047b28
4
+ data.tar.gz: beef56fd565707a474944374d320bac4a369a10e68c4ac35c4d462e58cd38186
5
5
  SHA512:
6
- metadata.gz: 77077256af0d8da43b3a22573183afa496faca553887464a18c535f05bb1db4831f44ac05c555c510f46a657956368b8f36ac796be0025707f3b9e1bf8d6db3e
7
- data.tar.gz: 5702c504a0bc374cbbb590d59c917652b37c85b68ad5ec151cb16892674fd7847a37787e778ab617e1e3efaa85dae2511466e2d508a880d1de2dd88887c487fb
6
+ metadata.gz: be4c605758122af4e5fdb7e768852e0c76f02b6ee97e4bdf95eb8fe1a8dec2dabe4aae87e72c28e9e49a6f4a0a380a501d917666cccd09dd6845ad1a3c75ee9d
7
+ data.tar.gz: 5540c7288967670d4e87f87f32de82519b1ff127ec3e43c37c960ddc2bace989c9f8e25d28ee35d3160bee62aea10d7d84be6f4c911e6bf2e8c70a13e998132c
data/lib/generators.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "rails/generators"
2
+
3
+ module Payfast
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def modify_routes
9
+ insert_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
10
+ # Read and insert the contents of your routes template file
11
+ File.read(File.join(__dir__, "templates", "routes.rb"))
12
+ end
13
+ end
14
+
15
+ def create_controller
16
+ template "controller.rb", File.join("app/controllers", "#{controller_file_name}_controller.rb")
17
+ end
18
+
19
+ def create_views
20
+ actions.each do |action|
21
+ template "#{action}.html.erb", File.join("app/views", controller_file_name, "#{action}.html.erb")
22
+ end
23
+ end
24
+
25
+ def create_view_helper
26
+ template "helper.rb", "app/helpers/carts_helper.rb"
27
+ end
28
+
29
+ def create_migration
30
+ template "migration.rb", "db/migrate/#{timestamp}_create_carts.rb"
31
+ end
32
+
33
+ def create_config_file
34
+ template "config.yml", "config/payfast.yml"
35
+ end
36
+
37
+ def create_model
38
+ template "model.rb", "app/models/cart.rb"
39
+ end
40
+
41
+ private
42
+
43
+ def controller_file_name
44
+ "carts"
45
+ end
46
+
47
+ def timestamp
48
+ Time.now.strftime("%Y%m%d%H%M%S")
49
+ end
50
+
51
+ def actions
52
+ %w(index make_payment)
53
+ end
54
+ end
55
+ end
56
+ 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
@@ -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
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.1
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.rb
64
+ - lib/onsite_payments.rb
65
+ - lib/templates/config.yml
66
+ - lib/templates/controller.rb
67
+ - lib/templates/helper.rb
68
+ - lib/templates/index.html.erb
69
+ - lib/templates/make_payment.html.erb
70
+ - lib/templates/migration.rb
71
+ - lib/templates/model.rb
72
+ - lib/templates/routes.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
  - - ">="