<commit_before>= Payson API

A simple utility to handle requests against the Payson payment gateway API.


== Supported versions

* Ruby 1.8.7, 1.9.2, 1.9.3


== Install

Put this line in your Gemfile:
  gem 'payson_api'

Then bundle:
  % bundle


== Usage

=== General configuration options

You can configure the following default values by overriding these values using the <tt>PaysonAPI.configure</tt> method:

  PaysonAPI.configure do |config|
    config.api_user_id = 'XXXX'
    config.api_password = '<PASSWORD>'
  end

=== Initiating a payment

  return_url = 'http://localhost/payson/success'
  cancel_url = 'http://localhost/payson/cancel'
  ipn_url = 'http://localhost/payson/ipn'
  memo = 'Sample order description'

  receivers = []
  receivers << PaysonAPI::Receiver.new(
    email = '<EMAIL>',
    amount = 100
  )

  sender = PaysonAPI::Sender.new(
    email = '<EMAIL>',
    first_name = 'My',
    last_name = 'Customer'
  )

  order_items = []
  order_items << PaysonAPI::OrderItem.new(
    description = 'Order item description',
    unit_price = 100,
    quantity = 1,
    tax = 0,
    sku = 'MY-ITEM-1'
  )

  payment = PaysonAPI::Request::Payment.new(
    return_url,
    cancel_url,
    ipn_url,
    memo,
    sender,
    receivers
  )
  payment.order_items = order_items

  response = PaysonAPI::Client.initiate_payment(payment)

  if response.success?
    # Redirect to response.forward_url
  else
    puts response.errors
  end


=== Requesting payment details

  token = 'token-<PASSWORD>-payment-<PASSWORD>'

  payment_details = PaysonAPI::Request::PaymentDetails.new(token)

  response = PaysonAPI::Client.get_payment_details(payment_details)

  if response.success?
    # Do stuff with response object
  else
    puts response.errors
  end


=== Updating a payment

  token = 'token-received-from-payment-request'
  action = 'CANCELORDER'

  payment_update = PaysonAPI::PaymentUpdate.new(token, action)

  response = PaysonAPI::Client.update_payment(payment_update)

  if response.success?
    # Do stuff with response object
  else
    puts response.errors
  end


== Todo

Implement the following requests:

* Validate - Validate an IPN (Instant Payment Notification).



== Build Status {<img src="https://secure.travis-ci.org/stoffus/payson_api.png"/>}[http://travis-ci.org/stoffus/payson_api]


== Questions, Feedback

Feel free to message me on Github (stoffus).


== Copyright

Copyright (c) 2012 <NAME>.<commit_msg>Docs for IPN<commit_after>= Payson API

A simple utility to handle requests against the Payson payment gateway API.


== Supported versions

* Ruby 1.8.7, 1.9.2, 1.9.3


== Install

Put this line in your Gemfile:
  gem 'payson_api'

Then bundle:
  % bundle


== Usage

=== General configuration options

You can configure the following default values by overriding these values using the <tt>PaysonAPI.configure</tt> method:

  PaysonAPI.configure do |config|
    config.api_user_id = 'XXXX'
    config.api_password = '<PASSWORD>'
  end

=== Initiating a payment

  return_url = 'http://localhost/payson/success'
  cancel_url = 'http://localhost/payson/cancel'
  ipn_url = 'http://localhost/payson/ipn'
  memo = 'Sample order description'

  receivers = []
  receivers << PaysonAPI::Receiver.new(
    email = '<EMAIL>',
    amount = 100
  )

  sender = PaysonAPI::Sender.new(
    email = '<EMAIL>',
    first_name = 'My',
    last_name = 'Customer'
  )

  order_items = []
  order_items << PaysonAPI::OrderItem.new(
    description = 'Order item description',
    unit_price = 100,
    quantity = 1,
    tax = 0,
    sku = 'MY-ITEM-1'
  )

  payment = PaysonAPI::Request::Payment.new(
    return_url,
    cancel_url,
    ipn_url,
    memo,
    sender,
    receivers
  )
  payment.order_items = order_items

  response = PaysonAPI::Client.initiate_payment(payment)

  if response.success?
    # Redirect to response.forward_url
  else
    puts response.errors
  end


=== Requesting payment details

  token = 'token-<PASSWORD>'

  payment_details = PaysonAPI::Request::PaymentDetails.new(token)

  response = PaysonAPI::Client.get_payment_details(payment_details)

  if response.success?
    # Do stuff with response object
  else
    puts response.errors
  end


=== Updating a payment

  token = 'token-<PASSWORD>'
  action = 'CANCELORDER'

  payment_update = PaysonAPI::Request::PaymentUpdate.new(token, action)

  response = PaysonAPI::Client.update_payment(payment_update)

  if response.success?
    # Do stuff with response object
  else
    puts response.errors
  end


=== Validating an IPN response

This example assumes the use of the Rails web framework.

  class Payson < ApplicationController

    def ipn_responder
      request_body = request.body.read
      ipn_response = PaysonAPI::Response::IPN.new(request_body)

      # Do business stuff, e.g. update the corresponding order:
      #   order = Order.find_by_payson_token(ipn.token)
      #   order.payson_status = ipn.status
      #   order.save!

      # Create a new IPN request object containing the raw response from above
      ipn_request = PaysonAPI::Request::IPN.new(ipn_response.raw)

      validate = PaysonAPI::Client.validate_ipn(ipn_request)

      unless validate.verified?
        raise "Something went terribly wrong."
      end
    end
  end


== Todo

Implement the following requests:

* Validate - Validate an IPN (Instant Payment Notification).



== Build Status {<img src="https://secure.travis-ci.org/stoffus/payson_api.png"/>}[http://travis-ci.org/stoffus/payson_api]


== Questions, Feedback

Feel free to message me on Github (stoffus).


== Copyright

Copyright (c) 2012 <NAME>.
{'fraction_non_alphanumeric': 0.10690818682294441, 'fraction_numerical': 0.00781388740898597, 'mean_word_length': 3.46984126984127, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '702706', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1801, 'n_words': 459}