Using C# to call Dwolla Off-Site Gateway / Submit Directly

333 views Asked by At

I am attempting to utilize Dwolla's Off-Site Gateway via the "Submit Directly" workflow (https://developers.dwolla.com/dev/pages/gateway#submit-directly) into my custom ASPX/C#-based website.

I have been able to successfully add a Dwolla button to an ASPX page using the script that they provide:

<script
    src="https://www.dwolla.com/scripts/button.min.js" class="dwolla_button" type="text/javascript"
    data-key="ConsumerKeyObtainedFromDwolla"
    data-redirect="RedirectPage.aspx"
    data-label="Dwolla"
    data-name="MyNameGoesHere"
    data-description="MyDescriptionGoesHere"
    data-amount="123.45"
    data-shipping="0"
    data-tax="0"
    data-guest-checkout="true"
    data-type="freetype"
    >
</script>

However I need to also include a PayPal button on the same page, and don't want the script input tags to conflict. I also want to easily populate variables (such as the HMAC-SHA1 hexadecimal hash of the consumer key, timestamp, and order ID) and do some calculations without having to do so in javascript. So my goal is to do this all in the C# code-behind of the page.

My first step was to simply remove the form tags from the PayPal script and add an ASP button with PayPal's PostBackURL. This worked, so I further refactored the PayPal section completely out of the ASPX and implemented C# code to build a redirect URL based on the guts of the PayPal script:

string txtRedirectURL = "";
txtRedirectURL += "https://www.paypal.com/cgi-bin/webscr?&cmd=_xclick";
txtRedirectURL += "&business=A1B2C3D4";
txtRedirectURL += "&lc=US";
...
txtRedirectURL += "&item_name=abcdefg";
txtRedirectURL += "&amount=123.45";
txtRedirectURL += "&currency_code=USD";
Response.Redirect(txtRedirectURL);

This worked well, so I was hoping to do the same with the script that Dwolla uses (as documented above). This approach unfortunately has not proven as successful. The first option I've tried was to mimic the PayPal redirect based on the data fields in the Dwolla script:

string txtRedirectURL = "";
txtRedirectURL += "https://www.dwolla.com/payment/pay?";
txtRedirectURL += "key=ConsumerKeyObtainedFromDwolla";
txtRedirectURL += "&label=Dwolla";
txtRedirectURL += "&name=MyNameGoesHere";
txtRedirectURL += "&description=MyDescriptionGoesHere";
txtRedirectURL += "&amount=123.45";
txtRedirectURL += "&shipping=0.00";
txtRedirectURL += "&tax=0.00";
Response.Redirect(txtRedirectURL);

This does try to navigate me to Dwolla's https://www.dwolla.com/payment/pay page, but it ultimately navigates me to Dwolla's 404 page (the crying blue koala bear). I've also added various versions of the following lines to no better success:

txtRedirectURL += "&signature=HMACSHA1Hash;
txtRedirectURL += "&test=true";
txtRedirectURL += "&destinationid=UserIDObtainedFromDwolla";
txtRedirectURL += "&orderid=999;
txtRedirectURL += "&timestamp=" + txtTimeStamp;
txtRedirectURL += "&allowFundingSources=true";

My assumption is that either:

  1. something in my URL is throwing things off and Dwolla's error handling is throwing me to the 404 page instead of showing the error (as I have seen when playing around with the buttons)

  2. The button.min.js script is doing something funky that I need to recreate in my C#. I've reviewed it, but can't make heads or tails of what that missing step might be.

I also tried a more direct approach to try to execute the script from inside the C#:

string dwollaScript = "<script
    src="https://www.dwolla.com/scripts/button.min.js" class="dwolla_button" type="text/javascript"
    data-key="ConsumerKeyObtainedFromDwolla"
    data-redirect="RedirectPage.aspx"
    data-label="Dwolla"
    data-name="MyNameGoesHere"
    data-description="MyDescriptionGoesHere"
    data-amount="123.45"
    data-shipping="0"
    data-tax="0"
    data-guest-checkout="true"
    data-type="freetype"
    >
</script>";
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "123", dwollaScript.ToString(), false);

This fires off successfully when bound to a button click, but all it does is display a Dwolla button on my page once it posts back. It doesn't navigate me to Dwolla like the regular Dwolla buttons do.

Any thoughts?

1

There are 1 answers

0
Gordon Zheng On

First off, an important note:

A better solution is to use the Server to Server Checkout request instead of Submit Directly, since it doesn't allow the user to modify any of the requests' parameters. You would simply need to POST to the endpoint and receive a checkout ID which you can use to generate a checkout URL for the user to follow. No JS trickery required.

I think you may find this Dwolla C# wrapper helpful. It supports both types of Gateway requests and Callback handling.

To answer your question:

It looks like you're trying to GET https://www.dwolla.com/payment/pay with the checkout parameters in querystring variables, which is not a valid way to request a checkout session.

The Submit Directly workflow requires you to create a form that will POST to https://www.dwolla.com/payment/pay. For example:

    <form accept-charset="UTF-8" action="https://www.dwolla.com/payment/pay" method="post">
    <input id="key" name="key" type="hidden" value="abcdefg" />
    <input id="signature" name="signature" type="hidden" value="abcd" />
    <input id="callback" name="callback" type="hidden" 
    value="http://www.mywebsite.com/callback.aspx" />
    <input id="redirect" name="redirect" type="hidden" 
    value="http://www.mywebsite.com/redirect.aspx" />
    <input id="test" name="test" type="hidden" value="true" />
    <input id="name" name="name" type="hidden" value="Purchase" />
    <input id="description" name="description" type="hidden" 
    value="Description" />
    <input id="destinationid" name="destinationid" type="hidden" 
    value="812-111-1111" />
    <input id="amount" name="amount" type="hidden" value="1.00" />
    <input id="shipping" name="shipping" type="hidden" value="0.00" />
    <input id="tax" name="tax" type="hidden" value="0.00" />
    <input id="orderid" name="orderid" type="hidden" value="188375" />
    <input id="timestamp" name="timestamp" type="hidden" 
    value="1323302400" />

    <button type="submit">Submit Order</button>
    </form>