helloworld.php - How to run PHP Sample Code using the Hello World API and X-Pay-Token

Highlighted
Visa Employee

helloworld.php - How to run PHP Sample Code using the Hello World API and X-Pay-Token

In this "How-to" guide we will show you how to run the “Hello World” project using Visa Hello World API and and API Key - Shared Secret Authentication. The Hello World API is a simple API for testing the connectivity with the Visa Network.

 

Important Links:

 

 

How to Create a Visa Developer Project

 

Before you are able to run the “Hello World” Project, you must create a Visa Developer Portal (VDP) project and get credentials. If you haven't registered yet just click on register here, fill out the account information, agree to the terms and conditions and click on receive emails. Once you have successfully activated your account, you will see your dashboard and you are ready to go.

 

 

java1.png

 

 

Once you are there, click on create your first project if this is your first project.  On the next page, you will be asked for details, such as project name, description and a list of APIs to choose from.

 

 

Java2.png

 

For this tutorial, we're going to select CyberSource Payments and click create project.

 

 

CyberSourcePaymentMethod.png

 

 

 

How to get Credentials

 

After creating your project, you will be redirected to the project summary page. You can obtain your project credentials by browsing the left side navigation menu of your project and click on “Credentials”.

 

 

Java4.png

 

What is required for the X-Pay-Token Authentication?

 

To be able to make an API call with X-Pay-Token Authentication, you need to have the following:

  1. Api Key
  2. Secret Key

CredentialsForXPayToken.png

 

How to run the PHP Sample code of the “Hello World API” using PhpStorm

 

 

Next, we'll show you how to run the PHP sample code of the “Hello World API” using PhpStorm. PhpStorm is an Integrated Development Environment for PHP developers built on top of the IntelliJ IDEA platform, and can be downloaded using below link:

 

https://www.jetbrains.com/idea/download/

 

In PhpStorm IDEA, a project helps you organize your source code, tests, libraries that you use, build instructions, and your personal settings in a single unit.

 

 

Step 1 - Launch PhpStorm

 

  • On the PhpStorm Welcome Screen, click Create New Project.

2020-11-15_15-01-11.png

 

 

Otherwise, from the main menu, select File | New | Project

 

Step 2 - In the New Project wizard, select Php Empty Project from the list on the left, provide the project Location and click Create.

 

2020-11-15_15-01-38.png

 

 

Step 3 - Create a new PHP file and named it “helloworld.php

 

2020-11-15_15-03-05.png

 

 

 

 

Step 4 - Copy the below sample code to the “helloworld.php” file

 

  • Set the below parameters:

 

$apiKey = '<YOUR API KEY>';

$sharedSecret = '<YOUR SHARED SECRET>';

 

 

 

<?php
/**
 * (c) Copyright 2018 - 2020 Visa. All Rights Reserved.**
 *
 * NOTICE: The software and accompanying information and documentation (together, the “Software”) remain the property of and are proprietary to Visa and its suppliers and affiliates. The Software remains protected by intellectual property rights and may be covered by U.S. and foreign patents or patent applications. The Software is licensed and not sold.*
 *
 *  By accessing the Software you are agreeing to Visa's terms of use (developer.visa.com/terms) and privacy policy (developer.visa.com/privacy).In addition, all permissible uses of the Software must be in support of Visa products, programs and services provided through the Visa Developer Program (VDP) platform only (developer.visa.com). **THE SOFTWARE AND ANY ASSOCIATED INFORMATION OR DOCUMENTATION IS PROVIDED ON AN “AS IS,” “AS AVAILABLE,” “WITH ALL FAULTS” BASIS WITHOUT WARRANTY OR  CONDITION OF ANY KIND. YOUR USE IS AT YOUR OWN RISK.** All brand names are the property of their respective owners, used for identification purposes only, and do not imply product endorsement or affiliation with Visa. Any links to third party sites are for your information only and equally  do not constitute a Visa endorsement. Visa has no insight into and control over third party content and code and disclaims all liability for any such components, including continued availability and functionality. Benefits depend on implementation details and business factors and coding steps shown are exemplary only and do not reflect all necessary elements for the described capabilities. Capabilities and features are subject to Visa’s terms and conditions and may require development,implementation and resources by you based on your business and operational details. Please refer to the specific API documentation for details on the requirements, eligibility and geographic availability.*
 *
 * This Software includes programs, concepts and details under continuing development by Visa. Any Visa features,functionality, implementation, branding, and schedules may be amended, updated or canceled at Visa’s discretion.The timing of widespread availability of programs and functionality is also subject to a number of factors outside Visa’s control,including but not limited to deployment of necessary infrastructure by issuers, acquirers, merchants and mobile device manufacturers.*
 *
 */

echo '<br/>START Sample Code for Two-Way (Mutual) SSL<br/>';
$url = 'https://sandbox.api.visa.com/vdp/helloworld';

// THIS IS EXAMPLE ONLY how will apiKey and sharedSecret look like
// apiKey = "1WM2TT4IHPXC8DQ5I3CH21n1rEBGK-Eyv_oLdzE2VZpDqRn_U";
// sharedSecret = "19JRVdej9";
$apiKey = '<YOUR API KEY>';
$sharedSecret = '<YOUR SHARED SECRET>';

$resource_path = 'helloworld';
$query_string = 'apiKey=' . $apiKey;
$body = '';

$curl = curl_init();

curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);

$time = time();
$pre_hash_string = $time . $resource_path . $query_string . $body;
$hashtoken = "xv2:" . $time . ":" . hash_hmac('sha256', $pre_hash_string, $sharedSecret);

$authHeader = "x-pay-token: " . $hashtoken;
$header = (array("Accept: application/json", "Content-Type: application/json", $authHeader));
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

$url = $url . '?' . $query_string;

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Make the request
$response = curl_exec($curl);

$response_info = curl_getinfo($curl);

if ($response_info['http_code'] === 0) {
    $curl_error_message = curl_error($curl);

    // curl_exec can sometimes fail but still return a blank message from curl_error().
    if (!empty($curl_error_message)) {
        $error_message = "API call to $url failed: $curl_error_message";
    } else {
        $error_message = "API call to $url failed, but for an unknown reason. " .
            "This could happen if you are disconnected from the network.";
    }
    echo $error_message;
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
    echo "Your call returned with a success code - " . $response_info['http_code'] . "<br/>";
    echo "<br/>Json Response: $response";
} else {
    echo "<br/>[HTTP Status: " . $response_info['http_code'] . "]\n\n[" . $response . "]<br/>";
    echo "<br/>Error connecting to the API ($url)<br/>";
}
echo '<br/>END Sample Code for Two-Way (Mutual) SSL<br/>';

 

 

 

 

 

Step 5 - Compile Your Code 

 

  • Simply right click and run “helloworld.php (PHP Script)"

2020-11-15_15-04-16.png

 

 

 

  • If you are prompt with the “Edit Configuration Screen”,  click on the “Fix” button to configure your PHP Interpreter

 

2020-11-15_15-04-45.png

 

Once the above step is completed, right click and click Run helloworld.php (PHP Script)

 

 

Want more? Join the Visa Developer Community to get alerts on the latest tutorials, guides and new developer resources. Stay tuned for more in the series. 

 

Written by: @shameem@jmedlen, & @mprsic