From 543b3413552187a1ceedb8cfe6d2693fd431d2ae Mon Sep 17 00:00:00 2001 From: Rian Zietsman Date: Thu, 30 Apr 2020 14:31:30 +0200 Subject: [PATCH] saving merchant reference in results, allow pre-auth on card regsitration --- src/Api/PaymentMethods/CopyAndPay.php | 15 ++++++++++++--- src/Api/PaymentMethods/ServerToServer.php | 1 + src/Models/PaymentCard.php | 8 ++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Api/PaymentMethods/CopyAndPay.php b/src/Api/PaymentMethods/CopyAndPay.php index 712b3bf..e0ecd0a 100644 --- a/src/Api/PaymentMethods/CopyAndPay.php +++ b/src/Api/PaymentMethods/CopyAndPay.php @@ -117,17 +117,26 @@ public function getCheckoutRegistrationResult($path, $owner) * * @return mixed|\Psr\Http\Message\ResponseInterface */ - public function registerCardDuringPayment(int $amount) + public function registerCardDuringPayment(int $amount, $preAuth = false) { - return $this->client->request('POST', 'checkouts', [ + $response = $this->client->request('POST', 'checkouts', [ 'form_params' => [ 'entityId' => $this->settings->getEntityIdOnceOff(), 'amount' => Currency::paymentFriendlyNumber($amount), 'currency' => 'ZAR', - 'paymentType' => self::DEBIT, + 'paymentType' => $preAuth == true ? self::PREAUTHORISATION : self::DEBIT, 'createRegistration' => true, ], ])->getBody()->getContents(); + + $result = json_decode($response, true); + $responseCheck = new Response(); + + if ($responseCheck->isSuccessfulResponse($result['result']['code'])) { + return $result['id']; + } + + return false; } /** diff --git a/src/Api/PaymentMethods/ServerToServer.php b/src/Api/PaymentMethods/ServerToServer.php index 84cebd0..ddb959b 100644 --- a/src/Api/PaymentMethods/ServerToServer.php +++ b/src/Api/PaymentMethods/ServerToServer.php @@ -103,6 +103,7 @@ public function repeatedPayment(PaymentCard $card, $owner, int $amount, string $ 'currency' => 'ZAR', 'paymentType' => self::DEBIT, 'recurringType' => $type, + 'merchantTransactionId' => class_basename($owner) . '-' . $owner->id ], ] )->getBody()->getContents(); diff --git a/src/Models/PaymentCard.php b/src/Models/PaymentCard.php index c9d01df..29a6ae0 100644 --- a/src/Models/PaymentCard.php +++ b/src/Models/PaymentCard.php @@ -52,10 +52,10 @@ public function results() } public function saveResult($results, $owner) - { + { $result = $this->results()->create([ - 'transaction_id' => $results['id'] ?? null, - 'registration_id' => $results['registrationId'] ?? null, + 'transaction_id' => $results['merchantTransactionId'] ?? null, + 'registration_id' => $results['id'] ?? null, 'payment_type' => $results['paymentType'] ?? null, 'amount' => isset($results['amount']) ? ($results['amount'] * 100) : null, 'currency' => $results['currency'] ?? 'ZAR', @@ -82,7 +82,7 @@ public static function create(array $attributes = []) 'holder' => $result['card']['holder'], 'expiry_month' => $result['card']['expiryMonth'], 'expiry_year' => $result['card']['expiryYear'], - 'registration_id' => $result['id'], + 'registration_id' => $result['registrationId'] ?? $result['id'], ]; $model = static::query()->create($formattedAttrs);