payment - Square PHP API - Record Data -
i new @ payment api , understanding of arrays().... using squares php api script expample... it, however, when processes payment, want catch data , record sql (mysqli)... can't make script echo specific data want... lost... me in right direction?
here form: (index.php)
<?php require '../composer/vendor/autoload.php'; # replace these values. want start sandbox credentials # start: https://docs.connect.squareup.com/articles/using-sandbox/ # access token use in connect api requests. use *sandbox* access # token if you're testing things out. $access_token = 'sandbox-xxxxxx'; # helps ensure code has been reached via form submission if ($_server['request_method'] != 'post') { error_log("received non-post request"); echo "request not allowed"; http_response_code(405); return; } # fail if card form didn't send value `nonce` server $nonce = $_post['nonce']; if (is_null($nonce)) { echo "invalid card data"; http_response_code(422); return; } \squareconnect\configuration::getdefaultconfiguration()->setaccesstoken($access_token); $locations_api = new \squareconnect\api\locationsapi(); try { $locations = $locations_api->listlocations(); #we location can process payments $location = current(array_filter($locations->getlocations(), function($location) { $capabilities = $location->getcapabilities(); return is_array($capabilities) && in_array('credit_card_processing', $capabilities); })); } catch (\squareconnect\apiexception $e) { echo "caught exception!<br/>"; print_r("<strong>response body:</strong><br/>"); echo "<pre>"; var_dump($e->getresponsebody()); echo "</pre>"; echo "<br/><strong>response headers:</strong><br/>"; echo "<pre>"; var_dump($e->getresponseheaders()); echo "</pre>"; exit(1); } $transactions_api = new \squareconnect\api\transactionsapi(); $request_body = array ( "card_nonce" => $nonce, # monetary amounts specified in smallest unit of applicable currency. # amount in cents. it's hard-coded $1.00, isn't useful. "amount_money" => array ( "amount" => 100, "currency" => "usd" ), # every payment process sdk must have unique idempotency key. # if you're unsure whether particular payment succeeded, can reattempt # same idempotency key without worrying double charging # buyer. "idempotency_key" => uniqid() ); # sdk throws exception if connect endpoint responds besides # 200-level http code. block catches exceptions occur request. try { $result = $transactions_api->charge($location->getid(), $request_body); echo "<pre>"; echo "card has been approved!"; echo $result['amount'][0]; echo $result['transaction_id'][1]; echo "</pre>"; } catch (\squareconnect\apiexception $e) { echo "caught exception!<br/>"; print_r("<strong>response body:</strong><br/>"); echo "<pre>"; var_dump($e->getresponsebody()); echo "</pre>"; echo "<br/><strong>response headers:</strong><br/>"; echo "<pre>"; var_dump($e->getresponseheaders()); echo "</pre>"; }
and here processor (process-card.php)
<?php require '../composer/vendor/autoload.php'; # replace these values. want start sandbox credentials # start: https://docs.connect.squareup.com/articles/using-sandbox/ # access token use in connect api requests. use *sandbox* access # token if you're testing things out. $access_token = 'sandbox-xxxxxx'; # helps ensure code has been reached via form submission if ($_server['request_method'] != 'post') { error_log("received non-post request"); echo "request not allowed"; http_response_code(405); return; } # fail if card form didn't send value `nonce` server $nonce = $_post['nonce']; if (is_null($nonce)) { echo "invalid card data"; http_response_code(422); return; } \squareconnect\configuration::getdefaultconfiguration()->setaccesstoken($access_token); $locations_api = new \squareconnect\api\locationsapi(); try { $locations = $locations_api->listlocations(); #we location can process payments $location = current(array_filter($locations->getlocations(), function($location) { $capabilities = $location->getcapabilities(); return is_array($capabilities) && in_array('credit_card_processing', $capabilities); })); } catch (\squareconnect\apiexception $e) { echo "caught exception!<br/>"; print_r("<strong>response body:</strong><br/>"); echo "<pre>"; var_dump($e->getresponsebody()); echo "</pre>"; echo "<br/><strong>response headers:</strong><br/>"; echo "<pre>"; var_dump($e->getresponseheaders()); echo "</pre>"; exit(1); } $transactions_api = new \squareconnect\api\transactionsapi(); $request_body = array ( "card_nonce" => $nonce, # monetary amounts specified in smallest unit of applicable currency. # amount in cents. it's hard-coded $1.00, isn't useful. "amount_money" => array ( "amount" => 100, "currency" => "usd" ), # every payment process sdk must have unique idempotency key. # if you're unsure whether particular payment succeeded, can reattempt # same idempotency key without worrying double charging # buyer. "idempotency_key" => uniqid() ); # sdk throws exception if connect endpoint responds besides # 200-level http code. block catches exceptions occur request. try { $result = $transactions_api->charge($location->getid(), $request_body); echo "<pre>"; echo "card has been approved!"; echo $result['amount'][0]; echo $result['transaction_id'][1]; echo "</pre>"; } catch (\squareconnect\apiexception $e) { echo "caught exception!<br/>"; print_r("<strong>response body:</strong><br/>"); echo "<pre>"; var_dump($e->getresponsebody()); echo "</pre>"; echo "<br/><strong>response headers:</strong><br/>"; echo "<pre>"; var_dump($e->getresponseheaders()); echo "</pre>"; }
example: want write transaction data "transactions" table member's id number , transaction number , if approved or declined.
thank you!!!! hope attempting isn't complicated.
try using var_dump()
instead of echo if having problems displaying things echo
, , might learn more info object using.
instead of $result['amount'][0]
try $result->gettransaction()-> gettenders()[0]->getamountmoney()->getamount()
aren't seeing echo'd because trying print things don't exist.
don't forget check out documentation well:https://github.com/square/connect-php-sdk/blob/master/docs/model/chargeresponse.md
Comments
Post a Comment