opay 1.0.6 → 1.0.7

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.
@@ -1,7 +1,20 @@
1
1
  module Opay
2
2
  class Payment < ActiveRecord::Base
3
3
  belongs_to :payable, polymorphic: true
4
- attr_accessible :provider, :amount, :finished, :session_id
5
- validates :payable, :provider, :amount, :session_id, presence: true
4
+ attr_accessible :provider, :amount, :finished, :session_id, :status
5
+ validates :payable, :provider, :amount, presence: true
6
+
7
+ before_create do |p|
8
+ p.session_id = Payment.generate_session_id
9
+ end
10
+
11
+ def self.generate_session_id
12
+ # Generate a token by looping and ensuring does not already exist.
13
+ loop do
14
+ token = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
15
+ break token unless Payment.where(session_id: token).first
16
+ end
17
+ end
18
+
6
19
  end
7
20
  end
@@ -2,13 +2,15 @@ class CreateOpayPayments < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :opay_payments do |t|
4
4
  t.references :payable, polymorphic: true
5
- t.string :session_id
6
- t.string :provider
7
- t.integer :amount
5
+ t.string :session_id, null: false
6
+ t.string :provider, null: false
7
+ t.integer :amount, null: false
8
8
  t.boolean :finished, null: false, default: false
9
+ t.string :status
9
10
 
10
11
  t.timestamps
11
12
  end
12
13
  add_index :opay_payments, [:payable_id, :payable_type]
14
+ add_index :opay_payments, :session_id, unique: true
13
15
  end
14
16
  end
@@ -1,5 +1,6 @@
1
1
  module Opay
2
2
  class FormBuilder < ActionView::Helpers::FormBuilder
3
+ include Opay::Helpers::FormHelper
3
4
  include Opay::Helpers::PayuHelper
4
5
  end
5
6
  end
@@ -8,6 +8,10 @@ module Opay
8
8
  payu_form_for(record, options, &block)
9
9
  end
10
10
 
11
+ def payment_info(options = {})
12
+ payu_payment_info(options)
13
+ end
14
+
11
15
  end
12
16
 
13
17
  end
@@ -9,12 +9,13 @@ module Opay
9
9
  options[:url] = Opay::Providers::Payu.url(:new_payment)
10
10
  options[:html] = { id: "payu_payment_form_#{record.id}", class: 'payu_payment_form' }
11
11
 
12
- record.create_payment!(session_id: record.payment_session_id, provider: 'payu', amount: record.amount) if record.payment.blank?
12
+ record.prepare_payment
13
+ # record.create_payment!(session_id: record.payment_session_id, provider: 'payu', amount: record.amount) if record.payment.blank?
13
14
 
14
15
  form_for(record, options, &block)
15
16
  end
16
17
 
17
- def payment_info(options = {})
18
+ def payu_payment_info(options = {})
18
19
  options[:first_name] ||= object.first_name
19
20
  options[:last_name] ||= object.last_name
20
21
  options[:email] ||= object.email
data/lib/opay/payable.rb CHANGED
@@ -18,9 +18,18 @@ module Opay
18
18
  end
19
19
  end
20
20
 
21
+ def prepare_payment
22
+ if payment.blank?
23
+ create_payment!(provider: 'payu', amount: amount)
24
+ else
25
+ payment.update_attribute(:session_id, Payment.generate_session_id)
26
+ payment
27
+ end
28
+ end
29
+
21
30
  def payment_session_id
22
- raise 'Resource must be saved before payment' if id.nil?
23
- Digest::MD5.hexdigest(self.class.name + id.to_s)
31
+ raise 'Resource must be prepared before payment' if payment.blank?
32
+ payment.session_id
24
33
  end
25
34
 
26
35
  module ClassMethods
data/lib/opay/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Opay
2
- VERSION = '1.0.6'
2
+ VERSION = '1.0.7'
3
3
  end
Binary file