Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Ruby Gems
/
var
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Abraham Rodriguez
2016-01-02 18:10:41 -0600
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
f591b874d22d86655d7a465049e8e47688e81b11
f591b874
1 parent
1f4ba7f0
Add base integration with paypal
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
15 deletions
lib/var.rb
lib/var.rb
View file @
f591b87
require
"var/version"
require
'conekta'
require
'paypal-sdk-rest'
module
Var
# TODO: add paypal
# @@valid_services = [:conekta, :paypal]
@@valid_services
=
[
:conekta
]
def
self
.
valid_services
...
...
@@ -15,7 +18,7 @@ module Var
if
(
!
object
.
respond_to?
(
:charge_with
))
return
{
error_message:
"
#{
object
.
class
.
to_s
}
doesn't support charges"
}
end
object
.
charge_with
(
:conekta
,
options
)
object
.
charge_with
(
service
,
options
)
end
end
...
...
@@ -32,6 +35,11 @@ module ActsAsChargeable
module
ChargeableInstanceMethods
def
charge_with
(
service
,
options
)
if
!
instance_support?
(
service
)
error_message
=
"
#{
self
.
class
.
to_s
}
doesn't support"
\
" charges with
#{
service
.
to_s
}
"
return
{
error_message:
error_message
}
end
begin
self
.
send
(
"charge_with_
#{
service
.
to_s
}
"
,
options
)
rescue
=>
exception
...
...
@@ -45,28 +53,62 @@ module ActsAsChargeable
return
{
error_message:
error_message
}
end
@charge
||=
Conekta
::
Charge
.
create
({
description:
self
.
sync
_attribute
(
'description'
),
amount:
self
.
sync
_attribute
(
'amount'
),
description:
self
.
sync
(
:conekta
,
'description'
),
amount:
self
.
sync
(
:conekta
,
'amount'
),
currency:
"MXN"
,
reference_id:
self
.
sync
_attribute
(
'reference_id'
),
reference_id:
self
.
sync
(
:conekta
,
'reference_id'
),
card:
options
[
:card_token
],
details:
{
name:
self
.
sync
_attribute
(
'details_
name'
),
email:
self
.
sync
_attribute
(
'details_
email'
),
name:
self
.
sync
(
:conekta
,
'
name'
),
email:
self
.
sync
(
:conekta
,
'
email'
),
line_items:
[{
name:
self
.
sync
_attribute
(
'details_
name'
),
description:
self
.
sync
_attribute
(
'description'
),
unit_price:
self
.
sync
_attribute
(
'amount'
),
name:
self
.
sync
(
:conekta
,
'
name'
),
description:
self
.
sync
(
:conekta
,
'description'
),
unit_price:
self
.
sync
(
:conekta
,
'amount'
),
quantity:
1
,
}]
}
})
end
def
charge_with_paypal
(
options
)
if
(
!
options
.
include?
:card
)
error_message
=
"Paypal needs a card sent as a third paramater"
return
{
error_message:
error_message
}
end
@payment
=
PayPal
::
SDK
::
REST
::
Payment
.
new
({
intent:
"sale"
,
payer:
{
payer_info:
{
email:
self
.
sync
(
:paypal
,
'email'
)},
payment_method:
"credit_card"
,
funding_instruments:
[{
credit_card:
{
type:
options
[
:card
][
:type
],
number:
options
[
:card
][
:number
],
expire_month:
options
[
:card
][
:expire_month
],
expire_year:
options
[
:card
][
:expire_year
],
cvv2:
options
[
:card
][
:cvv2
]}}]},
transactions:
[{
item_list:
{
items:
[{
name:
self
.
sync
(
:paypal
,
'name'
),
sku:
self
.
sync
(
:paypal
,
'sku'
),
price:
self
.
sync
(
:paypal
,
'price'
),
currency:
"MXN"
,
quantity:
1
}]},
amount:
{
total:
self
.
sync
(
:paypal
,
'price'
),
currency:
"MXN"
},
description:
self
.
sync
(
:paypal
,
'description'
)
}]})
end
def
find_charge
(
service
)
begin
if
(
!
Var
.
valid_services
.
include?
(
service
))
return
{
error_message:
'Service is not supported'
}
if
!
instance_support?
(
service
)
error_message
=
"
#{
self
.
class
.
to_s
}
doesn't support"
\
" charges with
#{
service
.
to_s
}
"
return
{
error_message:
error_message
}
end
self
.
send
(
"find_
#{
service
.
to_s
}
_charge"
)
rescue
=>
exception
...
...
@@ -75,13 +117,33 @@ module ActsAsChargeable
end
def
find_conekta_charge
ref_id
=
self
.
sync
_attribute
(
'reference_id'
)
ref_id
=
self
.
sync
(
:conekta
,
'reference_id'
)
Conekta
::
Charge
.
where
({
'status.ne'
=>
'paid'
,
'reference_id'
=>
ref_id
})
end
def
sync_attribute
(
key
)
return
self
.
send
(
key
)
unless
self
.
sync_attributes
.
include?
key
.
to_sym
self
.
send
(
sync_attributes
[
key
.
to_sym
])
def
instance_support?
(
service
)
return
self
.
sync_attributes
.
include?
(
service
)
end
def
sync
(
service
,
key
)
service_attributes
=
self
.
send
(
"
#{
service
.
to_s
}
_attributes"
)
if
!
service_attributes
.
include?
key
.
to_sym
return
self
.
send
(
key
)
end
self
.
send
(
service_attributes
[
key
.
to_sym
])
end
# def sync_attribute(key)
# return self.send(key) unless self.sync_attributes.include? key.to_sym
# self.send(sync_attributes[key.to_sym])
# end
def
conekta_attributes
self
.
sync_attributes
[
:conekta
]
||
{}
end
def
paypal_attributes
self
.
sync_attributes
[
:paypal
]
||
{}
end
end
end
...
...
Please
register
or
login
to post a comment