Giter Club home page Giter Club logo

promisepay-ruby's Introduction

Ruby SDK - PromisePay API

Join the chat at https://gitter.im/PromisePay/promisepay-ruby

Gem Version Build Status Coverage Status Code Climate

To see a completed integration in a Ruby on Rails app, visit this repository.

1. Installation

Add these lines to your application's Gemfile:

gem 'promisepay'

And then execute:

$ bundle

Or install it yourself as:

$ gem install promisepay

2. Configuration

Before interacting with Promispay API you need to generate an access token.

See PromisePay documentation for more information.

Create a PromisePay client

The client can be configured through environment variables.

# In your environment file
PROMISEPAY_USERNAME ||= youremailaddress
PROMISEPAY_TOKEN ||= y0urt0k3n12345678910123456789101

The following parameters are configurable through the client:

  • :username / ENV['PROMISEPAY_USERNAME']: username for basic authentication
  • :token / ENV['PROMISEPAY_TOKEN']: token for basic authentication
  • :environment / ENV['PROMISEPAY_ENVIRONMENT']: API environment to use (default: 'test')
  • :api_domain / ENV['PROMISEPAY_API_DOMAIN']: API domain name to use (default: 'api.promisepay.com')

Instantiate the PromisePay client.

client = Promisepay::Client.new(username: ENV['PROMISEPAY_USERNAME'], token: ENV['PROMISEPAY_TOKEN'])

3. Examples

Tokens

Example 1 - Request session token

The below example shows the controller request for a marketplace configured to have the Item and User IDs generated automatically for them. Note: by default, the ability to have PromisePay auto generate IDs is turned off. However, it can easily be requested by contacting PromisePay support.

token_request = client.tokens.create(:session, {
  current_user: 'seller',
  item_name: 'Test Item',
  amount: '2500',
  seller_lastname: 'Seller',
  seller_firstname: 'Sally',
  buyer_lastname: 'Buyer',
  buyer_firstname: 'Bobby',
  buyer_country: 'AUS',
  seller_country: 'USA',
  seller_email: '[email protected]',
  buyer_email: '[email protected]',
  fee_ids: [],
  payment_type_id: 2
})
token = token_request['token']
item_id = token_request['item']
buyer_id = token_request['buyer']
seller_id = token_request['seller']
Example 2 - Request session token

The below example shows the request for a marketplace that passes the Item and User IDs.

token = client.tokens.create(:session, {
  current_user_id: 'seller1234',
  item_name: 'Test Item',
  amount: '2500',
  seller_lastname: 'Seller',
  seller_firstname: 'Sally',
  buyer_lastname: 'Buyer',
  buyer_firstname: 'Bobby',
  buyer_country: 'AUS',
  seller_country: 'USA',
  seller_email: '[email protected]',
  buyer_email: '[email protected]',
  external_item_id: 'TestItemId1234',
  external_seller_id: 'seller1234',
  external_buyer_id: 'buyer1234',
  fee_ids: [],
  payment_type_id: 2
})['token']

Items

Create an item
item = client.items.create(
  id: '12345',
  name: 'test item for 5AUD',
  amount: '500',
  payment_type: '1',
  buyer_id: buyer.id,
  seller_id: seller.id,
  fee_ids: fee.id,
  description: '5AUD transfer'
)
Get an item
item = client.items.find('1')
Get a list of items
items = client.items.find_all
Update an item
item.update(name: 'new name')
Cancel an item
item.cancel
Get an item status
item.status
Get an item's buyer
item.buyer
Get an item's seller
item.seller
Get an item's fees
item.fees
Get an item's transactions
item.transactions
Get an item's batch transactions
item.batch_transactions
Get an item's wire details
item.wire_details
Get an item's BPAY details
item.bpay_details

Users

Create a user
user = client.users.create(
  id: '123456',
  first_name: 'test',
  last_name: 'buyer',
  email: '[email protected]',
  address_line1: '48 collingwood',
  state: 'vic',
  city: 'Mel',
  zip: '3000',
  country: 'AUS',
  dob:'12/06/1980'
)
Update a user
user = client.users.update(
  id: '123456',
  first_name: 'test',
  last_name: 'buyer',
  email: '[email protected]',
  address_line1: '48 collingwood',
  state: 'vic',
  city: 'Mel',
  zip: '3000',
  country: 'AUS',
  dob:'12/06/1980'
)
Get a user
user = client.users.find('1')
Get a list of users
users = client.users.find_all
Get a user's card account
user.card_account
Get a user's PayPal account
user.paypal_account
Get a user's bank account
user.bank_account
Get a user's wallet account
user.wallet_account
Get a user's items
user.items
Get a user's address
user.address
Set a user's disbursement account
user.disbursement_account(bank_account.id)

Item Actions

