diff --git a/app/src/main/java/io/token/sample/Application.java b/app/src/main/java/io/token/sample/Application.java index 0639ca0..97a53ac 100644 --- a/app/src/main/java/io/token/sample/Application.java +++ b/app/src/main/java/io/token/sample/Application.java @@ -3,14 +3,18 @@ import static com.google.common.base.Charsets.UTF_8; import static io.grpc.Status.Code.NOT_FOUND; import static io.token.TokenIO.TokenCluster.SANDBOX; -import static io.token.proto.common.alias.AliasProtos.Alias.Type.EMAIL; +import static io.token.TokenRequest.TokenRequestOptions.REDIRECT_URL; +import static io.token.proto.common.alias.AliasProtos.Alias.Type.DOMAIN; import static io.token.util.Util.generateNonce; import com.google.common.io.Resources; import io.grpc.StatusRuntimeException; -import io.token.Destinations; import io.token.Member; import io.token.TokenIO; +import io.token.TokenRequest; +import io.token.TransferTokenBuilder; +import io.token.proto.common.account.AccountProtos.BankAccount; +import io.token.proto.common.account.AccountProtos.BankAccount.Sepa; import io.token.proto.common.alias.AliasProtos.Alias; import io.token.proto.common.token.TokenProtos.Token; import io.token.proto.common.transfer.TransferProtos.Transfer; @@ -28,6 +32,7 @@ import java.util.LinkedHashMap; import java.util.Map; + import spark.Spark; /** @@ -58,22 +63,51 @@ public static void main(String[] args) throws IOException { // Initializes the server Spark.port(3000); - // Endpoint for transfer payment, called by client side after user approves payment. + // Endpoint for transfer payment, called by client side to initiate a payment. Spark.post("/transfer", (req, res) -> { Map formData = parseFormData(req.body()); - String tokenId = formData.get("tokenId"); - // Make sure to get the token first, - // and check its validity + TransferEndpoint destination = TransferEndpoint.newBuilder() + .setAccount(BankAccount.newBuilder() + .setSepa(Sepa.newBuilder() + .setIban(formData.get("destination")))) + .build(); + //create TokenRequest + TransferTokenBuilder tokenBuilder = + new TransferTokenBuilder( + Double.parseDouble(formData.get("amount")), + formData.get("currency")) + .setDescription(formData.get("description")) + .addDestination(destination) + .setToAlias(merchantMember.firstAlias()) + .setToMemberId(merchantMember.memberId()); + + TokenRequest request = TokenRequest.create(tokenBuilder) + .setOption(REDIRECT_URL, "http://localhost:3000/redeem"); + + String requestId = merchantMember.storeTokenRequest(request); + + //generate Token Request URL to redirect to + String tokenRequestUrl = tokenIO.generateTokenRequestUrl(requestId); + //send a 302 Redirect + res.status(302); + res.redirect(tokenRequestUrl); + return null; + }); + + Spark.get("/redeem", (req, res) -> { + String callbackUri = req.raw().getRequestURL().toString() + + "?" + + req.raw().getQueryString(); + String tokenId = tokenIO.parseTokenRequestCallbackUrl(callbackUri).getTokenId(); + //get the token and check its validity Token token = merchantMember.getToken(tokenId); - // Redeem the token at the server, to move the funds - Transfer transfer = merchantMember.redeemToken(token, 4.99, "EUR", "example"); - return ""; + //redeem the token at the server to move the funds + Transfer transfer = merchantMember.redeemToken(token); + res.status(200); + return "Success! Redeemed transfer " + transfer.getId(); }); - // (If user closes browser before this function is called, we don't redeem the token. - // Since this function is where we get the shipping information, we probably don't - // want to redeem the token: we wouldn't know where to ship the goods.) // Serve the web page and JS script: String script = Resources.toString(Resources.getResource("script.js"), UTF_8) @@ -138,10 +172,10 @@ private static Member createMember(TokenIO tokenIO) { // Generate a random username. // If we try to create a member with an already-used name, // it will fail. - String email = "merchant-sample-" + generateNonce().toLowerCase() + "+noverify@example.com"; + String domain = "merchant-sample-" + generateNonce().toLowerCase() + ".com"; Alias alias = Alias.newBuilder() - .setType(EMAIL) - .setValue(email) + .setType(DOMAIN) + .setValue(domain) .build(); return tokenIO.createMember(alias); // The newly-created member is automatically logged in. diff --git a/app/src/main/resources/index.html b/app/src/main/resources/index.html index 1ea912d..1c6a659 100644 --- a/app/src/main/resources/index.html +++ b/app/src/main/resources/index.html @@ -4,7 +4,7 @@ -
+ diff --git a/app/src/main/resources/script.js b/app/src/main/resources/script.js index 448221b..3689011 100644 --- a/app/src/main/resources/script.js +++ b/app/src/main/resources/script.js @@ -1,38 +1,26 @@ -function shippingCb(address, tokenCallback) { - tokenCallback({ // Can return price based on address - shippingMethods: [ - { - id: '0', - name: 'Standard Ground (5-9 business days)', - deliveryTime: '5 - 9 Business Days', - cost: 0 - }, - ], - tax: 0 - }); +function initiatePayment() { + var XHR = new XMLHttpRequest(); + + // Set up our request + XHR.open('POST', 'http://localhost:3000/transfer', true); + + XHR.setRequestHeader("Content-Type", "application/json; charset=utf-8"); + + var data = $.param({ + merchantId: 'Merchant 123', + amount: 4.99, + currency: 'EUR', + description: 'Book Purchase', + destination: 'DE16700222000072880129' + }); + + // Define what happens on successful data submission + XHR.addEventListener("load", function(event) { + window.location.replace(event.target.responseURL); + }); + + // Send the data; HTTP headers are set automatically + XHR.send(data); } -// Initializes the Quick Checkout Button -Token.styleButton({ // Sets up the Quick Checkout button - id: "tokenPayBtn", - label: "Token Quick Checkout" -}).bindPayButton( - { // Terms - alias: { // Merchant alias - type: 'EMAIL', - value: '{alias}' // (filled in by server) - }, - amount: 4.99, // Amount - currency: 'EUR', // Currency - destinations: [{account: {sepa: { iban: "DE16700222000072880129"}}}] - }, - shippingCb, // Shipping callback (null for "virtual" goods) - function(data) { // Success callback - $.post( - 'http://localhost:3000/transfer', - data); - }, - function(error) { // Failure callback - console.log('Something\'s wrong!', error); - } -); +document.getElementById("tokenPayBtn").onclick = initiatePayment; diff --git a/build.gradle b/build.gradle index 9b6c444..44992b7 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ buildscript { allprojects { group = 'io.token.sample' - version = '1.0.1' + version = '1.0.2' project.apply plugin: 'java' project.apply plugin: 'idea' @@ -20,7 +20,7 @@ allprojects { ext { ver = [ sparkjava: '2.6.0', - tokenSdk: '1.0.85', + tokenSdk: '1.0.123', ] }