Case Study: Automating Payment

I would like to share with the java gaming community my experiences with online payments. Because we’re entering the participation age, it’s important for me to focus on the advantages of sharing the revenues of our company with the customers. Customers in the participation age is more than consumers, they’re active participants who add value to the site. This added value attracts more buyers. I expect that it will soon - sometimes - be a sound buisiness decision to pay, yes, to pay your customers.

Automating the payment process with Java is much simpler than one would expect. I’d like to give an example of how this can be done.

Last year I experimented with some code that makes a lookup in a database and logs into my companys PayPal account and transfers money to other accounts, based on the information in this database. PayPal is a HTTPS site. The code underneeth has no database logic but simply logs into PayPal and transfers money to another account.



package indie.postmoderngames.paypal;

import com.meterware.httpunit.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * <p>Copyleft (L) 2002-2006. All rights reserved.</p>
 * <p>Donated by Post Modern Games</p>
 *
 * @author Morten
 * @version 1.01 03-02-2005
 */
public class PaypalProxy
{
    public PaypalProxy()
    {
        try {
            // create the conversation object which will maintain state for us
            WebConversation wc = new WebConversation();

            // Obtain the main page on the meterware web site
            WebRequest request = new GetMethodWebRequest( "https://www.paypal.com" );
            WebResponse response = wc.getResponse( request );

            WebForm webForm = response.getFormWithName("login_form");
            webForm.setParameter("login_email", "<your email here>");
            webForm.setParameter("login_password","<your password here>");

            WebResponse r2 = webForm.submit();

            WebLink link = r2.getFirstMatchingLink(WebLink.MATCH_CONTAINED_TEXT, "click here");
            WebResponse r3 = link.click();

            WebLink link2 = r3.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "_transaction-run");
            WebResponse r4 = link2.click();
            WebForm form2 = r4.getFormWithName("focusform");
            form2.setParameter("email","<receiver email here>");
            form2.setParameter("amount","0.01");
            form2.setParameter("amount_ccode","AUD");
            form2.setParameter("payment_type","S");
            form2.setParameter("subject", "Coin1 has sent you money");
            form2.setParameter("note", "You have received money.");
            WebResponse r5 = form2.submit();

            WebForm[] forms = r5.getForms();
            System.out.println("number of forms: " + forms.length);

            WebForm form3 = forms[0];
            SubmitButton submit = form3.getSubmitButton("submit.x");

            WebResponse r6 = form3.submit(submit);

            BufferedReader input = new BufferedReader(new InputStreamReader(r6.getInputStream(), "UTF8"));
            String str;
            while (null != ((str = input.readLine()))) {
                System.out.println(str);
            }
        } catch (Exception e) {
            System.err.println( "Exception: " + e );
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        new PaypalProxy();
    }
}

The code is for you to take.

Best regards Morten

That’s pretty easy to understand! Thanks for sharing. :slight_smile:

I have two questions:

  1. It does not seem you are using the PayPal API, but rather access the web site: “r4.getFormWithName(“focusform”);”. What happens if PayPal decides to change from let’s say “focusform” -> “F_FORM”? Does your program still work? Or how do you cope with that?

  2. Did you try the PayPal API? I saw some Java SDK to access PayPal before. This might be more reliable?

Hi again Kingaschi - our first user ;D

  1. On that occasion it doesn’t work. You’re right.

  2. Thanks for the suggestion. I wasn’t aware of this API.

br Morten

I never tried it, but you can get the Java SDK for PayPal from: https://www.paypal.com/IntegrationCenter/ic_sdk-resource.html.

Thanks. Best of luck Kingaschi!