PayPal Adaptive Payments API: Test Call With AFNetworking 3.0 (Objective-C)
I’m working with Paypal’s Adaptive Payments API. The ultimate goal is enabling peer-to-peer payments in an iOS app.
They walk you through making a test call in their guide, explaining every value you’re sending in the POST request and what it represents. When testing with money transfer, you need a safe and reliable testing environment. They have a testing endpoint, testing accounts, and even a sandbox that you can use after your first successful test call.
Here’s the block of code they tell you to copy and paste. There are http headers and a JSON payload. They’re using cURL (Client URL Library), a library that can make HTTP requests:
curl https://svcs.sandbox.paypal.com/AdaptivePayments/Pay \
-H "X-PAYPAL-SECURITY-USERID: caller_1312486258_biz_api1.gmail.com" \
-H "X-PAYPAL-SECURITY-PASSWORD: 1312486294" \
-H "X-PAYPAL-SECURITY-SIGNATURE: AbtI7HV1xB428VygBUcIhARzxch4AL65.T18CTeylixNNxDZUu0iO87e" \
-H "X-PAYPAL-REQUEST-DATA-FORMAT: JSON" \
-H "X-PAYPAL-RESPONSE-DATA-FORMAT: JSON" \
-H "X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T" \
I tried making the call with this code in Postman, a Chrome plug-in for making API calls outside of an editor. For some reason, I wasn’t getting a successful response, which was weird, since it’s, well, copy and paste. I called them and ended up on the phone with someone on their integrations team, who was super helpful and interested. He’d never heard of Postman and thought that maybe that’s where the call went wrong. After checking that I wasn’t making an obvious mistake he asked for some ID numbers and error codes that I got in the response. Then, he suggested I make the call in my program and run it.
Turns out he was right. The username in the header, supposed to be “caller_1312486258_biz_api1.gmail.com” was coming through as blank.
I ended up taking his advice and running the test in XCode. I’ve never made a call with headers before, so I was pumped when I figured it out. And yup, you guessed it, success. AFNetworking has methods for setting HTTP headers, and will convert the payload into JSON from an NSDictionary object. Here’s the code: