Skip to content

unitpay/ruby-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

#Unitpay ##Установка Внесите эту строчку в ваш gem файл

gem 'unitpay-api'

И затем выполните команду

bundle

Или установите вручную с помощью

gem install unitpay_api

Теперь включите в проект с помощью

require 'unitpay_api'

##Примеры использования API для фреймворка Rails ###Пример использования API для отображения формы:

class UnitpayController < ApplicationController
  def payment
    domain = 'unitpay.money' # Your working domain: unitpay.money or unitpay.ru
  	secretKey = 'asdfsd3243adsfa32fsa2345r3e3w3rf'
  	publicKey = '9978-2bfc2'
  	summ = '10.00'
  	account = 'asdf-1234-fds1'
  	desc = 'Платеж по заказу'
  	
    unitpay = UnitPay.new(domain, secretKey)
    url = unitpay.form(publicKey, summ, account, desc)
    redirect_to url
  end
end

###Пример использования API для написания запросов к сервисам unitpay.ru и unitpay.money

class UnitpayController < ApplicationController
  def payment

    domain = 'unitpay.money' # Your working domain: unitpay.money or unitpay.ru
    secretKey = 'asdfsd3243adsfa32fsa2345r3e3w3rf'
    publicKey = '9978-2bfc2'
    summ = '10.00'
    account = 'asdf-1234-fds1'
    desc = 'Платеж по заказу'
    currency = 'RUB'
    projectId = '9978'

    unitpay = UnitPay.new(domain, secretKey)
    response = unitpay.api('initPayment', {
        'account' => account,
        'desc' => desc,
        'sum' => summ,
        'paymentType' => 'yandex',
        'currency' => currency,
        'projectId' => projectId,
    });

    #If need user redirect on Payment Gate
    if !!response['result'] && !!response['result']['type'] && response['result']['type'] == 'redirect'
      # Url on PaymentGate
      url = response['result']['redirectUrl']
      # Payment ID in Unitpay (you can save it)
      paymentId = response['result']['paymentId']
      # User redirect
      redirect_to url
    elsif !!response['result'] && !!response['result']['type'] && response['result']['type'] == 'invoice'
      # Url on receipt page in Unitpay
        url = response['result']['receiptUrl']
        # Payment ID in Unitpay (you can save it)
        paymentId = response['result']['paymentId']
        # Invoice Id in Payment Gate (you can save it)
        invoiceId = response['result']['invoiceId']
        # User redirect
        redirect_to url
      elsif !!response['error'] && !!response['error']['message']
        # text error message
        error = response['error']['message']
    end
  end
end

###пример callback'а

class UnitpayController < ApplicationController
  def callback
    domain = 'unitpay.money' # Your working domain: unitpay.money or unitpay.ru
    secretKey = 'asdfsd3243adsfa32fsa2345r3e3w3rf'
    summ = '10.00'
    account = 'asdf-1234-fds1'
    currency = 'RUB'
    projectId = '9978'

    begin
        unitpay = UnitPay.new(domain, secretKey)
        method = params[:method]
        if params[:params].nil?
          p = {}
        else
          p = params[:params]
        end
        ip = request.remote_ip

        unitpay.checkHandlerRequest(method, params, ip)
        if p['orderSum'] != summ ||
          p['orderCurrency'] != currency ||
          p['account'] != account ||
          p['projectId'] != projectId

          raise 'Order validation error'
        end

        if ( method == 'check' )
          json = unitpay.getSuccessHandlerResponse('Check Success. Ready to pay.')
        elsif( method == 'pay' )
          json = unitpay.getSuccessHandlerResponse('Pay Success')
        elsif( method == 'error' )
          json = unitpay.getSuccessHandlerResponse('Error logged')
        elsif( method == 'refund' )
          json = unitpay.getSuccessHandlerResponse('Order canceled')
        end

        render :json => json
    rescue Exception => e  
        error = e.message
        render :json => unitpay.getErrorHandlerResponse(error)
    end 
  end
end

About

ruby sdk for unitpay payment

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages