From 30e0f950d454873efdf38fb50e07be089b163dfa Mon Sep 17 00:00:00 2001 From: Tom Macdonald Date: Wed, 10 Sep 2014 09:42:20 +0200 Subject: [PATCH 1/3] add refund capability You will need to save the transaction reference from the original transaction and pass it in to the gateway refund method along with amount, currency and a transactionId (I just use the current time, down to the second). As far as I can tell, the transaction reference allows CardSave to identify the card, so you can use it to refund any amount. --- src/Gateway.php | 5 +++++ src/Message/RefundRequest.php | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/Message/RefundRequest.php diff --git a/src/Gateway.php b/src/Gateway.php index 8fbfc50..e5587b9 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -55,4 +55,9 @@ public function completePurchase(array $parameters = array()) { return $this->createRequest('\Omnipay\CardSave\Message\CompletePurchaseRequest', $parameters); } + + public function refund(array $parameters = array()) + { + return $this->createRequest('\Omnipay\CardSave\Message\RefundRequest', $parameters); + } } diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php new file mode 100644 index 0000000..15a5183 --- /dev/null +++ b/src/Message/RefundRequest.php @@ -0,0 +1,36 @@ +validate('transactionReference', 'amount', 'currency'); + + $data = new SimpleXMLElement(''); + $data->addAttribute('xmlns', $this->namespace); + + $data->PaymentMessage->MerchantAuthentication['MerchantID'] = $this->getMerchantId(); + $data->PaymentMessage->MerchantAuthentication['Password'] = $this->getPassword(); + $data->PaymentMessage->TransactionDetails['Amount'] = $this->getAmountInteger(); + $data->PaymentMessage->TransactionDetails['CurrencyCode'] = $this->getCurrencyNumeric(); + $data->PaymentMessage->TransactionDetails->OrderID = $this->getTransactionId(); + $data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType'] = 'REFUND'; + $data->PaymentMessage->TransactionDetails->MessageDetails['NewTransaction'] = false; + $data->PaymentMessage->TransactionDetails->MessageDetails['CrossReference'] = $this->getTransactionReference(); + + // requires numeric country code + // $data->PaymentMessage->CustomerDetails->BillingAddress->CountryCode = $this->getCard()->getCountryNumeric; + $data->PaymentMessage->CustomerDetails->CustomerIPAddress = $this->getClientIp(); + + return $data; + } +} From 4f52aefe591e8cd4ada38d85043228e524827c20 Mon Sep 17 00:00:00 2001 From: Tom Macdonald Date: Wed, 10 Sep 2014 15:12:20 +0200 Subject: [PATCH 2/3] add referenced purchase --- src/Gateway.php | 5 +++++ src/Message/ReferencedPurchaseRequest.php | 11 +++++++++++ src/Message/RefundRequest.php | 4 +++- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/Message/ReferencedPurchaseRequest.php diff --git a/src/Gateway.php b/src/Gateway.php index e5587b9..b8bfeec 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -51,6 +51,11 @@ public function purchase(array $parameters = array()) return $this->createRequest('\Omnipay\CardSave\Message\PurchaseRequest', $parameters); } + public function referencedPurchase(array $parameters = array()) + { + return $this->createRequest('\Omnipay\CardSave\Message\ReferencedPurchaseRequest', $parameters); + } + public function completePurchase(array $parameters = array()) { return $this->createRequest('\Omnipay\CardSave\Message\CompletePurchaseRequest', $parameters); diff --git a/src/Message/ReferencedPurchaseRequest.php b/src/Message/ReferencedPurchaseRequest.php new file mode 100644 index 0000000..e7430fe --- /dev/null +++ b/src/Message/ReferencedPurchaseRequest.php @@ -0,0 +1,11 @@ +PaymentMessage->TransactionDetails['Amount'] = $this->getAmountInteger(); $data->PaymentMessage->TransactionDetails['CurrencyCode'] = $this->getCurrencyNumeric(); $data->PaymentMessage->TransactionDetails->OrderID = $this->getTransactionId(); - $data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType'] = 'REFUND'; + $data->PaymentMessage->TransactionDetails->OrderDescription = $this->getDescription(); + $data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType'] = $this->transactionType; $data->PaymentMessage->TransactionDetails->MessageDetails['NewTransaction'] = false; $data->PaymentMessage->TransactionDetails->MessageDetails['CrossReference'] = $this->getTransactionReference(); From 083c7dcb13805a742ff744bee91d9fe283ce9218 Mon Sep 17 00:00:00 2001 From: Tom Macdonald Date: Thu, 18 Sep 2014 21:33:17 +0200 Subject: [PATCH 3/3] add tests for refund and referenced purchase --- .../Message/ReferencedPurchaseRequestTest.php | 37 +++++++++++++++++++ tests/Message/RefundRequestTest.php | 37 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/Message/ReferencedPurchaseRequestTest.php create mode 100644 tests/Message/RefundRequestTest.php diff --git a/tests/Message/ReferencedPurchaseRequestTest.php b/tests/Message/ReferencedPurchaseRequestTest.php new file mode 100644 index 0000000..0eeaa2b --- /dev/null +++ b/tests/Message/ReferencedPurchaseRequestTest.php @@ -0,0 +1,37 @@ +request = new ReferencedPurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize( + array( + 'amount' => '12.00', + 'transactionReference' => '0987654345678900987654', + 'currency' => 'GBP', + 'testMode' => true, + ) + ); + } + + public function testGetData() + { + $data = $this->request->getData(); + + /* + * See https://bugs.php.net/bug.php?id=29500 for why this is cast to string + */ + $this->assertSame('SALE', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType']); + $this->assertSame('1200', (string)$data->PaymentMessage->TransactionDetails['Amount']); + $this->assertSame('826', (string)$data->PaymentMessage->TransactionDetails['CurrencyCode']); + $this->assertSame('0987654345678900987654', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['CrossReference']); + } + +} diff --git a/tests/Message/RefundRequestTest.php b/tests/Message/RefundRequestTest.php new file mode 100644 index 0000000..060e1d2 --- /dev/null +++ b/tests/Message/RefundRequestTest.php @@ -0,0 +1,37 @@ +request = new RefundRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize( + array( + 'amount' => '12.00', + 'transactionReference' => '0987654345678900987654', + 'currency' => 'GBP', + 'testMode' => true, + ) + ); + } + + public function testGetData() + { + $data = $this->request->getData(); + + /* + * See https://bugs.php.net/bug.php?id=29500 for why this is cast to string + */ + $this->assertSame('REFUND', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType']); + $this->assertSame('1200', (string)$data->PaymentMessage->TransactionDetails['Amount']); + $this->assertSame('826', (string)$data->PaymentMessage->TransactionDetails['CurrencyCode']); + $this->assertSame('0987654345678900987654', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['CrossReference']); + } + +}