When making an e-commerce system one of the key points, is often the ability for customers to pay online with credit card or others online payment services. Tea Commerce comes with a couple of pre-installed payment providers, but one of the great things is if you need to use a payment provider that isn’t given to you out of the box – it’s really simple to make you own. It’s as simple as inherit from a single .Net class and implement some metods and that’s it!

APaymentProvider

APaymentProvider is an abstract class, in the TeaCommerce.Payment.dll, all payment providers in Tea Commerce needs to inherit from, to be able to process payments through the Tea Commerce API and admin interface. When your payment provider inherits from APaymentProvider you will have to implement 6 different methods and a property – each explained below
All methods that returns a valid PaymentCallbackInfo will change the order’s transaction information.

FormPostUrl

You need to implement this property as it tells Tea Commerce what url to post the payment request to when the customer makes it to the payment step.

string FormPostUrl { get { return "http://www.teacommerce.dk"; } }

GenerateForm

When webshop customers is ready to go to the payment step – and the front-end developer call TeaCommerce.goToPayment() in our javascript API – Tea Commerce will see what payment method the customer selected, and based on the related payment provider, Tea Commerce generates a form and post it to the FromPostUrl. Return a dictionary containing the name/value pairs that builds up the form
Summary

Dictionary<string, string> GenerateForm( string orderName, string currencyISOCode, decimal amount, string defaultContinueUrl, string defaultCancelUrl, string defaultCallBackUrl, Dictionary<string, string> settings );

Example

Dictionary<string, string> GenerateForm( string orderName, string currencyISOCode, decimal amount, string defaultContinueUrl, string defaultCancelUrl, string defaultCallBackUrl, Dictionary<string, string> settings ){
  Dictionary<string, string> nameValues = new Dictionary<string, string>();
  nameValues.Add( "provider", "TeaCommerce" );
  nameValues.Add( "secretKey", "Expect simplicity" );
  return nameValues;
}

ProcessCallback

This method is called when Tea Commerce recieves a callback from the payment gateway. You need to process the request and tell Tea Commerce if the callback is valid

PaymentCallbackInfo ProcessCallback( HttpRequest request, Dictionary<string, string> settings );

GetStatus

For Tea Commerce to show the newest information about a transaction, this method is called when the webshop owner views an order in the admin.

PaymentCallbackInfo GetStatus( string transactionId, Dictionary<string, string> settings );

CapturePayment

Called when capturing the transaction from the Tea Commerce admin

PaymentCallbackInfo CapturePayment( string transactionId, string currencyISOCode, decimal amount, Dictionary<string, string> settings );

RefundPayment

Called when refunding the transaction from the Tea Commerce admin

PaymentCallbackInfo RefundPayment( string transactionId, string currencyISOCode, decimal amount, Dictionary<string, string> settings );

CancelPayment

Called when cancelling the transaction from the Tea Commerce admin

PaymentCallbackInfo CancelPayment( string transactionId, Dictionary<string, string> settings );

Helper methods

To make it much easier for your to make a payment provider for Tea Commerce, we have implemented some generic used methods in the APaymentProvider base class for you to use

MakePostRequest

These methods makes a post request for you and returns the response as a string.

string MakePostRequest( string url, Dictionary<string, string> inputFields );
string MakePostRequest( string url, string request );

GenerateContinueUrl

For Tea Commerce to work correctly, you need to use this method when generating a continue url. This methods makes a wrapper for your url, which force the continue url to be returned to Tea Commerce and process the request from the payment gateway before returning to your specified continue url.

string GenerateContinueUrl( string finalContinueUrl );

GenerateCancelUrl

This one works just like the GenerateContinueUrl method, just for the cancel url.

string GenerateCancelUrl( string finalCancelUrl );

GetMD5Hash

Some payment gateways use a MD5 hash as a security mechanism and this method makes it easy to generate one.

string GetMD5Hash( string input );

Extra properties

There is some extra properties for you to control in the APaymentProvider

//Return true to finalize the order as soon as the customer accepts the payment terms
bool AutoFinalize { get { return false; } }

//Add extra attributes to the genereated form element in GenerateForm()
string FormAttributes { get { return @"name=\""ePay\"" target=\""ePay_window\"" id=\""ePay\"""; } }

//If a method is specified - Tea Commerce will call it instead of posting the form when goToPayment is called
string SubmitJavascriptFunction { get { return @"open_ePay_window();"; } }

//These properties tells Tea Commerce if the specific action is possible with your payment provider
bool AllowsGetStatus { get { return true; } }
bool AllowsCapturePayment { get { return true; } }
bool AllowsRefundPayment { get { return true; } }
bool AllowsCancelPayment { get { return true; } }

PaymentCallbackInfo

This is the object almost every APaymentProvider method returns. The object should contain information about the response from the payment provider. When returning this object, if it’s valid, it will be used to change the order’s current transaction information

/// <summary>
/// Payment callback info
/// </summary>
///<param name="orderName">Name of the order</param>
///<param name="amount">Amount of the order</param>
///<param name="state">State of the order</param>
///<param name="transactionId">Id of the transaction</param>
///<param name="paymentStatus">The new payment status for the order to have</param>
PaymentCallbackInfo( string orderName, decimal amount, string state, string transactionId, PaymentStatus paymentStatus );

/// <summary>
/// Used to tell Tea Commerce an error occured in the payment provider
/// </summary>
///<param name="errorMessage">Message to show in Tea Commerce</param> 
PaymentCallbackInfo( string errorMessage );