Class: Gateway::Payu
- Inherits:
-
PaymentMethod
- Object
- PaymentMethod
- Gateway::Payu
- Defined in:
- app/models/spree/gateway/payu.rb
Instance Method Summary collapse
- #amount(amount) ⇒ Object
- #authorize ⇒ Object
- #authorize_url ⇒ Object
- #cancel(order_id) ⇒ Object
- #credit(credit_cents, payment_id, options) ⇒ Object
- #items_payload(line_items) ⇒ Object
- #language(country_iso) ⇒ Object
- #order_url ⇒ Object
- #payment_profiles_supported? ⇒ Boolean
- #register_order(order, payment_id, gateway_id) ⇒ Object
- #register_order_payload(order, payment, gateway_id) ⇒ Object
- #source_required? ⇒ Boolean
- #verify_transaction(status, payment, amount, currency, payu_order_id) ⇒ Object
- #verify_url(payu_order_id) ⇒ Object
Instance Method Details
#amount(amount) ⇒ Object
29 30 31 |
# File 'app/models/spree/gateway/payu.rb', line 29 def amount(amount) (BigDecimal(amount.to_s) * BigDecimal('100')).to_i.to_s end |
#authorize ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/spree/gateway/payu.rb', line 121 def # https://developers.payu.com/europe/api#tag/Authorize/operation/oauth conn = Faraday.new(url: ) do |faraday| faraday.adapter Faraday.default_adapter end = { grant_type: 'client_credentials', client_id: preferred_payu_client_id&.to_i, client_secret: preferred_payu_client_secret } response = conn.post do |req| req.headers['Content-Type'] = 'application/x-www-form-urlencoded' req.body = URI.encode_www_form() end if response.success? response_body = JSON.parse(response.body) response_body['access_token'] else Rails.logger.debug("Token: #{response.inspect}") end end |
#authorize_url ⇒ Object
113 114 115 116 117 118 119 |
# File 'app/models/spree/gateway/payu.rb', line 113 def if preferred_test_mode 'https://secure.snd.payu.com/pl/standard/user/oauth/authorize' else 'https://secure.payu.com/pl/standard/user/oauth/authorize' end end |
#cancel(order_id) ⇒ Object
22 23 24 25 26 27 |
# File 'app/models/spree/gateway/payu.rb', line 22 def cancel(order_id) Rails.logger.debug("Starting cancellation for #{order_id}") Rails.logger.debug("Spree order #{order_id} has been canceled.") ActiveMerchant::Billing::Response.new(true, 'Spree order has been canceled.') end |
#credit(credit_cents, payment_id, options) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'app/models/spree/gateway/payu.rb', line 33 def credit(credit_cents, payment_id, ) order = [:originator].try(:payment).try(:order) payment = [:originator].try(:payment) reimbursement = [:originator].try(:reimbursement) order_number = order.try(:number) order_currency = order.try(:currency) ActiveMerchant::Billing::Response.new(true, 'Refund successful') end |
#items_payload(line_items) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'app/models/spree/gateway/payu.rb', line 103 def items_payload(line_items) line_items.map do |item| { quantity: item.quantity, unitPrice: amount(item.price), name: item.name } end end |
#language(country_iso) ⇒ Object
194 195 196 197 198 199 |
# File 'app/models/spree/gateway/payu.rb', line 194 def language(country_iso) country_iso = country_iso&.downcase country_iso = 'cs' if country_iso == 'cz' country_iso || 'en' end |
#order_url ⇒ Object
43 44 45 46 47 48 49 |
# File 'app/models/spree/gateway/payu.rb', line 43 def order_url if preferred_test_mode 'https://secure.snd.payu.com/api/v2_1/orders' else 'https://secure.payu.com/api/v2_1/orders' end end |
#payment_profiles_supported? ⇒ Boolean
14 15 16 |
# File 'app/models/spree/gateway/payu.rb', line 14 def payment_profiles_supported? false end |
#register_order(order, payment_id, gateway_id) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/spree/gateway/payu.rb', line 51 def register_order(order, payment_id, gateway_id) return if order.blank? || payment_id.blank? || gateway_id.blank? payment = order.payments.find payment_id conn = Faraday.new(url: order_url) do |faraday| faraday.adapter Faraday.default_adapter faraday.request :authorization, 'Bearer', end response = conn.post do |req| req.headers['Content-Type'] = 'application/json' req.body = register_order_payload(order, payment, gateway_id).to_json end if (200..303).include? response.status response_body = JSON.parse(response.body) payment.update(public_metadata: { token: response_body['orderId'], payment_url: (response_body['redirectUri']+ "&lang=#{order.billing_address.country.iso.downcase}") }) else Rails.logger.warn("register_order #{order.id}, payment_id: #{payment_id} failed => #{response.inspect}") nil end end |
#register_order_payload(order, payment, gateway_id) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/spree/gateway/payu.rb', line 75 def register_order_payload(order, payment, gateway_id) ip_address = order.last_ip_address || Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address { customerIp: ip_address, merchantPosId: preferred_payu_pos_id, description: order.store.name, currencyCode: order.currency, totalAmount: amount(order.total), products: items_payload(order.line_items), continueUrl: preferred_return_url, notifyUrl: "#{preferred_return_status_url}/gateway/payu/comeback/#{gateway_id}/#{order.id}", extOrderId: "#{order.number}|#{payment.number}", buyer: { email: order.email, phone: order.billing_address.phone, firstName: order.billing_address.firstname, lastName: order.billing_address.lastname, language: language(order.billing_address.country.iso), delivery: { street: order.shipping_address.address1, postalCode: order.shipping_address.zipcode, city: order.shipping_address.city, countryCode: order.shipping_address.country.iso } } } end |
#source_required? ⇒ Boolean
18 19 20 |
# File 'app/models/spree/gateway/payu.rb', line 18 def source_required? false end |
#verify_transaction(status, payment, amount, currency, payu_order_id) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/models/spree/gateway/payu.rb', line 154 def verify_transaction(status, payment, amount, currency, payu_order_id) return false if status.blank? || payment.blank? || amount.blank? || currency.blank? || payu_order_id.blank? return true if status == 'PENDING' return false if !['WAITING_FOR_CONFIRMATION', 'COMPLETED', 'CANCELED'].include?(status) float_amount = (amount.to_f / 100).to_f if status == 'WAITING_FOR_CONFIRMATION' conn = Faraday.new(url: verify_url(payu_order_id)) do |faraday| faraday.adapter Faraday.default_adapter faraday.request :authorization, 'Bearer', end response = conn.post do |req| req.headers['Content-Type'] = 'application/json' end if response.success? return true else Rails.logger.warn("Verify_transaction #{payment.order.id} failed => #{response.inspect}") return false end end if payment.can_complete? && status == 'COMPLETED' payment.amount = float_amount payment.complete elsif status == 'CANCELED' payment.cancel! end = payment. [:order_id] = payu_order_id [:currency] = currency [:amount] = amount payment.update(private_metadata: ) true end |
#verify_url(payu_order_id) ⇒ Object
146 147 148 149 150 151 152 |
# File 'app/models/spree/gateway/payu.rb', line 146 def verify_url(payu_order_id) if preferred_test_mode "https://secure.snd.payu.com/api/v2_1/orders/#{payu_order_id}/captures" else "https://secure.payu.com/api/v2_1/orders/#{payu_order_id}/captures" end end |