Paymentmodel in Spree tracks payments against Orders. Payments relate to a
sourcewhich indicates how the payment was made, and a
PaymentMethod, indicating the processor used for this payment.
checkout: Checkout has not been completed
processing: The payment is being processed (temporary – intended to prevent double submission)
pending: The payment has been processed but is not yet complete (ex. authorized but not captured)
failed: The payment was rejected (ex. credit card was declined)
void: The payment should not be counted against the order
completed: The payment is completed. Only payments in this state count against the order total
PaymentMethodcan have the following attributes:
type: The subclass of
Spree::PaymentMethodthis payment method represents. Uses rails single table inheritance feature.
name: The visible name for this payment method
description: The description for this payment method
active: Whether or not this payment method is active. Set it
falseto hide it in frontend.
display_on: Determines where the payment method can be visible. Values can be
backfor backend or
PaymentMethod.availablemethod. The code is this:
auto_capture?method depends on the
Spree::Config[:auto_capture]preference. If you have set this preference to
true, but don't want a payment method to be auto-capturable like other payment methods in your system, you can override the
auto_capture?method in your
customer: The email address related to the order
ip: The last IP address for the order
order_id: The Order's
numberattribute, plus the
identifierfor each payment, generated when the payment is first created
shipping: The total shipping cost for the order, in cents
tax: The total tax cost for the order, in cents
subtotal: The item total for the order, in cents
currency: The 3-character currency code for the order
discount: The promotional discount applied to the order
billing_address: A hash containing billing address information
shipping_address: A hash containing shipping address information
name: The combined
last_namefrom the address
address1: The first line of the address information
address2: The second line of address information
city: The city of the address
state: An abbreviated version of the state name or, failing that, the state name itself, from the related
Stateobject. If that fails, the
state_nameattribute from the address.
country: The ISO name for the country. For example, United States of America is "US", Australia is "AU".
phone: The phone number associated with the address
Paymentobject associated with the order has the
process!method called on it (unless
payment_required?for the order returns
false), in order to attempt to automatically fulfill the payment required for the order. If the payment method requires a source, and the payment has a source associated with it, then Spree will attempt to process the payment. Otherwise, the payment will need to be processed manually.
PaymentMethodobject is configured to auto-capture payments, then the
Payment#purchase!method will be called, which will call
Payment#authorize!method will be called, with the same arguments as the
PaymentMethodsub-class' implementation of the
authorizemethods on the payment method objects must be an
ActiveMerchant::Billing::Responseobject. This response object is then stored (in YAML) in the
spree_log_entriestable. Log entries can be retrieved with a call to the
log_entriesassociation on any
purchase!route is taken and is successful, the payment is marked as
completed. If it fails, it is marked as
failed. If the
authorizemethod is successful, the payment is transitioned to the
pendingstate so that it can be manually captured later by calling the
capture!method. If it is unsuccessful, it is also transitioned to the
ActiveMerchant::Billing::Responseobjects. When Spree handles a response from a payment gateway, it will serialize the object as YAML and store it in the database as a log entry for a payment. These responses can be useful for debugging why a payment has failed.
Spree::Paymentobject. To get the
Active::Merchant::Billing::Responseout of these
Spree::LogEntryobjects, call the
spree_gatewaygem included in your application's
Gemfile, these gateways will be selectable in the admin backend for payment methods.
app/models/spree/gatewayinside that project.