Make payment
item.make_payment(
  account_id: buyer_card_account.id
)
Request payment
item.request_payment
Release payment
item.release_payment
Request release
item.request_release
Cancel
item.cancel
Acknowledge wire
item.acknowledge_wire
Acknowledge PayPal
item.acknowledge_paypal
Revert wire
item.revert_wire
Request refund
item.request_refund(
  refund_amount: '1000',
  refund_message: 'because'
)
Decline refund
item.decline_refund
Refund
item.refund(
  refund_amount: '1000',
  refund_message: 'because'
)
Raise dispute
item.raise_dispute(user_id: '5830def0-ffe8-11e5-86aa-5e5517507c66')
Request resolve dispute
item.request_resolve_dispute
Resolve dispute
item.resolve_dispute
Escalate dispute
item.escalate_dispute
Send tax invoice
item.send_tax_invoice
Request tax invoice
item.request_tax_invoice

Card Accounts

Create a card account
card_account = client.card_accounts.create(
  user_id: buyer.id,
  full_name: 'test Buyer',
  number: '4111 1111 1111 1111',
  expiry_month: Time.now.month,
  expiry_year: Time.now.year + 1,
  cvv: '123'
)
Get a card account
card_account = client.card_accounts.find('1')
Deactivate a card account
card_account.deactivate
Get a card account's users
card_account.user

Bank Accounts

Create a bank account
bank_account = client.bank_accounts.create(
  user_id: seller.id,
  bank_name: 'Nab',
  account_name: 'test seller',
  routing_number: '22222222',
  account_number: '1234567890',
  account_type: 'savings',
  holder_type: 'personal',
  country: 'AUS'
)
Get a bank account
bank_account = client.bank_accounts.find('1')
Deactivate a bank account
bank_account.deactivate
Get a bank account's users
bank_account.user
Validate Routing Number
client.bank_accounts.validate('122235821')

PayPal Accounts

Create a PayPal account
paypal_account = client.paypal_accounts.create(
  user_id: seller.id,
  paypal_email: '[email protected]'
)
Get a PayPal account
paypal_account = client.paypal_accounts.find('1')
Deactivate a PayPal account
paypal_account.deactivate
Get a PayPal account's users
paypal_account.user

Wallet Accounts

Get a Wallet account
wallet_account = client.wallet_accounts.find('1')
Deposit funds
wallet_account.deposit(
  account_id: '123',
  amount: 500
)
Withdraw funds
wallet_account.withdraw(
  account_id: '123',
  amount: 200
)
Get a Wallet account's users
wallet_account.user

Companies

Create a company
client.companies.create(
  user_id: "1",
  name: "Acme Co",
  legal_name: "Acme Co Pty Ltd",
  tax_number: "1231231",
  charge_tax: true,
  address_line1: "123 Test St",
  address_line2: "",
  city: "Melbourne",
  state: "VIC",
  zip: "3000",
  country: "AUS"
)
Get a company
client.companies.find('compamy_id')
Get a list of companies
client.companies.find_all
Get a company's address
company.address
Update a company
client.companies.update(
  id: "8d578b9c-5b79-11e5-885d-feff819cdc9f",
  name: "Acme Co",
  legal_name: "Acme Co Pty Ltd",
  tax_number: "1231231",
  charge_tax: true,
  address_line1: "123 Test St",
  address_line2: "",
  city: "Melbourne",
  state: "VIC",
  zip: "3000",
  country: "AUS"
)

Fees

Get a list of fees
fees = client.fees.find_all
Get a fee
fees = client.fees.find('1')
Create a fee
fee = client.fees.create(
  name: 'test fee for 5 AUD',
  fee_type_id: '1',
  amount: '75',
  to: 'seller'
)

Transactions

Get a list of transactions
transactions = client.transactions.find_all
Get a transaction
transaction = client.transactions.find('1')
Get a transaction's users
transaction.users
Get a transaction's fees
transaction.fees

Batch Transactions

Get a list of batch transactions
batch_transactions = client.batch_transactions.find_all
Get a transaction
batch_transaction = client.batch_transactions.find('1')

Charges

Get a list of charges
charges = client.charges.find_all
Get a charge
charge = client.charges.find('1')
Create a charge
charge = client.charges.create(
  account_id: '123',
  user_id: '456',
  name: 'Charge for Delivery',
  email: '[email protected]',
  amount: 4_500,
  zip: '3000',
  curency: 'AUD',
  country: 'AUS',
  retain_account: true,
  device_id: '0900JapG4txqVP4Nf...',
  ip_address: '172.16.81.100'
)
Get a charge's buyer
charge.buyer
Get a charge's status
charge.status

Direct Debit Authority

Get a list of direct debit authorities for a given account
bank_account = client.bank_accounts.find('9fda18e7-b1d3-4a83-830d-0cef0f62cd25')
ddas = client.charges.find_all(bank_account.id)
Get a direct debit authority
dda = client.direct_debit_authorities.find('8f233e04-ffaa-4c9d-adf9-244853848e21')
Create a direct debit authority
charge = client.direct_debit_authorities.create(
  account_id: '9fda18e7-b1d3-4a83-830d-0cef0f62cd25',
  amount: '10000'
)
Delete a direct debit authority
dda = client.direct_debit_authorities.find('8f233e04-ffaa-4c9d-adf9-244853848e21')
dda.delete

Tools

Health check
client.tools.health_check

Marketplace

client.marketplace

Token

