forked from lucasmro/ClearSale
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ecommerce-order-example.php
146 lines (124 loc) · 3.65 KB
/
ecommerce-order-example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
date_default_timezone_set('America/Sao_Paulo');
require __DIR__.'/../vendor/autoload.php';
use ClearSale\ClearSaleAnalysis;
use ClearSale\Environment\Sandbox;
use ClearSale\XmlEntity\SendOrder\Address;
use ClearSale\XmlEntity\SendOrder\AbstractCustomer;
use ClearSale\XmlEntity\SendOrder\CustomerBillingData;
use ClearSale\XmlEntity\SendOrder\CustomerShippingData;
use ClearSale\XmlEntity\SendOrder\FingerPrint;
use ClearSale\XmlEntity\SendOrder\Item;
use ClearSale\XmlEntity\SendOrder\Order;
use ClearSale\XmlEntity\SendOrder\Payment;
use ClearSale\XmlEntity\SendOrder\Phone;
try {
// Dados da Integração com a ClearSale
$entityCode = '<CLEARSALE_ENTITY_CODE>';
$environment = new Sandbox($entityCode);
// Dados do Pedido
$fingerPrint = new FingerPrint(createSessionId());
$orderId = createOrderId();
$date = new \DateTime();
$email = 'cliente@clearsale.com.br';
$totalItems = 10.0;
$totalOrder = 17.5;
$quantityInstallments = 1;
$ip = '127.0.0.1';
$origin = 'WEB';
$customerBillingData = createCustomerBillingData();
$customerShippingData = createCustomerShippingData();
$item = Item::create(1, 'Adaptador USB', 10.0, 1);
$payment = Payment::create(Payment::BOLETO_BANCARIO, new \DateTime(), 17.5);
// Criar Pedido
$order = Order::createEcommerceOrder(
$fingerPrint,
$orderId,
$date,
$email,
$totalItems,
$totalOrder,
$quantityInstallments,
$ip,
$origin,
$customerBillingData,
$customerShippingData,
$payment,
$item
);
// Enviar pedido para análise
$clearSale = new ClearSaleAnalysis($environment);
$response = $clearSale->analysis($order);
// Resultado da análise
switch ($response)
{
case ClearSaleAnalysis::APROVADO:
// Análise aprovou a cobrança, realizar o pagamento
echo 'Aprovado' . PHP_EOL;
break;
case ClearSaleAnalysis::REPROVADO:
// Análise não aprovou a cobrança
echo 'Reprovado' . PHP_EOL;
break;
case ClearSaleAnalysis::AGUARDANDO_APROVACAO:
// Análise pendente de aprovação manual
echo 'Aguardando aprovação manual' . PHP_EOL;
break;
}
} catch (Exception $e) {
// Erro genérico da análise
echo $e->getMessage();
}
function createOrderId()
{
return sprintf('TEST-%s', createSessionId());
}
function createSessionId()
{
return md5(uniqid(rand(), true));
}
function createCustomerBillingData()
{
$id = '1';
$legalDocument = '63165236372';
$name = 'Fulano da Silva';
$address = createAddress();
$phone = Phone::create(Phone::COMERCIAL, '11', '37288788');
$birthDate = new \DateTime('1980-01-01');
return CustomerBillingData::create(
$id,
AbstractCustomer::TYPE_PESSOA_FISICA,
$legalDocument,
$name,
$address,
$phone,
$birthDate
);
}
function createCustomerShippingData()
{
$id = '1';
$legalDocument = '63165236372';
$name = 'Fulano da Silva';
$address = createAddress();
$phone = Phone::create(Phone::COMERCIAL, '11', '37288788');
return CustomerShippingData::create(
$id,
AbstractCustomer::TYPE_PESSOA_FISICA,
$legalDocument,
$name,
$address,
$phone
);
}
function createAddress()
{
$street = 'Rua José de Oliveira Coutinho';
$number = 151;
$county = 'Barra Funda';
$country = 'Brasil';
$city = 'São Paulo';
$state = 'SP';
$zip = '01144020';
return Address::create($street, $number, $county, $country, $city, $state, $zip);
}