GlisWeb framework
_amazonpay.starter.php
Vai alla documentazione di questo file.
1 <?php
2 
3  // namespace
4 #1 namespace Sample;
5 
6  // emulazione acquisto
7  if( isset( $_REQUEST['productId'] ) && ! empty( $_REQUEST['productId'] ) ) {
8  $_REQUEST['__ecommerce__']['__carrello__']['__articoli__']['00000001']['quantita'] = 1;
9  $_REQUEST['__ecommerce__']['__carrello__']['__articoli__']['00000001']['id_iva'] = 1;
10  $_REQUEST['__ecommerce__']['__carrello__']['__articoli__']['00000001']['id_modalita_spedizione'] = 1;
11  }
12 
13  // inclusione del framework
14  require '../../../../_src/_config.php';
15 
16 #1 use PayPalCheckoutSdk\Core\PayPalHttpClient;
17 #1 use PayPalCheckoutSdk\Core\SandboxEnvironment;
18 
19  ini_set('error_reporting', E_ALL);
20  ini_set('display_errors', '1');
21  ini_set('display_startup_errors', '1');
22 
23 # $h = fopen( 'order.log', 'w+' );
24 # fwrite( $h, '=== INIZIO LOG ORDINE ===' . PHP_EOL );
25 # fwrite( $h, file_get_contents('php://input') . PHP_EOL );
26 # fwrite( $h, print_r( json_decode( file_get_contents('php://input'), true ), true ) );
27 
28  $dati = json_decode( file_get_contents('php://input'), true );
29  if( is_array( $dati ) ) { $_REQUEST = array_replace_recursive( $_REQUEST, $dati ); }
30 
31  $_REQUEST['debug']['SDK_VERSION'] = Amazon\Pay\API\Client::SDK_VERSION;
32 
33  // riordino della $_REQUEST
34  rksort( $_REQUEST );
35 
36  // provider (a beneficio delle controller)
37  $provider = 'amazonpay';
38 
39  // nome del file di ricevuta
40  $fileRicevuta = 'var/log/payments/amazonpay/amazonpay.starter.' . time() . '.log';
41 
42  // registro la comunicazione
43  appendToFile( print_r( $_REQUEST, true ), $fileRicevuta );
44 
45  // inizializzazione
47  'public_key_id' => $cf['ecommerce']['provider']['amazonpay']['profile']['publicKey'],
48  'private_key' => DIRECTORY_BASE . 'etc/amazon/'.$cf['ecommerce']['provider']['amazonpay']['profile']['publicKey'].'.pem',
49  'sandbox' => $cf['ecommerce']['provider']['amazonpay']['profile']['sandbox'],
50  'region' => 'EU'
51  );
52 
53  // contesto
54  $payload = array(
55  'storeId' => $cf['ecommerce']['provider']['amazonpay']['profile']['storeId'],
56  'webCheckoutDetails' => array(
57  'checkoutReviewReturnUrl' => $cf['contents']['pages']['carrello']['url'][ $cf['localization']['language']['ietf'] ],
58  'checkoutResultReturnUrl' => $cf['site']['url'] . $cf['ecommerce']['provider']['amazonpay']['profile']['listener']
59  ),
60  'addressRestrictions' => array(
61  'type' => 'Allowed',
62  'restrictions' => array(
63  'IT' => array()
64  )
65  )
66  );
67  $headers = array('x-amz-pay-Idempotency-Key' => uniqid());
68 
69 # die( print_r( $payload, true ) );
70 
71  // registro la comunicazione
72  appendToFile( print_r( $payload, true ), $fileRicevuta );
73  appendToFile( print_r( $headers, true ), $fileRicevuta );
74 
75  // chiamata
76  try {
77  $client = new Amazon\Pay\API\Client($amazonpay_config);
78  $result = $client->createCheckoutSession($payload, $headers);
79  if ($result['status'] === 201) {
80  // created
81  $response = json_decode($result['response'], true);
82  $checkoutSessionId = $response['checkoutSessionId'];
83 # echo "checkoutSessionId=$checkoutSessionId\n";
84  buildJson( array( 'checkoutSessionId' => $checkoutSessionId ) );
85  mysqlQuery( $cf['mysql']['connection'], 'UPDATE carrelli SET session_checkout = ? WHERE id = ?', array( array( 's' => $checkoutSessionId ), array( 's' => $_SESSION['carrello']['id'] ) ) );
86  } else {
87  // check the error
88 # echo 'status=' . $result['status'] . '; response=' . $result['response'] . "\n";
89  buildJson( array( 'status' => $result['status'], 'response' => $result['response'] ) );
90  }
91  } catch (\Exception $e) {
92  // handle the exception
93  echo $e . "\n";
94  }
95 
96  // registro la comunicazione
97  appendToFile( print_r( $result, true ), $fileRicevuta );
98 
99 /*1
100  // identificativo del carrello
101  if( isset( $_REQUEST['orderID'] ) ) {
102  $id = $_REQUEST['orderID'];
103  } else {
104  $id = 0;
105  }
106 
107 # fwrite( $h, print_r( $_REQUEST, true ) );
108 
109  class PayPalClient {
110 
111  public static function client() {
112  return new PayPalHttpClient(self::environment());
113  }
114 
115  public static function environment() {
116 
117  global $cf;
118  $clientId = getenv("CLIENT_ID") ?: $cf['ecommerce']['provider']['paypal']['profile']['client'];
119  $clientSecret = getenv("CLIENT_SECRET") ?: $cf['ecommerce']['provider']['paypal']['profile']['secret'];
120  if( $cf['site']['status'] === PRODUCTION ) {
121  return new lIVEEnvironment($clientId, $clientSecret);
122  } else {
123  return new SandboxEnvironment($clientId, $clientSecret);
124  }
125 
126  }
127 
128  }
129 
130  use Sample\PayPalClient;
131  use PayPalCheckoutSdk\Orders\OrdersGetRequest;
132 
133  class GetOrder {
134 
135  public static function getOrder($orderId) {
136 
137  global $fileRicevuta;
138 
139  $client = PayPalClient::client();
140  $response = $client->execute(new OrdersGetRequest($orderId));
141 
142 # print json_encode($response->result);
143 
144  print "Status Code: {$response->statusCode}\n";
145  print "Status: {$response->result->status}\n";
146  print "Order ID: {$response->result->id}\n";
147  print "Intent: {$response->result->intent}\n";
148  print "Links:\n";
149 
150  foreach($response->result->links as $link) {
151  print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
152  }
153 
154  // TODO Save the transaction in your database. Implement logic to save transaction to your database for future reference.
155 # print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
156 
157 # echo json_encode($response->result, JSON_PRETTY_PRINT);
158 
159 # fwrite( $h, print_r( (array) $response, true ) );
160  appendToFile( print_r( json_decode( json_encode( $response ), true ), true ), $fileRicevuta );
161 
162  }
163 
164  }
165 
166  if (!count(debug_backtrace())) {
167  GetOrder::getOrder( $_REQUEST['orderID'], true);
168  }
169 1*/
170 
171 ?>
appendToFile( $t, $f, &$e=NULL)
aggiunge una stringa a un file
$amazonpay_config
rksort(&$array)
mysqlQuery( $c, $q, $p=false, &$e=array())
$cf['ricerca']['template']
Definition: _030.common.php:7
if(is_array( $dati)) $_REQUEST['debug']['SDK_VERSION']
$e
Definition: _slack.php:121
const DIRECTORY_BASE
Definition: _osm.php:3
if(isset( $_REQUEST['amazonCheckoutSessionId'])) $_SESSION['carrello']
buildJson( $content, $encoding=ENCODING_UTF8, $headers=array())