7

Creating a Customer in PHP

 2 years ago
source link: https://developer.squareup.com/blog/creating-a-customer-in-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
October 20th, 2017 | 1 minute read

Creating a Customer in PHP

Using Square's Customers API using PHP

Twitter
Facebook
Reddit
LinkedIn

You can’t have a business if you don’t have any customers, but importing customers from an existing system can be a pain, and sometimes you need to be able to do so programmatically. In PHP, our PHP SDK makes it fairly easy to create these customers using one simple script:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken('XXXX');


$customers_api = new SquareConnect\Api\CustomersApi();
$customer = new \SquareConnect\Model\CreateCustomerRequest();

$customer->setGivenName('Stacy');
$customer->setFamilyName('Smith');
$customer->setEmailAddress('[email protected]');
$customer->setAddress(
    array(
        'address_line_1'=>'123 Main St.',
        'locality'=>'Anywhere',
        'country'=>'US'
    )
);

try {
    $result = $customers_api->createCustomer($customer);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling CustomersApi->createCustomer: ', $e->getMessage(), PHP_EOL;
}
?>

The code starts out by loading the PHP SDK that was installed with Composer and then setting the access token for the account. Then you can create a new instance of the CustomersApi and a new object that will hold all of the customer information that you are trying to upload. Finally, you call the createCustomer() method with your customer object and voilà, you have a brand new customer in your account.

I hope you liked this post, let me know what you’d like the next one to be about by reaching out to @SquareDev on twitter.

Picture of Tristan Sokol

By Tristan Sokol
Medium


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK