payment_recipes 1.0.1 → 1.1.0

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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/payment_recipes/paypal/rest/action/capture_authorization.rb +27 -0
  3. data/lib/payment_recipes/paypal/rest/action/create_payment.rb +73 -0
  4. data/lib/payment_recipes/paypal/rest/action/execute_payment.rb +25 -0
  5. data/lib/payment_recipes/paypal/rest/action/refund_capture.rb +24 -0
  6. data/lib/payment_recipes/paypal/rest/action/refund_sale.rb +24 -0
  7. data/lib/payment_recipes/paypal/rest/action/void_authorization.rb +24 -0
  8. data/lib/payment_recipes/paypal/rest/authorization.rb +172 -0
  9. data/lib/payment_recipes/paypal/rest/capture.rb +140 -0
  10. data/lib/payment_recipes/paypal/rest/payer.rb +38 -0
  11. data/lib/payment_recipes/paypal/rest/payment.rb +126 -0
  12. data/lib/payment_recipes/paypal/rest/refund.rb +160 -0
  13. data/lib/payment_recipes/paypal/rest/sale.rb +172 -0
  14. data/lib/payment_recipes/paypal/rest/settings.rb +18 -0
  15. data/lib/payment_recipes/paypal/rest/transaction.rb +118 -0
  16. data/lib/payment_recipes/paypal/soap/action/capture_authorization.rb +104 -0
  17. data/lib/payment_recipes/paypal/soap/action/create_express_checkout_payment.rb +76 -0
  18. data/lib/payment_recipes/paypal/soap/action/execute_express_checkout_payment.rb +131 -0
  19. data/lib/payment_recipes/paypal/soap/action/perform_direct_payment.rb +62 -0
  20. data/lib/payment_recipes/paypal/soap/settings.rb +22 -0
  21. data/lib/payment_recipes/paypal/soap/transaction.rb +116 -0
  22. data/lib/payment_recipes/utils/action.rb +145 -8
  23. data/lib/payment_recipes/version.rb +1 -1
  24. data/lib/payment_recipes.rb +24 -15
  25. metadata +37 -17
  26. data/lib/payment_recipes/paypal/action/capture_authorization.rb +0 -25
  27. data/lib/payment_recipes/paypal/action/create_payment.rb +0 -66
  28. data/lib/payment_recipes/paypal/action/execute_payment.rb +0 -17
  29. data/lib/payment_recipes/paypal/action/refund_capture.rb +0 -22
  30. data/lib/payment_recipes/paypal/action/refund_sale.rb +0 -22
  31. data/lib/payment_recipes/paypal/action/void_authorization.rb +0 -22
  32. data/lib/payment_recipes/paypal/authorization.rb +0 -170
  33. data/lib/payment_recipes/paypal/capture.rb +0 -138
  34. data/lib/payment_recipes/paypal/payer.rb +0 -36
  35. data/lib/payment_recipes/paypal/payment.rb +0 -124
  36. data/lib/payment_recipes/paypal/refund.rb +0 -158
  37. data/lib/payment_recipes/paypal/sale.rb +0 -170
  38. data/lib/payment_recipes/paypal/settings.rb +0 -14
  39. data/lib/payment_recipes/paypal/transaction.rb +0 -116
@@ -0,0 +1,160 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module REST
4
+ class Refund
5
+ attr_reader :id
6
+ attr_reader :currency
7
+ attr_reader :total
8
+ attr_reader :payment
9
+ attr_reader :payment_id
10
+ attr_reader :state
11
+
12
+ attr_reader :create_time
13
+ attr_reader :update_time
14
+
15
+ attr_reader :sale_id
16
+ attr_reader :sale
17
+
18
+ attr_reader :capture_id
19
+ attr_reader :capture
20
+
21
+ attr_reader :raw_refund
22
+
23
+ include ::PaymentRecipes::Utils::Converters
24
+ include ::PaymentRecipes::Utils::Equality
25
+
26
+ def initialize(paypal_refund, payment: nil)
27
+ unless paypal_refund.is_a?(::PayPal::SDK::REST::DataTypes::Refund)
28
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Refund"
29
+ end
30
+
31
+ if payment
32
+ unless payment.is_a?(::PaymentRecipes::PayPal::REST::Payment)
33
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::Payment"
34
+ end
35
+
36
+ @payment = payment
37
+ @payment_id = payment.id
38
+ end
39
+
40
+ extract_and_store(paypal_refund)
41
+ end
42
+
43
+ def reload!
44
+ paypal_refund = self.class.find_raw(@id)
45
+ extract_and_store(paypal_refund)
46
+
47
+ self
48
+ end
49
+
50
+ def reload_payment!
51
+ @payment = ::PaymentRecipes::PayPal::REST::Payment.find(@payment_id)
52
+
53
+ @payment
54
+ end
55
+
56
+ def reload_sale!
57
+ return unless @sale_id
58
+
59
+ @sale = ::PaymentRecipes::PayPal::REST::Sale.find(@sale_id)
60
+
61
+ @sale
62
+ end
63
+
64
+ def reload_capture!
65
+ return unless @capture_id
66
+
67
+ @capture = ::PaymentRecipes::PayPal::REST::Capture.find(@capture_id)
68
+
69
+ @capture
70
+ end
71
+
72
+ def payment
73
+ reload_payment! unless @payment
74
+
75
+ @payment
76
+ end
77
+
78
+ def capture
79
+ reload_capture! unless @capture
80
+
81
+ @capture
82
+ end
83
+
84
+ def sale
85
+ reload_sale! unless @sale
86
+
87
+ @sale
88
+ end
89
+
90
+ def authorizations
91
+ payment.authorizations
92
+ end
93
+
94
+ def authorization
95
+ payment.authorization
96
+ end
97
+
98
+ def pending?
99
+ @state == :pending
100
+ end
101
+
102
+ def completed?
103
+ @state == :completed
104
+ end
105
+
106
+ def failed?
107
+ @state == :failed
108
+ end
109
+
110
+ def inspect
111
+ to_str
112
+ end
113
+
114
+ def to_s
115
+ to_str
116
+ end
117
+
118
+ def to_str
119
+ "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
120
+ end
121
+
122
+ class << self
123
+ def find(id)
124
+ paypal_refund = find_raw(id)
125
+
126
+ if paypal_refund
127
+ new(paypal_refund, payment: nil)
128
+ else
129
+ nil
130
+ end
131
+ end
132
+
133
+ def find_raw(id)
134
+ begin
135
+ ::PayPal::SDK::REST::Refund.find(id)
136
+
137
+ rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
138
+ nil
139
+ end
140
+ end
141
+ end
142
+
143
+ def extract_and_store(paypal_refund)
144
+ @id = convert_to_string(paypal_refund.id)
145
+ @currency = convert_to_string(paypal_refund.amount.currency)
146
+ @total = convert_to_money(amount: paypal_refund.amount.total, currency: paypal_refund.amount.currency)
147
+ @payment_id = convert_to_string(paypal_refund.parent_payment)
148
+ @state = convert_to_symbol(paypal_refund.state)
149
+
150
+ @create_time = convert_to_time(paypal_refund.create_time)
151
+ @update_time = convert_to_time(paypal_refund.update_time)
152
+
153
+ @sale_id = convert_to_string(paypal_refund.sale_id)
154
+ @capture_id = convert_to_string(paypal_refund.capture_id)
155
+ @raw_refund = paypal_refund
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,172 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module REST
4
+ class Sale
5
+ attr_reader :currency
6
+ attr_reader :total
7
+ attr_reader :create_time
8
+ attr_reader :id
9
+ attr_reader :payment
10
+ attr_reader :payment_id
11
+ attr_reader :state
12
+ attr_reader :update_time
13
+
14
+ attr_reader :transaction_fee
15
+ attr_reader :payment_mode
16
+ attr_reader :protection_eligibility
17
+ attr_reader :protection_eligibility_type
18
+
19
+ attr_reader :expanded
20
+ attr_reader :raw_sale
21
+
22
+ include ::PaymentRecipes::Utils::Converters
23
+ include ::PaymentRecipes::Utils::Equality
24
+
25
+ def initialize(paypal_sale, payment: nil, expanded: false)
26
+ unless paypal_sale.is_a?(::PayPal::SDK::REST::DataTypes::Sale)
27
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Sale"
28
+ end
29
+
30
+ if payment
31
+ unless payment.is_a?(::PaymentRecipes::PayPal::REST::Payment)
32
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::Payment"
33
+ end
34
+
35
+ @payment = payment
36
+ @payment_id = payment.id
37
+ end
38
+
39
+ extract_and_store(paypal_sale)
40
+ @expanded = expanded
41
+ end
42
+
43
+ def reload!
44
+ paypal_sale = self.class.find_raw(@id)
45
+ extract_and_store(paypal_sale)
46
+ @expanded = true
47
+
48
+ self
49
+ end
50
+
51
+ def reload_payment!
52
+ @payment = ::PaymentRecipes::PayPal::REST::Payment.find(@payment_id)
53
+
54
+ @payment
55
+ end
56
+
57
+ def payment
58
+ reload_payment! unless @payment
59
+
60
+ @payment
61
+ end
62
+
63
+ def transaction_fee
64
+ reload! unless @transaction_fee
65
+
66
+ @transaction_fee
67
+ end
68
+
69
+ def payment_mode
70
+ reload! unless @payment_mode
71
+
72
+ @payment_mode
73
+ end
74
+
75
+ def protection_eligibility
76
+ reload! unless @protection_eligibility
77
+
78
+ @protection_eligibility
79
+ end
80
+
81
+ def protection_eligibility_type
82
+ reload! unless @protection_eligibility_type
83
+
84
+ @protection_eligibility_type
85
+ end
86
+
87
+ def refunds
88
+ payment.refunds.select do |refund|
89
+ refund.sale_id == @id
90
+ end
91
+ end
92
+
93
+ def refund
94
+ refunds.first
95
+ end
96
+
97
+ def inspect
98
+ to_str
99
+ end
100
+
101
+ def to_s
102
+ to_str
103
+ end
104
+
105
+ def to_str
106
+ "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
107
+ end
108
+
109
+ def can_be_refunded?
110
+ completed?
111
+ end
112
+
113
+ def completed?
114
+ @state == :completed
115
+ end
116
+
117
+ def partially_refunded?
118
+ @state == :partially_refunded
119
+ end
120
+
121
+ def pending?
122
+ @state == :pending
123
+ end
124
+
125
+ def refunded?
126
+ @state == :refunded
127
+ end
128
+
129
+ def denied?
130
+ @state == :denied
131
+ end
132
+
133
+ class << self
134
+ def find(id)
135
+ paypal_sale = find_raw(id)
136
+
137
+ if paypal_sale
138
+ new(paypal_sale, payment: nil, expanded: true)
139
+ else
140
+ nil
141
+ end
142
+ end
143
+
144
+ def find_raw(id)
145
+ begin
146
+ ::PayPal::SDK::REST::Sale.find(id)
147
+
148
+ rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
149
+ nil
150
+ end
151
+ end
152
+ end
153
+
154
+ def extract_and_store(paypal_sale)
155
+ @currency = convert_to_string(paypal_sale.amount.currency)
156
+ @total = convert_to_money(amount: paypal_sale.amount.total, currency: @currency)
157
+ @create_time = convert_to_time(paypal_sale.create_time)
158
+ @update_time = convert_to_time(paypal_sale.update_time)
159
+ @id = convert_to_string(paypal_sale.id)
160
+ @payment_id = convert_to_string(paypal_sale.parent_payment)
161
+ @state = convert_to_symbol(paypal_sale.state)
162
+ @raw_sale = paypal_sale
163
+
164
+ @transaction_fee = convert_to_money(amount: paypal_sale.transaction_fee.value, currency: paypal_sale.transaction_fee.currency)
165
+ @payment_mode = convert_to_string(paypal_sale.payment_mode)
166
+ @protection_eligibility = convert_to_string(paypal_sale.protection_eligibility)
167
+ @protection_eligibility_type = convert_to_string(paypal_sale.protection_eligibility_type)
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,18 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module REST
4
+ module Settings
5
+ def self.configure(live: false, client_id:, client_secret:)
6
+ mode = live ? "live" : "sandbox"
7
+
8
+ ::PayPal::SDK.configure(
9
+ :mode => mode,
10
+ :client_id => client_id,
11
+ :client_secret => client_secret,
12
+ :ssl_options => { }
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,118 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module REST
4
+ class Transaction
5
+ attr_reader :currency
6
+ attr_reader :total
7
+ attr_reader :payment
8
+ attr_reader :payment_id
9
+ attr_reader :description
10
+
11
+ attr_reader :subtotal
12
+ attr_reader :tax
13
+ attr_reader :fee
14
+ attr_reader :shipping
15
+ attr_reader :handling_fee
16
+ attr_reader :insurance
17
+ attr_reader :shipping_discount
18
+ attr_reader :insurance
19
+ attr_reader :gift_wrap
20
+
21
+ attr_reader :sales
22
+ attr_reader :authorizations
23
+ attr_reader :captures
24
+ attr_reader :refunds
25
+
26
+ attr_reader :raw_transaction
27
+
28
+ include ::PaymentRecipes::Utils::Converters
29
+
30
+ def initialize(paypal_transaction, payment:, expand: false)
31
+ unless paypal_transaction.is_a?(::PayPal::SDK::REST::DataTypes::Transaction)
32
+ raise Exception, "#{ self.class.name } must be initialized with a PayPal Transaction"
33
+ end
34
+
35
+ unless payment.is_a?(::PaymentRecipes::PayPal::REST::Payment)
36
+ raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::REST::Payment"
37
+ end
38
+
39
+ @currency = convert_to_string(paypal_transaction.amount.currency)
40
+ @total = convert_to_money(amount: paypal_transaction.amount.total, currency: @currency)
41
+ @description = convert_to_string(paypal_transaction.description)
42
+ @payment = payment
43
+ @payment_id = payment.id
44
+
45
+ @subtotal = convert_to_money(amount: paypal_transaction.amount.details.subtotal, currency: @currency)
46
+ @tax = convert_to_money(amount: paypal_transaction.amount.details.tax, currency: @currency)
47
+ @fee = convert_to_money(amount: paypal_transaction.amount.details.fee, currency: @currency)
48
+ @shipping = convert_to_money(amount: paypal_transaction.amount.details.shipping, currency: @currency)
49
+ @handling_fee = convert_to_money(amount: paypal_transaction.amount.details.handling_fee, currency: @currency)
50
+ @insurance = convert_to_money(amount: paypal_transaction.amount.details.insurance, currency: @currency)
51
+ @shipping_discount = convert_to_money(amount: paypal_transaction.amount.details.shipping_discount, currency: @currency)
52
+ @insurance = convert_to_money(amount: paypal_transaction.amount.details.insurance, currency: @currency)
53
+ @gift_wrap = convert_to_money(amount: paypal_transaction.amount.details.gift_wrap, currency: @currency)
54
+ @raw_transaction = paypal_transaction
55
+
56
+ @sales = []
57
+ @authorizations = []
58
+ @captures = []
59
+ @refunds = []
60
+
61
+ paypal_transaction.related_resources.each do |paypal_related_resource|
62
+ if paypal_related_resource.sale.id
63
+ sale = ::PaymentRecipes::PayPal::REST::Sale.new(paypal_related_resource.sale, payment: payment)
64
+ sale.reload! if expand
65
+
66
+ @sales << sale
67
+ end
68
+
69
+ if paypal_related_resource.authorization.id
70
+ authorization = ::PaymentRecipes::PayPal::REST::Authorization.new(paypal_related_resource.authorization, payment: payment)
71
+ authorization.reload! if expand
72
+
73
+ @authorizations << authorization
74
+ end
75
+
76
+ if paypal_related_resource.capture.id
77
+ capture = ::PaymentRecipes::PayPal::REST::Capture.new(paypal_related_resource.capture, payment: payment)
78
+ @captures << capture
79
+ end
80
+
81
+ if paypal_related_resource.refund.id
82
+ refund = ::PaymentRecipes::PayPal::REST::Refund.new(paypal_related_resource.refund, payment: payment)
83
+ @refunds << refund
84
+ end
85
+ end
86
+ end
87
+
88
+ def sale
89
+ @sales.first
90
+ end
91
+
92
+ def authorization
93
+ @authorizations.first
94
+ end
95
+
96
+ def capture
97
+ @captures.first
98
+ end
99
+
100
+ def refund
101
+ @refunds.first
102
+ end
103
+
104
+ def inspect
105
+ to_str
106
+ end
107
+
108
+ def to_s
109
+ to_str
110
+ end
111
+
112
+ def to_str
113
+ "<#{ self.class.name } total=#{ @total.format } sales=#{ @sales.size } authorizations=#{ @authorizations.size } captures=#{ @captures.size }>"
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,104 @@
1
+ """
2
+ PayPal::SDK::Merchant::DataTypes::DoCaptureResponseType
3
+ ├── Ack: Success
4
+ ├── Build: ***
5
+ ├── CorrelationID: ***
6
+ ├── DoCaptureResponseDetails: PayPal::SDK::Merchant::DataTypes::DoCaptureResponseDetailsType
7
+ │ ├── AuthorizationID: ***
8
+ │ └── PaymentInfo: PayPal::SDK::Merchant::DataTypes::PaymentInfoType
9
+ │ ├── ExchangeRate: nil
10
+ │ ├── FeeAmount: PayPal::SDK::Merchant::DataTypes::BasicAmountType
11
+ │ │ ├── currencyID: USD
12
+ │ │ └── value: 0.33
13
+ │ │
14
+ │ ├── GrossAmount: PayPal::SDK::Merchant::DataTypes::BasicAmountType
15
+ │ │ ├── currencyID: USD
16
+ │ │ └── value: 1.00
17
+ │ │
18
+ │ ├── ParentTransactionID: ***
19
+ │ ├── PaymentDate: 2017-02-05T18:15:36+00:00
20
+ │ ├── PaymentStatus: Completed
21
+ │ ├── PaymentType: instant
22
+ │ ├── PendingReason: none
23
+ │ ├── ProtectionEligibility: Ineligible
24
+ │ ├── ProtectionEligibilityType: None
25
+ │ ├── ReasonCode: none
26
+ │ ├── ReceiptID: 5351-7638-9795-5628
27
+ │ ├── TaxAmount: PayPal::SDK::Merchant::DataTypes::BasicAmountType
28
+ │ │ ├── currencyID: USD
29
+ │ │ └── value: 0.00
30
+ │ │
31
+ │ ├── TransactionID: ***
32
+ │ └── TransactionType: web-accept
33
+
34
+ ├── Timestamp: 2017-02-05T18:15:37+00:00
35
+ └── Version: 106.0
36
+ """
37
+
38
+ module PaymentRecipes
39
+ module PayPal
40
+ module SOAP
41
+ module Action
42
+ class CaptureAuthorization < PaymentRecipes::Utils::Action
43
+ variable :authorization_id, "String"
44
+ variable :amount, "Hash"
45
+
46
+ def perform
47
+ prepare_soap_api
48
+
49
+ do_capture_authorization
50
+
51
+ if response.success?
52
+ load_transaction
53
+ end
54
+
55
+ response
56
+ end
57
+
58
+ def prepare_soap_api
59
+ store(:api) do
60
+ ::PaymentRecipes::PayPal::SOAP::Settings.api
61
+ end
62
+ end
63
+
64
+ def response
65
+ do_capture_response
66
+ end
67
+
68
+ def do_capture_authorization
69
+ do_capture = api.build_do_capture({
70
+ :AuthorizationID => authorization_id,
71
+ :Amount => amount,
72
+ :CompleteType => "Complete" })
73
+
74
+ store(:do_capture_response) do
75
+ capture_response = api.do_capture(do_capture)
76
+
77
+ unless capture_response.success?
78
+ @error = capture_response.errors
79
+
80
+ capture_response = nil
81
+ end
82
+
83
+ capture_response
84
+ end
85
+ end
86
+
87
+ def load_transaction
88
+ store(:authorization_transaction) do
89
+ ::PaymentRecipes::PayPal::SOAP::Transaction.find(authorization_id)
90
+ end
91
+
92
+ store(:capture_transaction) do
93
+ ::PaymentRecipes::PayPal::SOAP::Transaction.find(capture_id)
94
+ end
95
+ end
96
+
97
+ def capture_id
98
+ response.do_capture_response_details.payment_info.transaction_id
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,76 @@
1
+ module PaymentRecipes
2
+ module PayPal
3
+ module SOAP
4
+ module Action
5
+ class CreateExpressCheckout < PaymentRecipes::Utils::Action
6
+ variable :details, "Hash"
7
+ variable :intent, "Symbol"
8
+
9
+ def perform
10
+ prepare_soap_api
11
+ modify_details
12
+ set_express_checkout
13
+
14
+ get_express_checkout_url
15
+ end
16
+
17
+ def prepare_soap_api
18
+ store(:api) do
19
+ ::PaymentRecipes::PayPal::SOAP::Settings.api
20
+ end
21
+ end
22
+
23
+ def modify_details
24
+ unless [:sale, :authorize].include?(intent)
25
+ raise Exception, "Allowed values for intent: :sale, :authorize"
26
+ end
27
+
28
+ store(:express_checkout_details) do
29
+ payment_action = if intent == :sale
30
+ 'Sale'
31
+ elsif intent == :authorize
32
+ 'Authorization'
33
+ end
34
+
35
+ details[:SetExpressCheckoutRequestDetails][:PaymentDetails][0][:PaymentAction] = payment_action
36
+
37
+ details
38
+ end
39
+ end
40
+
41
+ def set_express_checkout
42
+ set_express_checkout = api.build_set_express_checkout(express_checkout_details)
43
+
44
+ store(:set_express_checkout_response) do
45
+ response = api.set_express_checkout(set_express_checkout)
46
+
47
+ unless response.success?
48
+ @error = response.errors
49
+
50
+ response = nil
51
+ end
52
+
53
+ response
54
+ end
55
+ end
56
+
57
+ def response
58
+ set_express_checkout_response
59
+ end
60
+
61
+ def get_express_checkout_url
62
+ store(:express_checkout_redirect_url) do
63
+ if set_express_checkout_response
64
+ api.express_checkout_url(set_express_checkout_response)
65
+ end
66
+ end
67
+ end
68
+
69
+ def redirect_url
70
+ express_checkout_redirect_url
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end