1 <?php
2 /**
3 * Pagarme Create Customer Request
4 */
5
6 namespace Omnipay\Pagarme\Message;
7
8 /**
9 * Pagarme Create Customer Request
10 *
11 * Customer objects allow you to perform recurring charges and
12 * track multiple charges that are associated with the same customer.
13 * The API allows you to create, delete, and update your customers.
14 * You can retrieve individual customers as well as a list of all of
15 * your customers.
16 *
17 * Harnessing the Omnipay's CreditCard model, we can use the
18 * attributes listed below to create new customers. So it must
19 * pass the parameters for the card attribute or create a CreditCard
20 * Object (see the code example below). Alternatively you can pass
21 * the data to the customer attribute.
22 *
23 * * firstName
24 * * lastName
25 * * address1 (must be in the format "street, street_number and complementary")
26 * * address2 (used to specify the parameter "address_neighborhood")
27 * * city
28 * * postcode
29 * * state
30 * * country
31 * * phone (must be in the format "DDD PhoneNumber" e.g. "19 98888 5555")
32 * * email
33 * * birthday
34 * * gender
35 *
36 *
37 * Provided card's attributes will be ignored by Pagarme API.
38 *
39 * Either a pair name|email or document_number (valid CPF or CNPJ) is required.
40 *
41 *
42 *
43 * Example:
44 *
45 * <code>
46 * // Create a gateway for the Pagarme Gateway
47 * // (routes to GatewayFactory::create)
48 * $gateway = Omnipay::create('Pagarme');
49 *
50 * // Initialise the gateway
51 * $gateway->initialize(array(
52 * 'apiKey' => 'MyApiKey',
53 * ));
54 *
55 * // Create a credit card object or set the card
56 * // attribute
57 * // This card can be used for testing.
58 * $customer = array(
59 * 'firstName' => 'Example',
60 * 'lastName' => 'Customer',
61 * 'email' => 'customer@example.com',
62 * 'address1' => 'Street name, Street number, Complementary',
63 * 'address2' => 'Neighborhood',
64 * 'postcode' => '05443100',
65 * 'phone' => '19 3242 8855',
66 * 'holder_document_number' => '21437814860',
67 * );
68 *
69 * // Do a create customer transaction on the gateway
70 * $response = $gateway->createCustomer(array(
71 * 'customer' => $customer,
72 * ))->send();
73 *
74 * if ($response->isSuccessful()) {
75 * echo "Gateway createCustomer was successful.\n";
76 * // Find the customer ID
77 * $customer_id = $response->getCustomerReference();
78 * echo "Customer ID = " . $customer_id . "\n";
79 * // Find the card ID
80 * $card_id = $response->getCardReference();
81 * echo "Card ID = " . $card_id . "\n";
82 * }
83 * </code>
84 *
85 * @link https://docs.pagar.me/api/?shell#criando-um-cliente
86 */
87 class CreateCustomerRequest extends AbstractRequest
88 {
89 public function getData()
90 {
91 $data = array();
92 //var_dump($this->getCustomer());
93 //die;
94 if ( $this->getCustomer() ) {
95 $customerArray = $this->getCustomer();
96 $this->setCard($customerArray);
97 }
98
99 if ( $this->getCard() ) {
100 $customer = $this->getCustomerData();
101 $data = $customer['customer'];
102 if ( isset($data['address']) ) {
103 $data['address']['city'] = $this->getCard()->getCity();
104 $data['address']['state'] = $this->getCard()->getState();
105 $data['address']['country'] = $this->getCard()->getCountry();
106 }
107 }
108
109 // Validate Required Attributes
110 if ( ! isset($data['document_number']) ) {
111 if ( ! (isset($data['email']) && (isset($data['name']))) ) {
112 $this->validate('document_number');
113 }
114 }
115
116 return $data;
117 }
118
119 public function getEndpoint()
120 {
121 return $this->endpoint . 'customers';
122 }
123 }
124