GetPaid Authorize.Net Payment Processor
=======================================

The AuthorizeNetAdapter is an implementation of a GetPaid payment processor
that can process payments via the Authorize.net APIs.

Test Setup
----------

In order to use the AuthorizeNetAdapter, we first need a context that can be
adapted to IAuthorizeNetOptions to get the keys for accessing the Authorize.net
API. (LOGIN and KEY are initialized in tests.py based on environment variables.
Use the login Authorize.net)

  >>> from zope.interface import implements
  >>> from getpaid.authorizedotnet.interfaces import IAuthorizeNetOptions
  >>> class DummyAuthContext(object):
  ...     implements(IAuthorizeNetOptions)
  ...     server_url = 'Test'
  ...     merchant_id = LOGIN
  ...     merchant_key = KEY

We also need an order that we want to process payments for.

  >>> import time
  >>> from getpaid.core import order, item, cart, options, interfaces, payment
  >>> my_cart = cart.ShoppingCart()
  >>> my_cart['abc'] = abc = item.LineItem()
  >>> abc.cost = 22.20; abc.name = 'abc'; abc.quantity = 3
  >>> order = order.Order()
  >>> order.setOrderId('test%s' % int(time.time()))
  >>> order.shopping_cart = my_cart
  >>> order.contact_information = contact = payment.ContactInformation()
  >>> contact.name = 'Harvey Frank'
  >>> contact.phone_number = '2062681235'
  >>> contact.email = 'harvey@example.com'
  >>> order.billing_address = billing = payment.BillingAddress()
  >>> billing.bill_first_line = '1402 3rd Ave.'
  >>> billing.bill_city = 'Seattle'
  >>> billing.bill_state = 'WA'
  >>> billing.bill_postal_code = '98101'

And a property bag with details about the payment.

  >>> from datetime import datetime, timedelta
  >>> BillingInfo = options.PropertyBag.makeclass(interfaces.IUserPaymentInformation)
  >>> payment = BillingInfo(
  ...     name_on_card = 'Harvey Frank',
  ...     bill_phone_number = '2062861235',
  ...     credit_card_type = 'Visa',
  ...     credit_card = '4007000000027',
  ...     cc_expiration = datetime.now() + timedelta(365),
  ...     cc_cvc = '111',
  ...     )

Authorizing an Order
--------------------

Authorization confirms that an order may be processed using the given billing
information.

  >>> from getpaid.authorizedotnet.authorizenet import AuthorizeNetAdapter
  >>> authnet = AuthorizeNetAdapter(DummyAuthContext())
  >>> authnet.authorize(order, payment) == interfaces.keys.results_success
  True

Capturing/Charging an Order
---------------------------

Capturing an order tells Authorize.net to queue payment for settlement. (Actual
settlement happens in a daily batch process.)

  >>> authnet.capture(order, order.getTotalPrice()) == interfaces.keys.results_success
  True


Refunding an Order
------------------

Refunding an order tells Authorize.net to return the payment to the customer.

Refunds cannot be issued until the original payment has been captured and settled,
so we don't expect this to succeed in the test.

  >>> authnet.refund(order, order.getTotalPrice())
  'The referenced transaction does not meet the criteria for issuing a credit.'

Voiding an Order
----------------


Orders with recurring line items
--------------------------------

If an order whose cart contains a recurring line item is authorized, it will
result in the creation of a subscription-based payment using Authorize.net's
Automated Recurring Billing (ARB) API.  The subscriptionId will be recorded
on the order as its transaction ID.

Note that the creation of the subscription happens during the call to
``authorize``, not ``capture``, because it needs access to the billing
information which is passed to ``authorize`` but not ``capture``.

  >>> import copy
  >>> from zope.annotation.interfaces import IAnnotations
  >>> order2 = copy.deepcopy(order)
  >>> cart2 = cart.ShoppingCart()
  >>> cart2['abc'] = abc = item.RecurringLineItem()
  >>> abc.cost = 22.20; abc.name = 'abc'; abc.quantity = 1
  >>> abc.interval = 1; abc.total_occurrences = 3; abc.unit = 'months'
  >>> order2.shopping_cart = cart2
  >>> order2._order_id = 'recur%s' % int(time.time())
  >>> authnet.authorize(order2, payment) == interfaces.keys.results_success
  True
  >>> subscriptionId = IAnnotations(order2)[interfaces.keys.processor_txn_id]
  >>> subscriptionId is not None
  True

In the case of a recurring order ``capture`` is basically a no-op, but still
needs to succeed, because it will get called by the order workflow.

  >>> authnet.capture(order2, order2.getTotalPrice()) == interfaces.keys.results_success
  True

Orders with multiple recurring line items, or with a mixture of recurring and
non-recurring line items, are not currently supported.

A recurring order will first be authorized using the standard AIM API,
to make sure that valid CC info was provided.

  >>> payment2 = copy.deepcopy(payment)
  >>> payment2.credit_card = '1111111111111'
  >>> authnet.authorize(order2, payment2)
  'The credit card number is invalid.'

Canceling a recurring payment subscription
------------------------------------------

If an order has a subscriptionId, its subscription can be canceled.

  >>> authnet.cancel_subscription(order2) == interfaces.keys.results_success
  True
