forked from rafam31/pagseguro-classipress
/
pagseguro-classipress-gateway.php
executable file
·186 lines (149 loc) · 5.74 KB
/
pagseguro-classipress-gateway.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
class PagseguroGateway extends APP_Gateway {
public function __construct() {
parent::__construct( 'pagseguro', array(
'dropdown' => __( 'PagSeguro', 'ps_cp' ),
'admin' => __( 'PagSeguro', 'ps_cp' ),
) );
}
/**
* Returns an array representing the form to output for admin configuration
*
* @return array scbForms style form array
*/
public function form() {
$form_values = array(
array(
'title' => __( 'Email', 'ps_cp' ),
'type' => 'text',
'name' => 'user_email',
),
array(
'title' => __( 'Token', 'ps_cp' ),
'type' => 'text',
'name' => 'user_token',
),
array(
'title' => __( 'Usar Sandbox?', 'ps_cp' ),
'type' => 'checkbox',
'name' => 'use_sandbox',
),
array(
'title' => __( 'Sandbox Token', 'ps_cp' ),
'type' => 'text',
'name' => 'user_token_sandbox',
),
);
$return_array = array(
"title" => __( 'General Information', APP_TD ),
"fields" => $form_values
);
return $return_array;
}
/**
* Processes an order payment
*
* @param APP_Order $order The order to be processed
* @param array $options An array of user-entered options
* corresponding to the values provided in form()
*
* @return void
*/
public function process( $order, $options ) {
$url = $options[ 'use_sandbox' ] ? 'https://ws.sandbox.pagseguro.uol.com.br/v2/checkout' : 'https://ws.pagseguro.uol.com.br/v2/checkout';
$description = 'Compra no site ' . get_bloginfo('url');
$payment_request[ 'email' ] = $options[ 'user_email' ];
$payment_request[ 'token' ] = $options[ 'use_sandbox' ] ? $options[ 'user_token_sandbox' ] : $options[ 'user_token' ];
$payment_request[ 'currency' ] = 'BRL';
$payment_request[ 'itemId1' ] = $order->get_id();
$payment_request[ 'itemDescription1' ] = $description;
$payment_request[ 'itemAmount1' ] = $order->get_total();
$payment_request[ 'itemQuantity1' ] = 1;
$payment_request[ 'redirectURL' ] = get_bloginfo( 'url' );
$payment_request[ 'notificationURL' ] = get_bloginfo( 'url' );
$payment_request[ 'reference' ] = $order->get_id();
$buyer = get_userdata( $order->get_author() );
$payment_request[ 'senderEmail' ] = $buyer->user_email;
$payment_request = http_build_query( $payment_request );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payment_request );
$xml = curl_exec( $ch );
curl_close( $ch );
if ( $xml == 'Unauthorized' ) {
echo 'Houve um erro ao comunicar com o servidor do PagSeguro <br>';
pagseguro_log( 'status => UNAUTHORIZED' );
exit;
}
$xml = simplexml_load_string( $xml );
if ( count( $xml->error ) > 0 ) {
echo 'Falha na requisição.<br>';
foreach ( $xml->error as $error ) {
echo $error . '<br>';
}
exit;
}
$payment_url = $options[ 'use_sandbox' ] ? 'https://sandbox.pagseguro.uol.com.br/v2/checkout/payment.html' : 'https://pagseguro.uol.com.br/v2/checkout/payment.html';
echo '<p>Total do pedido: ' . $order->get_total() . '</p>';
echo '<a class="obtn btn_orange" href=' . $payment_url . '?code='.$xml->code.'>Concluir pagamento agora</a>';
}
}
appthemes_register_gateway( 'PagseguroGateway' );
add_action( 'wp_footer', 'pagseguro_create_payment_listener' );
function pagseguro_create_payment_listener() {
$code = ( isset( $_POST[ 'notificationCode' ] ) && trim( $_POST[ 'notificationCode' ] ) !== "" ?
trim( $_POST[ 'notificationCode' ] ) : null );
$type = ( isset( $_POST[ 'notificationType' ] ) && trim( $_POST[ 'notificationType' ] ) !== "" ?
trim( $_POST[ 'notificationType' ] ) : null );
if ( $code && $type ) {
pagseguro_log( '::::: Notificação PagSeguro recebida :::::' );
$options = APP_Gateway_Registry::get_gateway_options( 'pagseguro' );
$email = $options[ 'user_email' ];
$token = $options[ 'use_sandbox' ] ? $options[ 'user_token_sandbox' ] : $options[ 'user_token' ];
$url = $options[ 'use_sandbox' ] ? 'https://ws.sandbox.pagseguro.uol.com.br/v2/transactions/notifications/' : 'https://ws.pagseguro.uol.com.br/v2/transactions/notifications/';
$url = $url . $code . '?email=' . $email . '&token=' . $token;
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$transaction = curl_exec( $ch );
if ( $transaction == 'Unauthorized' ) {
pagseguro_log( 'Transação não autorizada. Token: ' . $token );
exit;
}
curl_close( $ch );
$transaction = simplexml_load_string( $transaction );
$status = $transaction->status;
$order = APP_Order_Factory::retrieve( intval( $transaction->reference ) );
if ( false === $order ) {
pagseguro_log( 'ERRO: Não foi encontrado pedido com ID_TRANSACAO == ' . $transaction->reference );
return;
}
switch ( $status ) {
case 3:
if ( $order->get_status() == 'tr_activated' ) {
pagseguro_log( 'Notificação repetida para ' . $transaction->reference . '. Ignorando...' );
return;
}
$order->activate();
pagseguro_log( 'Pedido ' . $transaction->reference . ' foi ativado');
break;
case 7:
if ( $order->get_status() == 'tr_failed' ) {
pagseguro_log( 'Notificação repetida para ' . $transaction->reference . '. Ignorando...' );
}
$order->failed();
pagseguro_log( 'Pedido ' . $transaction->reference . ' foi cancelado');
}
}
}
/**
* Writes to a log file.
*
* @param $message
*/
function pagseguro_log( $message ) {
file_put_contents( 'payment.log', date( "d-m-Y H:i:s" ) . ' ::::: ' . $message . PHP_EOL, FILE_APPEND );
}