satchmo 自定义支付模块

作者: admin 分类: satchmo 发布时间: 2013-08-12 12:01 ė 6 没有评论

satchmo虽然支持很多支付模块,但是很纠结的说不支持国内的支付宝,这个文档我依旧是翻译官网自定义的satchmo支付模块

概述:

satchmo 的付款都是模块化,你应该可以基于现在的模块创建自己的支付方式.

所有的支付模块都存储在/payment/modules 这个目录,你快速浏览下子目录会看到很多文件:

  • __init__.py
  • config.py
  • processor.py
  • urls.py
  • views.py

The __init__.py is required so that satchmo can import the files. There is no need to put any code in this file. Just be sure it exists!

The rest of the files are described below.

Building your processor

The processor.py file is where the majority of the heavy lifting is done. The processor does 4 things:

  • Sets up its configuration for the service (__init__)
  • Takes order data and formats it in the appropriate manner (prepareData)
  • Sends the data to the processing server/url (authorize_paymentcapture_paymentcapture_authorized_payment) and returns the results.

Of them, only capture_payment is required. If the processor can both authorize and process, then you need to add a functioncan_authorize, which returns True, and implement the other two methods. See the dummy module for an example.

Optionally, the processor can include include test code so that it is easy to verify from the command line.

Here is a stub you can use to create your own processor:

from payment.modules.base import BasePaymentProcessor, ProcessorResult

class PaymentProcessor(BasePaymentProcessor):
    def __init__(self, settings):
        # Set up your configuration for items like
        # Test server, url, various flags and settings
        super(PaymentProcessor, self).__init__('example', settings)

    def capture_payment(self):
        # Send the data via the appropriate manner and return a ProcessorResult
        # object.

Refer to the dummy, authorize.net, cybersource or trustcommerce modules for various examples to help you through the process.

Configuration

Each processor will have unique variables that need to be set. The config.py file is where you can leverage the Satchmo settings capability to add your unique variables. Please refer to the Basic Configuration in order to understand how the system works. For more examples, the existing working modules are great examples of what to setup. The basic format is:

Create the new configuration group:

PAYMENT_GROUP = ConfigurationGroup('PAYMENT_MYNEWPROCESSOR',
    _('My New Processor Payment Settings'),
    ordering=102)

Note

The key of the ConfigurationGroup must be the module name, upper-cased.

For example, if your custom payment module was located in mypaymentmodules.mynewprocessor, the ConfigurationGroup should be given the key PAYMENT_MYNEWPROCESSOR. The reason for this is that the active_gateways() function in payment/__init__.py attempts to automatically determine these keys by appending the upper-cased module name to PAYMENT_.

Now register the settings you need:

config_register_list(
    BooleanValue(PAYMENT_GROUP,
        'LIVE',
        description=_("Accept real payments"),
        help_text=_("False if you want to be in test mode"),
        default=False),

    StringValue(PAYMENT_GROUP,
        'LABEL',
        description=_('English name for this group on the checkout screens'),
        default = 'Credit Cards',
        help_text = _('This will be passed to the translation utility')),

    StringValue(PAYMENT_GROUP,
        'URL_BASE',
        description=_('The url base used for constructing urlpatterns which will use this module'),
        default = r'^credit/'),

    MultipleStringValue(PAYMENT_GROUP,
        'CREDITCHOICES',
        description=_('Available credit cards'),
        choices = (
            (('Amex', 'American Express')),
            (('Visa','Visa')),
            (('Mastercard','Mastercard')),
            (('Discover','Discover'))),
        default = ('Visa', 'Mastercard', 'Discover')),

    StringValue(PAYMENT_GROUP,
        'PASSWORD',
        description=_('Your Processor password'),
        default=""),

    BooleanValue(PAYMENT_GROUP,
        'EXTRA_LOGGING',
        description=_("Verbose logs"),
        help_text=_("Add extensive logs during post."),
        default=False)
)

All of these settings can be accessed in your __init__ method (shown above). For example, the LIVE value above can be accessed by using settings.LIVE.value

Views

Most payment processing have similar steps:

  • Collect demographic information
  • Collect payment information
  • Confirm info is correct
  • Return a status

The views.py file contains the information that maps your processor views to the existing views or your own custom view.

For most people, the views contained in payment.views will be sufficient. The example below maps these views to views already available in Satchmo:

from livesettings import config_get_group
from payment.views import confirm, payship

def pay_ship_info(request):
    return payship.credit_pay_ship_info(request, config_get_group('PAYMENT_MYNEWPROCESSOR'))

def confirm_info(request):
    return confirm.credit_confirm_info(request, config_get_group('PAYMENT_MYNEWPROCESSOR'))

However, there is nothing stopping you from creating your own view:

def confirm_info(request):
    # Do a lot of custom stuff here
    return render_to_response(template, context)

All of the current satchmo payment views are in /payment/views. Please review these before trying to build one of your own!

Url configuration

Now that you have built your processor, configured your settings and built your views, you need to tell Satchmo how to access these views. This is where the urls.py file is useful.

For most processors, a simple file would look like this:

from django.conf.urls.defaults import *
from livesettings import config_value, config_get_group

config = config_get_group('PAYMENT_MYNEWPROCESSOR')

urlpatterns = patterns('satchmo',
     (r'^$', 'mypaymentmodules.myprocessor.views.pay_ship_info',
                    {'SSL':config.SSL.value}, 'MYNEWPROCESSOR_satchmo_checkout-step2'),
     (r'^confirm/$', 'payment.modules.trustcommerce.views.confirm_info',
                    {'SSL':config.SSL.value}, 'MYNEWPROCESSOR_satchmo_checkout-step3'),
     (r'^success/$', 'payment.views.checkout.success',
                    {'SSL':config.SSL.value}, 'MYNEWPROCESSOR_satchmo_checkout-success'),
)

The nice thing about this file is that it allows you to easily plug in the views you need and rename the urls to whatever form you need. Just make sure to maintain the naming convention for the urls as shown above.

Enabling the new module

In order to enable your new payment processor, you must add it to your INSTALLED_APPS setting in your settings.py. For example:

INSTALLED_APPS = (
        ...
        'payment',
        'mypaymentmodules.myprocessor',

本文出自 小Q,转载时请注明出处及相应链接。

本文永久链接: http://www.linuxqq.com/archives/1088.html

0
更多
Ɣ回顶部