Just got paid? Your Arduino Yún can let you know.


We'll show you how to get your Arduino Yún to retrieve the latest payment from your PayPal Account. You can use this as a first step towards building a system that alerts you when people send you money.

This sketch uses our PayPal > Payments > ListPayments Choreo.

Get Set Up

1Make sure you have a Temboo account. If you don't already have one, you can register for free.

2You'll also need to register a new 'Web' application using the PayPal developer dashboard. After creating the app, you'll find the Client ID and Secret that you need to run the sketch in the REST API Credentials section of your app details panel, as shown in the screenshot below.

PayPal Developer Dashboard

PayPal app credentials in the PayPal Developer dashboard

3Make sure you have some payment data in your PayPal account. If you haven't received any money yet, you can generate some test payments with our PayPal > Payments > AcceptPayPalPayment Choreo.

You can use any URLs you want for the URL inputs e.g., http://test.com, and remember to set the optional UseSandbox input to 1, specifying that you're using a test PayPal app.

PayPal Inputs

Specifying that your PayPal app is using their sandbox API endpoints

4Make sure that you have the latest version of the Arduino IDE. You should also check that you have the newest version of the Temboo Library by checking the Arduino Library Manager

5Make sure that your Yún is connected to the Internet. Arduino has a helpful guide if you need assistance.

6When you open the IDE, check to make sure you have the right board selected by going to Tools > Boards > Arduino Yún.

Write the Sketch

Copy the code below into a new sketch in your Arduino IDE. Replace the PayPal Client ID and Client Secret placeholder values with your own app details.

/*
  GetLatestPayPalPayment

  Demonstrates retrieving the latest payment to a PayPal account. 
  
  This example code is in the public domain.
*/
#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" //contains Temboo account information 

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.

// your PayPal app's Client ID, available from developer.paypal.com
const String PAYPAL_CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// your PayPal app's Client Secret, available from developer.paypal.com
const String PAYPAL_CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// a value to indicate whether you're using the PayPal API sandbox or not: 1 = yes, 0 = no.
const String USE_SANDBOX = "1";

int numRuns = 1;   // execution count, so this doesn't run forever
int maxRuns = 10;   // maximum number of times the Choreo should be executed

void setup() {
  Serial.begin(9600);
  
  // For debugging, wait until a serial console is connected.
  delay(4000);
  while(!Serial);
  Bridge.begin();
}
void loop()
{
  if (numRuns <= maxRuns) {
    Serial.println("Running ListPayments - Run #" + String(numRuns++) + "\n");
    
    TembooChoreo ListPaymentsChoreo;

    // invoke the Temboo client
    ListPaymentsChoreo.begin();
    
    // set Temboo account credentials
    ListPaymentsChoreo.setAccountName(TEMBOO_ACCOUNT);
    ListPaymentsChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    ListPaymentsChoreo.setAppKey(TEMBOO_APP_KEY);
    
    // identify choreo to run
    ListPaymentsChoreo.setChoreo("/Library/PayPal/Payments/ListPayments");
    
    // set choreo inputs
    ListPaymentsChoreo.addInput("ClientID", PAYPAL_CLIENT_ID);
    ListPaymentsChoreo.addInput("ClientSecret", PAYPAL_CLIENT_SECRET);
    ListPaymentsChoreo.addInput("UseSandbox", USE_SANDBOX);

    
    // set choreo output filters
    ListPaymentsChoreo.addOutputFilter("time", "/payments/create_time", "Response");
    ListPaymentsChoreo.addOutputFilter("amount", "/payments/transactions/amount/total", "Response");
    ListPaymentsChoreo.addOutputFilter("id", "/payments/id", "Response");
    
    // run choreo
    ListPaymentsChoreo.run();
    
    // parse the results and print them to the serial monitor
    while(ListPaymentsChoreo.available()) {
        // read the name of the next output item
        String name = ListPaymentsChoreo.readStringUntil('\x1F');
        name.trim(); // use “trim” to get rid of newlines

        // read the value of the next output item
        String data = ListPaymentsChoreo.readStringUntil('\x1E');
        data.trim(); // use “trim” to get rid of newlines

         if (name == "time") {
             Serial.println("Received: " + data);
         } else if (name == "amount") {
             Serial.println("Amount: $" + data);
         } else if (name == "id") {
             Serial.println("Payment ID: " + data);
        }
    }
    ListPaymentsChoreo.close();

  }

  Serial.println("\nWaiting...\n");
  delay(30000); // wait 30 seconds between ListPayments calls
}

Create Your Header File

The sketch above references the TembooAccount.h header file, which contains your Temboo account information.

If you are currently logged in, you'll see your account details in the code snippet below (otherwise you'll see placeholder values). Create a new tab in the Arduino IDE and call it TembooAccount.h. Copy the code below into your new TembooAccount.h tab and then save your sketch.

#define TEMBOO_ACCOUNT "ACCOUNT_NAME"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "APP_NAME"  // your Temboo app key name
#define TEMBOO_APP_KEY  "APP_KEY"  // your Temboo app key

With both files in place you are ready to upload the sketch and watch your Yún fetch the latest payment to your PayPal account and display it on the serial monitor, cha-ching!

Convert the sketch to work with the Yún Shield

If you're working with the Yún Shield paired with another Arduino board, you'll need to make some small changes to the Yún sketch above it so that it's compatible with the Arduino Yún Shield.

1First, change the include statement #include <Temboo.h> to #include <TembooYunShield.h>. Your include statements should look like this:

#include <Bridge.h>
#include <TembooYunShield.h>
#include "TembooAccount.h" 

2Next, change the Temboo object name from TembooChoreo to TembooYunShieldChoreo. Your code should look like this:

TembooYunShieldChoreo  ListPaymentsChoreo;

3Now your code is ready to run on your Yún Shield!

What's Next?

Now that your finances are in order, why not check out the other 2000+ Choreos in our Library and start thinking about all the possibilities for your next Yún project.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back