Created
November 22, 2016 13:35
-
-
Save chethanbandi/fa2540cf4b9897b346a18b8207e887c2 to your computer and use it in GitHub Desktop.
paypal reference transaction example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cbandi.billing; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.util.Arrays; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.FormBody; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class App { | |
private static final Logger log = LoggerFactory.getLogger(App.class); | |
private static final String AUTH_URL = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=%s"; | |
private static final OkHttpClient okHttpClient = new OkHttpClient.Builder() | |
.addInterceptor(new LoggingInterceptor()) | |
.readTimeout(60, TimeUnit.SECONDS) | |
.build(); | |
public static void main(String[] args) throws IOException { | |
//expressCheckout(); | |
//String billingAgreementId = createBillingAgreement(); | |
//doReferenceTransaction(billingAgreementId); | |
doReferenceTransaction("B%2d7PN3209809538734N"); | |
} | |
private static void expressCheckout() throws IOException { | |
RequestBody body = new FormBody.Builder() | |
.add("USER", "chethanbandi-facilitator_api1.gmail.com") | |
.add("PWD", "LJKTSSP6NHP3R3UJ") | |
.add("SIGNATURE", "AFcWxV21C7fd0v3bYYYRCpSSRl31AQR-m3oESR3VblA6.PArPx.X1TiQ") | |
.add("METHOD", "SetExpressCheckout") | |
.add("L_BILLINGTYPE0", "MerchantInitiatedBilling") | |
.add("L_BILLINGAGREEMENTDESCRIPTION0", "SellinAll Services") | |
.add("PAYMENTREQUEST_0_PAYMENTACTION", "AUTHORIZATION") | |
.add("PAYMENTREQUEST_0_AMT", "0") | |
.add("PAYMENTREQUEST_0_CURRENCYCODE", "USD") | |
.add("cancelUrl", "http://www.return.com/") | |
.add("returnUrl", "http://www.return.com/") | |
.add("VERSION", "86") | |
.build(); | |
String response = execute(body); | |
String authUrl = Arrays.stream(response.split("&")) | |
.filter(s -> s.startsWith("TOKEN")) | |
.map(s -> s.split("=")[1]) | |
.map(s -> String.format(AUTH_URL, s)) | |
.findFirst() | |
.orElse(null); | |
log.info("Auth url\n {}", authUrl); | |
} | |
private static String createBillingAgreement() throws IOException { | |
byte[] token = new byte[100]; | |
System.out.print("Enter token value : "); | |
int bytesread = System.in.read(token); | |
RequestBody body = new FormBody.Builder() | |
.add("USER", "chethanbandi-facilitator_api1.gmail.com") | |
.add("PWD", "LJKTSSP6NHP3R3UJ") | |
.add("SIGNATURE", "AFcWxV21C7fd0v3bYYYRCpSSRl31AQR-m3oESR3VblA6.PArPx.X1TiQ") | |
.add("METHOD", "CreateBillingAgreement") | |
.add("VERSION", "86") | |
.add("TOKEN", new String(token, 0, bytesread - 1, Charset.defaultCharset())) | |
.build(); | |
return Arrays.stream(execute(body).split("&")) | |
.filter(s -> s.startsWith("BILLINGAGREEMENTID")) | |
.map(s -> s.split("=")[1]) | |
.findFirst() | |
.orElse(null); | |
} | |
private static void doReferenceTransaction(String billingAgreementId) throws IOException { | |
RequestBody body = new FormBody.Builder() | |
.add("USER", "chethanbandi-facilitator_api1.gmail.com") | |
.add("PWD", "LJKTSSP6NHP3R3UJ") | |
.add("SIGNATURE", "AFcWxV21C7fd0v3bYYYRCpSSRl31AQR-m3oESR3VblA6.PArPx.X1TiQ") | |
.add("METHOD", "DoReferenceTransaction") | |
.add("AMT", "100") | |
.add("CURRENCYCODE", "USD") | |
.add("PAYMENTACTION", "SALE") | |
.addEncoded("REFERENCEID", billingAgreementId) | |
.add("VERSION", "86") | |
.build(); | |
execute(body); | |
} | |
private static String execute(RequestBody body) throws IOException { | |
Request request = new Request.Builder() | |
.url("https://api-3t.sandbox.paypal.com/nvp") | |
.post(body) | |
.build(); | |
return okHttpClient.newCall(request).execute().body().string(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment