Wednesday 11 July 2012

Wepay integaration for asp.net , c#


The steps followed while implementing wepay in Asp.net application:

1) Wepay offers two type of payments gateway:
    a) Accept payements for your own site.
    b) Let your users accept payments through your application.
Here I had created application to accept payment for own site. We need to register application on WePay stage server.
Go to the application registration page "https://stage.wepay.com/developer/register" and fill in the name, description, and homepage of site. After you click submit you will be shown some details of your application. One of these is an access token. This access token is what lets you make calls to the WePay API







2) Open an account to hold the payments you receive. We can create a new account or we can use the existing account which created when registering your application. To know your account id go to url "https://stage.wepay.com/app". Then select your application and navigate to tab "Details", which consist of all details like ID,Secret,Access_Token,Account_Id.






3) When you user want to pay, redirect them to checkout URI of WePay. We can get this checkout URI using the web service " /v2/checkout/create ". We need to pass some arguments with web service call like
account_id: The unique ID of the account you want the money to go into
amount: The amount you want paid, ex 50.00
short_description: A short description of what the payment is for ex 'donation to Bella's college fund'.
type : GOODS/SERVICE/DONATION/PERSONAL.
redirect_uri: Where the payer will be returned to after making the payment

When we call this service we will recieve  the checkout_id and checkout_uri as a response. We will then send the payer to the checkout_uri so that they can enter their payment information, confirm their payment, and then be returned to your site.


    private void GetCheckoutUri(string accountId, string amount, string desc, string accesstoken)
    {
        string url = @"https://stage.wepayapi.com/v2/checkout/create?account_id=" + accountId + "&amount=" + amount + "&short_description=" + desc + "&type=SERVICE&redirect_uri=http://localhost:2645/ PaymentSuccess.aspx";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)";
        request.Headers.Add("Authorization", "Bearer "+accesstoken);
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse();
        Stream streamResponse = myHttpWebResponse.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);

        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);
        string jsonstring = string.Empty;
        while (count > 0)
        {
            String outputData = new String(readBuff, 0, count);
            jsonstring += outputData.ToString();
            count = streamRead.Read(readBuff, 0, 256);
        }

        string[] codes = jsonstring.Split(',');
        string checkOutUri = codes[1].Replace("\"", "").Replace("checkout_uri:", "").Replace("}", "");


        // Release the response object resources.
        streamRead.Close();
        streamResponse.Close();
        myHttpWebResponse.Close();
        Response.Redirect(checkOutUri, false);
    }

4) When user has confirm their payment, and returned to our site, we can show them the receipt of payment. To get the payment details we need to call the web service “/checkout/” with parameter checkout_id in response we get the following :
checkout_id : The unique ID of the checkout.
account_id: The unique ID of the account that the money will go into.
state: The state the checkout is in. See the API object state section for a listing of all states.
short_description: The short description of the checkout. Max 127 chars.
long_description: The long description of the checkout (if available). Max 2047 chars.
currency: The currency used (always "USD" for now).
amount: The dollar amount of the checkout object (ex. 3.50). This will always be the amount you passed in /checkout/create
fee: The dollar amount of the WePay fee.
gross: The total dollar amount paid by the payer.
app_fee: The dollar amount that the application received in fees. App fees go into the API application's WePay account.
fee_payer: Who is paying the fee (either "payer" for the person paying or "payee" for the person receiving the money).
reference_id: The unique reference_id passed by the application (if available).
redirect_uri: The uri the payer will be redirected to after payment (if available). Max 2083 chars.
callback_uri: The uri which Instant Payment Notifications will be sent to. Max 2083 chars.
payer_email: The email address of the person paying (only returned if a payment has been made).
payer_name: The name of the person paying (only returned if a payment has been made).
cancel_reason: If the payment was canceled, the reason why.
refund_reason: If the payment was refunded the reason why.
auto_capture: Default is true. If set to false then the payment will not automatically be released to the account and will be held by WePay in payment state 'reserved'. To release funds to the account you must call /checkout/capture
require_shipping: Default is false. If set to true then the payer will be required to enter their shipping address when paying.
shipping_address: If 'require_shipping' was set to true and the payment was made, this will be the shipping address that the payer entered. It will be in the following format:
{"address1":"380 Portage Ave","address2":"","city":"Palo Alto","state":"CA","zip":"94306","country":"US"}
tax: The dollar amount of taxes paid.
amount_refunded: If this checkout has been fully or partially refunded, this has the amount that has been refunded so far.
create_time: The unixtime when the checkout was created.



    private void GetCheckoutStatus(string checkout_id )
    {
        string url = @"https://stage.wepayapi.com/v2/checkout?checkout_id=" + checkout_id;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)";
        request.Headers.Add("Authorization", "Bearer " + Session["accesstoken$"].ToString());
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse();
        Stream streamResponse = myHttpWebResponse.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);

        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);
        string jsonstring = string.Empty;
        while (count > 0)
        {
            String outputData = new String(readBuff, 0, count);
            jsonstring += outputData.ToString();
            count = streamRead.Read(readBuff, 0, 256);
        }

        string[] codes = jsonstring.Split(',');
       // Release the response object resources.
        streamRead.Close();
        streamResponse.Close();
        myHttpWebResponse.Close();
       createpaymentreciept(codes);
       
    }

we get the all details of payment as response in string “codes”. We can now design the payment receipt as required in the method ”createpaymentreciept”.We get the payment state in one of the following stages:

new  The checkout was created by the application
authorized The payer entered their payment info and confirmed the payment on WePay
reserved The payment has been reserved from the payer
captured The payment has been credited to the payee account
settled The payment has been settled and no further changes are possible
cancelled The payment has been cancelled by the payer, payee, or application
refunded The payment was captured and then refunded by the payer, payee, or application. The payment has been debited from the payee account.
charged back The payment has been charged back by the payer and the payment has been debited from the payee account.
 Failed The payment has failed.
Expired Checkouts get expired if they are still in state new after 30 minutes (i.e., they have been abandoned).



1 comment:

  1. Aleem,

    Do you help us on wepay account creation using asp.net. We are using wepay for payment, but got stuck to create account for different users? Please advice.

    i followed the below link & achieved till access token, but unable to proceed further.

    https://stage.wepay.com/developer/platform/authorization
    Thanks & Regards,
    Vadivel.

    ReplyDelete