Generate
client.generate_token(token_type: 'card', user_id: '5830def0-ffe8-11e5-86aa-5e5517507c66')

Check out the online documentation to get a full list of available resources and methods.

4. Contributing

  1. Fork it ( https://github.com/PromisePay/promisepay-ruby/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

promisepay-ruby's People

Contributors

ap-skumar avatar dannyshafer avatar gitter-badger avatar mike-asm avatar romsssss avatar terenceponce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

promisepay-ruby's Issues

JSON::ParserError when running any command

I've been using promisepay for a few months and suddenly a week or so ago, I ran into a problem which occurs after running any command. For example, after running these two commands in the rails console,

client = Promisepay::Client.new(username: ENV['PROMISEPAY_USERNAME'], token: ENV['PROMISEPAY_TOKEN'])

user = client.users.create(
  id: '123456',
  first_name: 'test',
  last_name: 'buyer',
  email: '[email protected]',
  address_line1: '48 collingwood',
  state: 'vic',
  city: 'Mel',
  zip: '3000',
  country: 'AUS',
  dob:'12/06/1980'
)

gives this error and stack trace

JSON::ParserError: A JSON text must at least contain two octets!
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `initialize'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `new'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:43:in `build_error_message'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:31:in `initialize'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:25:in `new'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:25:in `from_response'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/client.rb:180:in `on_complete'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/client.rb:82:in `post'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/resources/user_resource.rb:43:in `create'
	from (irb):50
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'

I get a similar error when running items = client.items.find_all. This is the error and stack trace:

JSON::ParserError: A JSON text must at least contain two octets!
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `initialize'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `new'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:43:in `build_error_message'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:31:in `initialize'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:25:in `new'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/error.rb:25:in `from_response'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/client.rb:180:in `on_complete'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/client.rb:67:in `get'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/promisepay-1.1.0/lib/promisepay/resources/item_resource.rb:18:in `find_all'
	from (irb):73
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
	from /Users/colinsoleim/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
	from bin/rails:4:in `require'

Any and all help would be appreciated. Thank you!

Resource refactor

As the promisepay API resources are standard REST, almost all the classes *Resource do pretty much the same. There is no need of that such amount of code repetition that degrade the code quality and increase complexity.
What i'm talking is doing something like:

module Promisepay
  class RestResource < BaseResource
        def find_all(options = {})
          response = JSON.parse(@client.get(resource_key_name, options).body)
          resources = response.key?(resource_key_name) ? response[resource_key_name] : []
          resources.map { |attributes| model.new(@client, attributes) }
         end
         # So on for all the others rest actions.
  end
end

module Promisepay
  # Resource for the Users API
  class UserResource < RestResource
    def model
      Promisepay::User
    end
    def resource_key_name
      "users"
    end
    # any extra method needed.
  end
end

Any plan to implement Callbacks

Just that ... :) the CRUD for Callback resources should be almost trivial to implement as you already got the main engine.

Thanks

bug in Redact Bank Account

Seems that "Redact Bank Account" operation is not correct. In the code here it is require a mobile-pin param which is not mentioned on the api doc nor in the docs.

In the best case scenario, the mobile-pin should be optional.

Suddenly No fees are being charged

As this library has not been updated since August 2017 - is it still being maintained.

We have been using it, but since the middle of last month we started to see seller fees of zero, and now we are seeing all fees being set to zero (including the Assembly fee).

The fee objects have been created and are set to the correct amount.

A big issue is that on the pre-live environment everything is working as it should. When we run the same code on live environment, the problem occurs.

We can run the same transactions through postman and everything is fine - but this is not testing this library.

Promisepay::InternalServerError when creating items

Hello, when i'm getting Promisepay::InternalServerError when trying to create an item.

sidekiq_1      | 2017-01-07T04:57:32.480Z 10 TID-mvmnw WARN: Promisepay::InternalServerError: 
sidekiq_1      | 2017-01-07T04:57:32.480Z 10 TID-mvmnw WARN: /myapp/.bundle/gems/promisepay-1.2.0/lib/promisepay/client.rb:183:in `on_complete'
sidekiq_1      | /myapp/.bundle/gems/promisepay-1.2.0/lib/promisepay/client.rb:84:in `post'
sidekiq_1      | /myapp/.bundle/gems/promisepay-1.2.0/lib/promisepay/resources/item_resource.rb:49:in `create'

Any idea what is going on?

Error#build_error_message returns hard-to-parse string.

The errors the gem returns aren't easily parsable.
E.g.: "name: [\"can't be blank\", \"required field missing\"]"

A hash would be much more valuable IMHO. It's easier to iterate over, and it's a common structure in Rails.
This would be a matter of removing the following bit of code in the Error#build_error_message method:

    def build_error_message
      return nil if @response.nil? || @response.body.nil?

      json_response = JSON.parse(@response.body)
# Remove from here...
      message = ''
      message << json_response['message'] if json_response.key?('message')
      if json_response.key?('errors')
        json_response['errors'].each do |attribute, content|
          message << "#{attribute}: #{content}"
        end
      end
      message
# ...to here.
    end
  end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.