Skip to content

Commit

Permalink
Upgrade from transactions API (deprecated) to Payments and Refunds APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
jpverdejo committed Dec 27, 2019
1 parent 7e7568a commit 970f5e0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 59 deletions.
16 changes: 16 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getDefaultParameters()
return [
'accessToken' => '',
'locationId' => '',
'testMode' => false
];
}

Expand Down Expand Up @@ -71,6 +72,21 @@ public function setAppId($value)
return $this->setParameter('appId', $value);
}

/**
* Test Mode getters and setters
* @return mixed
*/

public function getTestMode()
{
return $this->getParameter('testMode');
}

public function setTestMode($value)
{
return $this->setParameter('testMode', $value);
}


/**
* Idempotency key getters and setters
Expand Down
41 changes: 19 additions & 22 deletions src/Message/ChargeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public function getData()
'amount' => $this->getAmountInteger(),
'currency' => $this->getCurrency()
];
$data['card_nonce'] = $this->getNonce();
$data['location_id'] = $this->getLocationId();
$data['source_id'] = $this->getNonce();
$data['customer_id'] = $this->getCustomerReference();
$data['customer_card_id'] = $this->getCustomerCardId();
$data['reference_id'] = $this->getReferenceId();
Expand All @@ -142,14 +143,21 @@ public function getData()

public function sendData($data)
{
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken());
$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setAccessToken($this->getAccessToken());

$api_instance = new SquareConnect\Api\TransactionsApi();
if($this->getParameter('testMode')) {
$defaultApiConfig->setHost("https://connect.squareupsandbox.com");
}

$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);

$api_instance = new SquareConnect\Api\PaymentsApi($defaultApiClient);

$tenders = [];

try {
$result = $api_instance->charge($this->getLocationId(), $data);
$result = $api_instance->createPayment($data);

if ($error = $result->getErrors()) {
$response = [
Expand All @@ -158,31 +166,20 @@ public function sendData($data)
'detail' => $error['detail']
];
} else {
$lineItems = $result->getTransaction()->getTenders();
if (count($lineItems) > 0) {
foreach ($lineItems as $key => $value) {
$tender = [];
$tender['id'] = $value->getId();
$tender['quantity'] = 1;
$tender['amount'] = $value->getAmountMoney()->getAmount() / 100;
$tender['currency'] = $value->getAmountMoney()->getCurrency();
$item['note'] = $value->getNote();
$tenders[] = $tender;
}
}
$response = [
'status' => 'success',
'transactionId' => $result->getTransaction()->getId(),
'referenceId' => $result->getTransaction()->getReferenceId(),
'created_at' => $result->getTransaction()->getCreatedAt(),
'orderId' => $result->getTransaction()->getOrderId(),
'tenders' => $tenders
'transactionId' => $result->getPayment()->getId(),
'referenceId' => $result->getPayment()->getReferenceId(),
'created_at' => $result->getPayment()->getCreatedAt(),
'orderId' => $result->getPayment()->getOrderId()
];
}
} catch (\Exception $e) {
$error = $e->getResponseBody()->errors[0]->detail ?? $e->getMessage();

$response = [
'status' => 'error',
'detail' => 'Exception when creating transaction: ' . $e->getMessage()
'detail' => 'Exception when creating transaction: ' . $error
];
}

Expand Down
19 changes: 12 additions & 7 deletions src/Message/ListRefundsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,23 @@ public function getData()

public function sendData()
{
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken());
$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setAccessToken($this->getAccessToken());

$api_instance = new SquareConnect\Api\TransactionsApi();
if($this->getParameter('testMode')) {
$defaultApiConfig->setHost("https://connect.squareupsandbox.com");
}

$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);
$api_instance = new SquareConnect\Api\RefundsApi($defaultApiClient);

try {
$result = $api_instance->listRefunds(
$this->getLocationId(),
$result = $api_instance->listPaymentRefunds(
$this->getBeginTime(),
$this->getEndTime(),
$this->getSortOrder(),
$this->getCursor()
$this->getCursor(),
$this->getLocationId()
);

if ($error = $result->getErrors()) {
Expand All @@ -117,9 +123,8 @@ public function sendData()
foreach ($refundItems as $refund) {
$item = new \stdClass();
$item->id = $refund->getId();
$item->tenderId = $refund->getTenderId();
$item->locationId = $refund->getLocationId();
$item->transactionId = $refund->getTransactionId();
$item->transactionId = $refund->getPaymentId();
$item->createdAt = $refund->getCreatedAt();
$item->reason = $refund->getReason();
$item->status = $refund->getStatus();
Expand Down
15 changes: 11 additions & 4 deletions src/Message/ListTransactionsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,24 @@ public function getData()

public function sendData()
{
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken());
$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setAccessToken($this->getAccessToken());

$api_instance = new SquareConnect\Api\TransactionsApi();
if($this->getParameter('testMode')) {
$defaultApiConfig->setHost("https://connect.squareupsandbox.com");
}

$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);

$api_instance = new SquareConnect\Api\PaymentsApi($defaultApiClient);

try {
$result = $api_instance->listTransactions(
$this->getLocationId(),
$this->getBeginTime(),
$this->getEndTime(),
$this->getSortOrder(),
$this->getCursor()
$this->getCursor(),
$this->getLocationId()
);

if ($error = $result->getErrors()) {
Expand Down
36 changes: 19 additions & 17 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,31 @@ public function getData()
{
$data = [];

$data['location_id'] = $this->getLocationId();
$data['transaction_id'] = $this->getTransactionId();
$data['body'] = new \SquareConnect\Model\CreateRefundRequest();
$data['body']->setIdempotencyKey($this->getIdempotencyKey());
$data['body']->setTenderId($this->getTenderId());
$data['body']->setReason($this->getReason());
$money = new \SquareConnect\Model\Money();
$money->setAmount($this->getAmountInteger());
$money->setCurrency($this->getCurrency());
$data['body']->setAmountMoney($money);
$data['idempotency_key'] = uniqid();
$data['payment_id'] = $this->getTransactionId();
$data['reason'] = $this->getReason();
$data['amount_money'] = [
'amount' => $this->getAmountInteger(),
'currency' => $this->getCurrency()
];

return $data;
}

public function sendData($data)
{
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken());
$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setAccessToken($this->getAccessToken());

$api_instance = new SquareConnect\Api\TransactionsApi();
if($this->getParameter('testMode')) {
$defaultApiConfig->setHost("https://connect.squareupsandbox.com");
}

$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);
$api_instance = new SquareConnect\Api\RefundsApi($defaultApiClient);

try {
$result = $api_instance->createRefund($data['location_id'], $data['transaction_id'], $data['body']);
$result = $api_instance->refundPayment($data);

if ($error = $result->getErrors()) {
$response = [
Expand All @@ -107,18 +110,17 @@ public function sendData($data)
$response = [
'status' => $result->getRefund()->getStatus(),
'id' => $result->getRefund()->getId(),
'location_id' => $result->getRefund()->getLocationId(),
'transaction_id' => $result->getRefund()->getTransactionId(),
'tender_id' => $result->getRefund()->getTenderId(),
'transaction_id' => $result->getRefund()->getPaymentId(),
'created_at' => $result->getRefund()->getCreatedAt(),
'reason' => $result->getRefund()->getReason(),
'amount' => $result->getRefund()->getAmountMoney()->getAmount(),
'currency' => $result->getRefund()->getAmountMoney()->getCurrency(),
];
$processing_fee = $result->getRefund()->getProcessingFeeMoney();
$processing_fee = $result->getRefund()->getProcessingFee();
if (!empty($processing_fee)) {
$response['processing_fee'] = $processing_fee->getAmount();
}

}
} catch (\Exception $e) {
$response = [
Expand Down
12 changes: 3 additions & 9 deletions src/Message/RefundResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ class RefundResponse extends AbstractResponse

public function isSuccessful()
{
return $this->data['status'] === 'APPROVED';
return $this->data['status'] === 'APPROVED' || $this->data['status'] === 'PENDING';
}

public function getMessage()
{
$message = '';
if (array_key_exists('code', $this->data) && strlen($this->data['code'])) {
$message .= $this->data['code'] . ': ';
}
if (array_key_exists('error', $this->data) && strlen($this->data['error'])) {
$message .= $this->data['error'];
}
return $message;

return $this->data['detail'] ?? '';
}
}

0 comments on commit 970f5e0

Please sign in to comment.