Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mitton 22 posts 71 karma points c-trib
    Jun 04, 2015 @ 08:43
    Mitton
    1

    Architecture of a PaymentMethod

    I'm trying to create a payment gateway provider for PayFast.

    http://www.payfast.co.za

    I'm struggling to make sense of the available documentation, and I wonder if the community would be so kind as to provide a little architecture review so I can make sure I understand the principles correctly.

    I've created two interfaces and two classes and implemented the abstract methods as per the recommendations.

    public interface IPayFastPaymentGatewayMethod public interface IPayFastPaymentGatewayProvider public class PayFastPaymentGatewayMethod : PaymentGatewayMethodBase, IPayFastPaymentGatewayMethod public class PayFastPaymentGatewayProvider : PaymentGatewayProviderBase, IPayFastPaymentGatewayProvider

    Am I correct in saying that the purpose of these classes would be to update the records in Merchello, and not really to communicate with the provider themselves?

    PayFast works by having the merchant site redirect the user with a post to a page on their site. The user then performs the payment there. Before sending the user back to your site, PayFast posts details about the transaction back to a specified address on the merchant site.

    What I've done, is to create a page on my site that accepts the post data from PayFast. A controller for this post then calls the methods on PayFastPaymentGatewayMethod, to create the invoice, order and payment.

    Is that basically how it's supposed to work?

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jun 04, 2015 @ 17:54
    Rusty Swayne
    1

    Hi Mitton,

    It looks like your close as you have the two main classes defined.

    The Provider Class

    Your PayFastPaymentGatewayProvider is responsible for creating and offering PayFast payment methods to Merchello. You need to make sure that the GatewayProviderActivationAttribute decorates it which is where the reference information that Merchello stores is located (key, name, description and an optional dialog editor view). If you include a dialog editor view, it allows you to create an angular view to be popped open to accept configurations for your provider, like username, password and some sort of mode - sandbox or live if available through PayFast.

    The ListResourcesOffered method is the way Merchello knows how many methods are allowed to be created. For example, PayFast may have the redirect method and also allow you to directly accept credit cards by adding a form to your site and sending the information to them server side without redirecting. So in that case you would have two. However, if you only have the redirection you would do something like:

        #region AvailableResources
    
        /// <summary>
        /// The available resources.
        /// </summary>
        internal static readonly IEnumerable<IGatewayResource> AvailableResources = new List<IGatewayResource>()
        {
            new GatewayResource("PayFastRedirect", "PayFast Redirect") 
        };
    
        #endregion
    
    
        /// <summary>
        /// Returns a list of remaining available resources
        /// </summary>
        /// <returns>
        /// The collection of <see cref="IGatewayResource"/>.
        /// </returns>
        public override IEnumerable<IGatewayResource> ListResourcesOffered()
        {
            // PaymentMethods is created in PaymentGatewayProviderBase.  It is a list of all previously saved payment methods
            return AvailableResources.Where(x => PaymentMethods.All(y => y.PaymentCode != x.ServiceCode));
        }
    

    The Payment Method class(es)

    Payment methods are responsible for handling the specific communication with the provider. The PaymentGatewayMethodBase class will require you to define several common methods.

    • Authorize
    • AuthorizeCapture
    • Capture
    • Void
    • Refund

    When working with a payment gateway method that redirects to accept payment, most people do the redirection in the Authorize method.

    So the workflow looks like:

    Build the invoice as normal (usually in the SalePreparation class) and then calling the Basket.SalePreparation().Authorize( ... ) function passing in the payment method key from the method that does the redirect. Before redirecting, you will save the record of the authorized payment get the payment key and maybe the invoice key to use as a parameter(s) for redirecting back on confirmation.

    Then, after PayFast has done it's thing, redirect back to a custom handler that finds the invoice and then uses the same PayFastPaymentGateWayMethod to "Capture" the payment.

    Barry Fogarty did a SagePay provider that works in the same way. You can check that out here: https://github.com/BluefinDigital/Merchello.Plugins.SagePay

    Let me know how it goes =)

Please Sign in or register to post replies

Write your reply to:

Draft