From be94e90c52b3b8ad5d588755862d9cb896c12bb6 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Thu, 4 Apr 2019 15:10:52 +0200 Subject: [PATCH 01/17] Integrate with SYNC api. --- lib/PayPal/Api/Amount.php | 25 ++ .../Api/ReportingTransactionDetails.php | 39 +++ lib/PayPal/Api/ReportingTransactionInfo.php | 271 ++++++++++++++++++ lib/PayPal/Api/ReportingTransactions.php | 68 +++++ .../ListTransactions.php | 24 ++ .../Test/Api/ReportingTransactionsTest.php | 73 +++++ 6 files changed, 500 insertions(+) create mode 100644 lib/PayPal/Api/ReportingTransactionDetails.php create mode 100644 lib/PayPal/Api/ReportingTransactionInfo.php create mode 100644 lib/PayPal/Api/ReportingTransactions.php create mode 100644 sample/reporting-transactions/ListTransactions.php create mode 100644 tests/PayPal/Test/Api/ReportingTransactionsTest.php diff --git a/lib/PayPal/Api/Amount.php b/lib/PayPal/Api/Amount.php index 82fd6c8e..4c395117 100644 --- a/lib/PayPal/Api/Amount.php +++ b/lib/PayPal/Api/Amount.php @@ -67,6 +67,31 @@ public function getTotal() return $this->total; } + /** + * Set amount value. Format it using currency converter. + * + * @param string|double $value + * + * @return $this + */ + public function setValue($value) + { + NumericValidator::validate($value, "Total"); + $value = FormatConverter::formatToPrice($value, $this->getCurrency()); + $this->value = $value; + return $this; + } + + /** + * Get amount value. + * + * @return string + */ + public function getValue() + { + return $this->value; + } + /** * Additional details of the payment amount. * diff --git a/lib/PayPal/Api/ReportingTransactionDetails.php b/lib/PayPal/Api/ReportingTransactionDetails.php new file mode 100644 index 00000000..13ef78fa --- /dev/null +++ b/lib/PayPal/Api/ReportingTransactionDetails.php @@ -0,0 +1,39 @@ +transactionInfo; + } + + /** + * @param \PayPal\Api\ReportingTransactionInfo $transactionInfo + */ + public function setTransactionInfo($transactionInfo) + { + $this->transactionInfo = $transactionInfo; + } + +} diff --git a/lib/PayPal/Api/ReportingTransactionInfo.php b/lib/PayPal/Api/ReportingTransactionInfo.php new file mode 100644 index 00000000..cdce7151 --- /dev/null +++ b/lib/PayPal/Api/ReportingTransactionInfo.php @@ -0,0 +1,271 @@ +paypalAccountId; + } + + /** + * The ID of the PayPal account of the counterparty. + * + * @param string $paypalAccountId + */ + public function setPaypalAccountId($paypalAccountId) + { + $this->paypalAccountId = $paypalAccountId; + } + + /** + * The PayPal-generated transaction ID. + * + * @return string + */ + public function getTransactionId() + { + return $this->transactionId; + } + + /** + * The PayPal-generated transaction ID. + * + * @param string $transactionId + */ + public function setTransactionId($transactionId) + { + $this->transactionId = $transactionId; + } + + /** + * A five-digit transaction event code that classifies the transaction type based on money movement and debit or credit. For example, T0001. See Transaction event codes. + * + * @return string + */ + public function getTransactionEventCode() + { + return $this->transactionEventCode; + } + + /** + * A five-digit transaction event code that classifies the transaction type based on money movement and debit or credit. For example, T0001. See Transaction event codes. + * + * @param string $transactionEventCode + */ + public function setTransactionEventCode($transactionEventCode) + { + $this->transactionEventCode = $transactionEventCode; + } + + /** + * The date and time when work on a transaction began in the PayPal system, as expressed in the time zone of the account on this side of the payment. Specify the value in Internet date and time format. + * + * @return string + */ + public function getTransactionInitiationDate() + { + return $this->transactionInitiationDate; + } + + /** + * The date and time when work on a transaction began in the PayPal system, as expressed in the time zone of the account on this side of the payment. Specify the value in Internet date and time format. + * + * @param string $transactionInitiationDate + */ + public function setTransactionInitiationDate($transactionInitiationDate) + { + $this->transactionInitiationDate = $transactionInitiationDate; + } + + /** + * The date and time when the transaction was last changed, as expressed in the time zone of the account on this side of the payment. Specify the value in Internet date and time format. + * + * @return string + */ + public function getTransactionUpdatedDate() + { + return $this->transactionUpdatedDate; + } + + /** + * The date and time when the transaction was last changed, as expressed in the time zone of the account on this side of the payment. Specify the value in Internet date and time format. + * + * @param string $transactionUpdatedDate + */ + public function setTransactionUpdatedDate($transactionUpdatedDate) + { + $this->transactionUpdatedDate = $transactionUpdatedDate; + } + + /** + * The all-inclusive gross transaction amount that was transferred between the sender and receiver through PayPal. + * + * @return \PayPal\Api\Amount + */ + public function getTransactionAmount() + { + return $this->transactionAmount; + } + + /** + * The all-inclusive gross transaction amount that was transferred between the sender and receiver through PayPal. + * + * @param \PayPal\Api\Amount $transactionAmount + */ + public function setTransactionAmount($transactionAmount) + { + $this->transactionAmount = $transactionAmount; + } + + /** + * The PayPal fee amount. All transaction fees are included in this amount, which is the record of fee associated with the transaction. + * + * @return \PayPal\Api\Amount + */ + public function getFeeAmount() + { + return $this->feeAmount; + } + + /** + * The PayPal fee amount. All transaction fees are included in this amount, which is the record of fee associated with the transaction. + * + * @param \PayPal\Api\Amount $feeAmount + */ + public function setFeeAmount($feeAmount) + { + $this->feeAmount = $feeAmount; + } + + /** + * Filters the transactions in the response by a PayPal transaction status code. + * + * @return string + */ + public function getTransactionStatus() + { + return $this->transactionStatus; + } + + /** + * Filters the transactions in the response by a PayPal transaction status code. + * + * @param string $transactionStatus + */ + public function setTransactionStatus($transactionStatus) + { + $this->transactionStatus = $transactionStatus; + } + + /** + * The ending balance. + * + * @return \PayPal\Api\Amount + */ + public function getEndingBalance() + { + return $this->endingBalance; + } + + /** + * The ending balance. + * + * @param \PayPal\Api\Amount $endingBalance + */ + public function setEndingBalance($endingBalance) + { + $this->endingBalance = $endingBalance; + } + + /** + * The available amount of transaction currency during the completion of this transaction. + * + * @return \PayPal\Api\Amount + */ + public function getAvailableBalance() + { + return $this->availableBalance; + } + + /** + * The available amount of transaction currency during the completion of this transaction. + * + * @param \PayPal\Api\Amount $availableBalance + */ + public function setAvailableBalance($availableBalance) + { + $this->availableBalance = $availableBalance; + } + + /** + * The invoice ID that is sent by the merchant with the transaction. + * + * @return string + */ + public function getInvoiceId() + { + return $this->invoiceId; + } + + /** + * The invoice ID that is sent by the merchant with the transaction. + * + * @param mixed $invoiceId + */ + public function setInvoiceId($invoiceId) + { + $this->invoiceId = $invoiceId; + } + + /** + * Indicates whether the transaction is eligible for protection. + * + * @return mixed + */ + public function getProtectionElegibility() + { + return $this->protectionElegibility; + } + + /** + * Indicates whether the transaction is eligible for protection. + * + * @param mixed $protectionElegibility + */ + public function setProtectionElegibility($protectionElegibility) + { + $this->protectionElegibility = $protectionElegibility; + } + +} diff --git a/lib/PayPal/Api/ReportingTransactions.php b/lib/PayPal/Api/ReportingTransactions.php new file mode 100644 index 00000000..0d5b2a74 --- /dev/null +++ b/lib/PayPal/Api/ReportingTransactions.php @@ -0,0 +1,68 @@ +transactionDetails; + } + + public function setTransactionDetails($transactionDetails) { + $this->transactionDetails = $transactionDetails; + } + + public static function get($params, $apiContext, $restCall = null) { + ArgumentValidator::validate($params, 'params'); + $payLoad = ""; + $allowedParams = array( + 'transaction_amount' => 1, + 'transaction_currency' => 1, + 'start_date' => 1, + 'end_date' => 1, + 'payment_instrument_type' => 1, + 'store_id' => 1, + 'terminal_id' => 1, + 'fields' => 1, + 'balance_affecting_records_only' => 1, + 'page_size' => 1, + 'page' => 1, + ); + $json = self::executeCall( + "/v1/reporting/transactions?" . http_build_query(array_intersect_key($params, $allowedParams)), + "GET", + $payLoad, + null, + $apiContext, + $restCall + ); + $reporting_transactions = new ReportingTransactions(); + $reporting_transactions->fromJson($json); + return $reporting_transactions; + } + + public static function all($params, $apiContext) { + + } + +} diff --git a/sample/reporting-transactions/ListTransactions.php b/sample/reporting-transactions/ListTransactions.php new file mode 100644 index 00000000..0f0e689f --- /dev/null +++ b/sample/reporting-transactions/ListTransactions.php @@ -0,0 +1,24 @@ + 100, 'start_date' => '2019-03-20T00:00:00-0700', 'end_date' => '2019-04-10T23:59:59-0700', 'fields' => 'all', 'page' => 1); + + $payments = ReportingTransactions::get($params, $apiContext); +} catch (Exception $ex) { + // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY + ResultPrinter::printError("List Payments", "Payment", null, $params, $ex); + exit(1); +} + +// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY +ResultPrinter::printResult("List Payments", "Payment", null, $params, $payments); diff --git a/tests/PayPal/Test/Api/ReportingTransactionsTest.php b/tests/PayPal/Test/Api/ReportingTransactionsTest.php new file mode 100644 index 00000000..9aaa16b5 --- /dev/null +++ b/tests/PayPal/Test/Api/ReportingTransactionsTest.php @@ -0,0 +1,73 @@ +getMockBuilder('\PayPal\Transport\PayPalRestCall') + ->disableOriginalConstructor() + ->getMock(); + + $mockPPRestCall->expects($this->any()) + ->method('execute') + ->will($this->returnValue( + ReportingTransactionsTest::getJson() + )); + + $params = array('count' => 100, 'start_date' => '2019-03-20T00:00:00-0700', 'end_date' => '2019-04-10T23:59:59-0700', 'fields' => 'all', 'page' => 1); + + /** @var \PayPal\Api\ReportingTransactions $transactions */ + $transactions = $obj->get($params, $mockApiContext, $mockPPRestCall); + $this->assertInstanceOf('\PayPal\Api\ReportingTransactions', $transactions); + $transaction_details = $transactions->getTransactionDetails(); + $this->assertTrue(is_array($transaction_details)); + $this->assertInstanceOf('\PayPal\Api\ReportingTransactionDetails', reset($transaction_details)); + /** @var ReportingTransactionInfo $transaction_info */ + $transaction_info = $transaction_details[0]->getTransactionInfo(); + $this->assertInstanceOf('\PayPal\Api\ReportingTransactionInfo', $transaction_info); + $this->assertEquals($transaction_info->getTransactionAmount()->getValue(), '50.00'); + $this->assertEquals($transaction_info->getEndingBalance()->getValue(), '50.00'); + $this->assertEquals($transaction_info->getInvoiceId(), '22892-1554291060'); + $this->assertEquals($transaction_info->getTransactionId(), '61041S'); + $this->assertEquals($transaction_info->getTransactionEventCode(), 'T0007'); + // @TODO: finish transaction details assertions! + } + + public static function getJson() + { + return '{"transaction_details":[{"transaction_info":{"paypal_account_id":"1234","transaction_id":"61041S","transaction_event_code":"T0007","transaction_initiation_date":"2019-04-03T11:33:04+0000","transaction_updated_date":"2019-04-03T11:33:04+0000","transaction_amount":{"currency_code":"EUR","value":"50.00"},"fee_amount":{"currency_code":"EUR","value":"-42.31"},"transaction_status":"S","ending_balance":{"currency_code":"EUR","value":"50.00"},"available_balance":{"currency_code":"EUR","value":"50.00"},"invoice_id":"22892-1554291060","protection_eligibility":"01"},"payer_info":{"account_id":"1234","email_address":"debug@example.com","address_status":"N","payer_status":"Y","payer_name":{"given_name":"test","surname":"buyer","alternate_full_name":"test buyer"},"country_code":"ES"},"shipping_info":{"name":"test, buyer"},"cart_info":{"item_details":[{"item_name":"Example","item_quantity":"1","item_unit_price":{"currency_code":"EUR","value":"50.00"},"item_amount":{"currency_code":"EUR","value":"50.00"},"total_item_amount":{"currency_code":"EUR","value":"50.00"},"invoice_number":"22892-1554291060","checkout_options":[{"checkout_option_name":"Product quantity","checkout_option_value":"1"}]}]},"store_info":{},"auction_info":{},"incentive_info":{}}],"account_number":"12345Y","start_date":"2019-03-20T07:00:00+0000","end_date":"2019-04-04T08:59:59+0000","last_refreshed_datetime":"2019-04-04T08:59:59+0000","page":1,"total_items":2,"total_pages":1,"links":[{"href":"https://api.sandbox.paypal.com/v1/reporting/transactions?start_date=2019-03-20T00%3A00%3A00-0700&end_date=2019-04-10T23%3A59%3A59-0700&fields=all&page_size=100&page=1","rel":"self","method":"GET"}]}'; + } + + /** + * Gets Object Instance with Json data filled in + * @return Payment + */ + public static function getObject() + { + return new ReportingTransactions(self::getJson()); + } + + public function mockProvider() + { + $obj = self::getObject(); + $mockApiContext = $this->getMockBuilder('ApiContext') + ->disableOriginalConstructor() + ->getMock(); + return array( + array($obj, $mockApiContext), + array($obj, null) + ); + } + +} From f7ba36de59be2cd097e2e0bb83c0ce320df0cf6a Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:34:49 +0200 Subject: [PATCH 02/17] Model for Reporting transaction amount. --- lib/PayPal/Api/ReportingTransactionAmount.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 lib/PayPal/Api/ReportingTransactionAmount.php diff --git a/lib/PayPal/Api/ReportingTransactionAmount.php b/lib/PayPal/Api/ReportingTransactionAmount.php new file mode 100644 index 00000000..4a2c9963 --- /dev/null +++ b/lib/PayPal/Api/ReportingTransactionAmount.php @@ -0,0 +1,69 @@ +currencyCode = $currency_code; + return $this; + } + + /** + * 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/). PayPal does not support all currencies. + * + * @return string + */ + public function getCurrencyCode() + { + return $this->currencyCode; + } + + /** + * Set amount value. Format it using currency converter. + * + * @param string|double $value + * + * @return $this + */ + public function setValue($value) + { + NumericValidator::validate($value, "Total"); + $value = FormatConverter::formatToPrice($value, $this->getCurrencyCode()); + $this->value = $value; + return $this; + } + + /** + * Get amount value. + * + * @return string + */ + public function getValue() + { + return $this->value; + } + +} From ed63a46bf43d71dbee88ba10e60fa3ece8ec0251 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:35:28 +0200 Subject: [PATCH 03/17] Use ReportingTransactionAmount for Reporting transaction info amount fields. --- lib/PayPal/Api/ReportingTransactionInfo.php | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/PayPal/Api/ReportingTransactionInfo.php b/lib/PayPal/Api/ReportingTransactionInfo.php index cdce7151..d3b1aed4 100644 --- a/lib/PayPal/Api/ReportingTransactionInfo.php +++ b/lib/PayPal/Api/ReportingTransactionInfo.php @@ -18,12 +18,12 @@ * @property string transaction_event_code * @property string transaction_initiation_date * @property string transaction_updated_date - * @property \PayPal\Api\Amount transaction_amount - * @property \PayPal\Api\Amount fee_amount - * @property \PayPal\Api\Amount ending_balance - * @property \PayPal\Api\Amount available_balance - * @property \PayPal\Api\Amount invoice_id - * @property \PayPal\Api\Amount protection_elegibility + * @property \PayPal\Api\ReportingTransactionAmount transaction_amount + * @property \PayPal\Api\ReportingTransactionAmount fee_amount + * @property \PayPal\Api\ReportingTransactionAmount ending_balance + * @property \PayPal\Api\ReportingTransactionAmount available_balance + * @property string invoice_id + * @property string protection_elegibility */ class ReportingTransactionInfo extends PayPalModel { @@ -131,7 +131,7 @@ public function setTransactionUpdatedDate($transactionUpdatedDate) /** * The all-inclusive gross transaction amount that was transferred between the sender and receiver through PayPal. * - * @return \PayPal\Api\Amount + * @return \PayPal\Api\ReportingTransactionAmount */ public function getTransactionAmount() { @@ -141,7 +141,7 @@ public function getTransactionAmount() /** * The all-inclusive gross transaction amount that was transferred between the sender and receiver through PayPal. * - * @param \PayPal\Api\Amount $transactionAmount + * @param \PayPal\Api\ReportingTransactionAmount $transactionAmount */ public function setTransactionAmount($transactionAmount) { @@ -151,7 +151,7 @@ public function setTransactionAmount($transactionAmount) /** * The PayPal fee amount. All transaction fees are included in this amount, which is the record of fee associated with the transaction. * - * @return \PayPal\Api\Amount + * @return \PayPal\Api\ReportingTransactionAmount */ public function getFeeAmount() { @@ -161,7 +161,7 @@ public function getFeeAmount() /** * The PayPal fee amount. All transaction fees are included in this amount, which is the record of fee associated with the transaction. * - * @param \PayPal\Api\Amount $feeAmount + * @param \PayPal\Api\ReportingTransactionAmount $feeAmount */ public function setFeeAmount($feeAmount) { @@ -191,7 +191,7 @@ public function setTransactionStatus($transactionStatus) /** * The ending balance. * - * @return \PayPal\Api\Amount + * @return \PayPal\Api\ReportingTransactionAmount */ public function getEndingBalance() { @@ -201,7 +201,7 @@ public function getEndingBalance() /** * The ending balance. * - * @param \PayPal\Api\Amount $endingBalance + * @param \PayPal\Api\ReportingTransactionAmount $endingBalance */ public function setEndingBalance($endingBalance) { @@ -211,7 +211,7 @@ public function setEndingBalance($endingBalance) /** * The available amount of transaction currency during the completion of this transaction. * - * @return \PayPal\Api\Amount + * @return \PayPal\Api\ReportingTransactionAmount */ public function getAvailableBalance() { @@ -221,7 +221,7 @@ public function getAvailableBalance() /** * The available amount of transaction currency during the completion of this transaction. * - * @param \PayPal\Api\Amount $availableBalance + * @param \PayPal\Api\ReportingTransactionAmount $availableBalance */ public function setAvailableBalance($availableBalance) { From b85edd436717b22d5568a41ad554bb521adf6c1b Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:35:55 +0200 Subject: [PATCH 04/17] Set correct data type for reporting transaction info invoice id. --- lib/PayPal/Api/ReportingTransactionInfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/PayPal/Api/ReportingTransactionInfo.php b/lib/PayPal/Api/ReportingTransactionInfo.php index d3b1aed4..cfbc9829 100644 --- a/lib/PayPal/Api/ReportingTransactionInfo.php +++ b/lib/PayPal/Api/ReportingTransactionInfo.php @@ -241,7 +241,7 @@ public function getInvoiceId() /** * The invoice ID that is sent by the merchant with the transaction. * - * @param mixed $invoiceId + * @param string $invoiceId */ public function setInvoiceId($invoiceId) { From dff67c6af76e1236ceeb4848db37305423fc2862 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:36:13 +0200 Subject: [PATCH 05/17] Add protection elegibility correctly --- lib/PayPal/Api/ReportingTransactionInfo.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/PayPal/Api/ReportingTransactionInfo.php b/lib/PayPal/Api/ReportingTransactionInfo.php index cfbc9829..c01dabf3 100644 --- a/lib/PayPal/Api/ReportingTransactionInfo.php +++ b/lib/PayPal/Api/ReportingTransactionInfo.php @@ -251,21 +251,21 @@ public function setInvoiceId($invoiceId) /** * Indicates whether the transaction is eligible for protection. * - * @return mixed + * @return string */ - public function getProtectionElegibility() + public function getProtectionEligibility() { - return $this->protectionElegibility; + return $this->protectionEligibility; } /** * Indicates whether the transaction is eligible for protection. * - * @param mixed $protectionElegibility + * @param string $protectionElegibility */ - public function setProtectionElegibility($protectionElegibility) + public function setProtectionEligibility($protectionElegibility) { - $this->protectionElegibility = $protectionElegibility; + $this->protectionEligibility = $protectionElegibility; } } From 36a012738507e57d5ef09fe1b27487bc265ddc8a Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:36:25 +0200 Subject: [PATCH 06/17] Update list transactions sample --- sample/reporting-transactions/ListTransactions.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sample/reporting-transactions/ListTransactions.php b/sample/reporting-transactions/ListTransactions.php index 0f0e689f..5fe6f52d 100644 --- a/sample/reporting-transactions/ListTransactions.php +++ b/sample/reporting-transactions/ListTransactions.php @@ -3,9 +3,9 @@ require __DIR__ . '/../bootstrap.php'; use PayPal\Api\ReportingTransactions; -// ### Retrieve payment -// Retrieve the PaymentHistory object by calling the -// static `get` method on the Payment class, +// ### List transactions. +// Retrieve the reporting transaction list from object by calling the +// static `get` method on the ReportingTransaction class, // and pass a Map object that contains // query parameters for paginations and filtering. // Refer the method doc for valid values for keys @@ -13,7 +13,7 @@ try { $params = array('count' => 100, 'start_date' => '2019-03-20T00:00:00-0700', 'end_date' => '2019-04-10T23:59:59-0700', 'fields' => 'all', 'page' => 1); - $payments = ReportingTransactions::get($params, $apiContext); + $response = ReportingTransactions::get($params, $apiContext); } catch (Exception $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY ResultPrinter::printError("List Payments", "Payment", null, $params, $ex); @@ -21,4 +21,5 @@ } // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY -ResultPrinter::printResult("List Payments", "Payment", null, $params, $payments); +ResultPrinter::printResult("List transactions", "Payment", null, $params, $response); +ResultPrinter::printResult("Transaction details", "Payment", null, $params, $response->getTransactionDetails()); From 74685306b475d05bec8eda53a103b635fbb4c700 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:36:33 +0200 Subject: [PATCH 07/17] Finish Reporting transactions test --- tests/PayPal/Test/Api/ReportingTransactionsTest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/PayPal/Test/Api/ReportingTransactionsTest.php b/tests/PayPal/Test/Api/ReportingTransactionsTest.php index 9aaa16b5..b5f5b503 100644 --- a/tests/PayPal/Test/Api/ReportingTransactionsTest.php +++ b/tests/PayPal/Test/Api/ReportingTransactionsTest.php @@ -37,11 +37,20 @@ public function testGet($obj, $mockApiContext) $transaction_info = $transaction_details[0]->getTransactionInfo(); $this->assertInstanceOf('\PayPal\Api\ReportingTransactionInfo', $transaction_info); $this->assertEquals($transaction_info->getTransactionAmount()->getValue(), '50.00'); + $this->assertEquals($transaction_info->getTransactionAmount()->getCurrencyCode(), 'EUR'); $this->assertEquals($transaction_info->getEndingBalance()->getValue(), '50.00'); + $this->assertEquals($transaction_info->getEndingBalance()->getCurrencyCode(), 'EUR'); + $this->assertEquals($transaction_info->getAvailableBalance()->getValue(), '50.00'); + $this->assertEquals($transaction_info->getAvailableBalance()->getCurrencyCode(), 'EUR'); + $this->assertEquals($transaction_info->getFeeAmount()->getValue(), '-42.31'); + $this->assertEquals($transaction_info->getFeeAmount()->getCurrencyCode(), 'EUR'); $this->assertEquals($transaction_info->getInvoiceId(), '22892-1554291060'); $this->assertEquals($transaction_info->getTransactionId(), '61041S'); $this->assertEquals($transaction_info->getTransactionEventCode(), 'T0007'); - // @TODO: finish transaction details assertions! + $this->assertEquals($transaction_info->getTransactionInitiationDate(), '2019-04-03T11:33:04+0000'); + $this->assertEquals($transaction_info->getTransactionUpdatedDate(), '2019-04-03T11:33:04+0000'); + $this->assertEquals($transaction_info->getProtectionEligibility(), '01'); + $this->assertEquals($transaction_info->getPaypalAccountId(), '1234'); } public static function getJson() From 2bbb56486fd8017704b19a5c4ab8ea150096640d Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:38:11 +0200 Subject: [PATCH 08/17] Revert Amount class as we are not using it for reporting transactions. --- lib/PayPal/Api/Amount.php | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/lib/PayPal/Api/Amount.php b/lib/PayPal/Api/Amount.php index 4c395117..30b97f70 100644 --- a/lib/PayPal/Api/Amount.php +++ b/lib/PayPal/Api/Amount.php @@ -23,7 +23,7 @@ class Amount extends PayPalModel * 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/). PayPal does not support all currencies. * * @param string $currency - * + * * @return $this */ public function setCurrency($currency) @@ -46,7 +46,7 @@ public function getCurrency() * Total amount charged from the payer to the payee. In case of a refund, this is the refunded amount to the original payer from the payee. 10 characters max with support for 2 decimal places. * * @param string|double $total - * + * * @return $this */ public function setTotal($total) @@ -67,36 +67,11 @@ public function getTotal() return $this->total; } - /** - * Set amount value. Format it using currency converter. - * - * @param string|double $value - * - * @return $this - */ - public function setValue($value) - { - NumericValidator::validate($value, "Total"); - $value = FormatConverter::formatToPrice($value, $this->getCurrency()); - $this->value = $value; - return $this; - } - - /** - * Get amount value. - * - * @return string - */ - public function getValue() - { - return $this->value; - } - /** * Additional details of the payment amount. * * @param \PayPal\Api\Details $details - * + * * @return $this */ public function setDetails($details) From 4d0b1ea27717e58b10cd98fdb0c3333a66b3b95b Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:51:22 +0200 Subject: [PATCH 09/17] Coding style --- lib/PayPal/Api/ReportingTransactionAmount.php | 1 - lib/PayPal/Api/ReportingTransactionDetails.php | 2 -- lib/PayPal/Api/ReportingTransactionInfo.php | 2 -- 3 files changed, 5 deletions(-) diff --git a/lib/PayPal/Api/ReportingTransactionAmount.php b/lib/PayPal/Api/ReportingTransactionAmount.php index 4a2c9963..43f65e53 100644 --- a/lib/PayPal/Api/ReportingTransactionAmount.php +++ b/lib/PayPal/Api/ReportingTransactionAmount.php @@ -65,5 +65,4 @@ public function getValue() { return $this->value; } - } diff --git a/lib/PayPal/Api/ReportingTransactionDetails.php b/lib/PayPal/Api/ReportingTransactionDetails.php index 13ef78fa..577e6957 100644 --- a/lib/PayPal/Api/ReportingTransactionDetails.php +++ b/lib/PayPal/Api/ReportingTransactionDetails.php @@ -3,7 +3,6 @@ namespace PayPal\Api; - use PayPal\Common\PayPalModel; /** @@ -35,5 +34,4 @@ public function setTransactionInfo($transactionInfo) { $this->transactionInfo = $transactionInfo; } - } diff --git a/lib/PayPal/Api/ReportingTransactionInfo.php b/lib/PayPal/Api/ReportingTransactionInfo.php index c01dabf3..27b67487 100644 --- a/lib/PayPal/Api/ReportingTransactionInfo.php +++ b/lib/PayPal/Api/ReportingTransactionInfo.php @@ -3,7 +3,6 @@ namespace PayPal\Api; - use PayPal\Common\PayPalModel; /** @@ -267,5 +266,4 @@ public function setProtectionEligibility($protectionElegibility) { $this->protectionEligibility = $protectionElegibility; } - } From b6be38c2c89d4c950640415d4b498f97cc3274e5 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:51:48 +0200 Subject: [PATCH 10/17] Reporting transactions: store total items. --- lib/PayPal/Api/ReportingTransactions.php | 17 +++++++++++++++++ .../Test/Api/ReportingTransactionsTest.php | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/PayPal/Api/ReportingTransactions.php b/lib/PayPal/Api/ReportingTransactions.php index 0d5b2a74..a893f626 100644 --- a/lib/PayPal/Api/ReportingTransactions.php +++ b/lib/PayPal/Api/ReportingTransactions.php @@ -19,9 +19,26 @@ class ReportingTransactions extends PayPalResourceModel { protected $startDate; protected $endDate; + protected $transactionDetails = array(); + protected $totalItems; + + /** + * @return int + */ + public function getTotalItems() + { + return $this->totalItems; + } /** + * @param int $totalItems + */ + public function setTotalItems($totalItems) + { + $this->totalItems = $totalItems; + } + * @return \PayPal\Api\ReportingTransactionDetails[] */ public function getTransactionDetails() { diff --git a/tests/PayPal/Test/Api/ReportingTransactionsTest.php b/tests/PayPal/Test/Api/ReportingTransactionsTest.php index b5f5b503..0b88b968 100644 --- a/tests/PayPal/Test/Api/ReportingTransactionsTest.php +++ b/tests/PayPal/Test/Api/ReportingTransactionsTest.php @@ -30,6 +30,7 @@ public function testGet($obj, $mockApiContext) /** @var \PayPal\Api\ReportingTransactions $transactions */ $transactions = $obj->get($params, $mockApiContext, $mockPPRestCall); $this->assertInstanceOf('\PayPal\Api\ReportingTransactions', $transactions); + $this->assertEquals(2, $transactions->getTotalItems()); $transaction_details = $transactions->getTransactionDetails(); $this->assertTrue(is_array($transaction_details)); $this->assertInstanceOf('\PayPal\Api\ReportingTransactionDetails', reset($transaction_details)); @@ -78,5 +79,4 @@ public function mockProvider() array($obj, null) ); } - } From f85928a61cc6896ff7e319a351285efdc52cfe26 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:52:13 +0200 Subject: [PATCH 11/17] Reporting transaction details: allow append items. --- lib/PayPal/Api/ReportingTransactions.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/PayPal/Api/ReportingTransactions.php b/lib/PayPal/Api/ReportingTransactions.php index a893f626..4ef633fb 100644 --- a/lib/PayPal/Api/ReportingTransactions.php +++ b/lib/PayPal/Api/ReportingTransactions.php @@ -39,14 +39,23 @@ public function setTotalItems($totalItems) $this->totalItems = $totalItems; } + /** + * An array of transaction detail objects. + * * @return \PayPal\Api\ReportingTransactionDetails[] */ - public function getTransactionDetails() { + public function getTransactionDetails() return $this->transactionDetails; } - public function setTransactionDetails($transactionDetails) { - $this->transactionDetails = $transactionDetails; + /** + * An array of transaction detail objects. + * + * @param \PayPal\Api\ReportingTransactionDetails[] $transactionDetails + */ + public function setTransactionDetails($transactionDetails) + { + $this->transactionDetails = array_merge($this->transactionDetails, $transactionDetails); } public static function get($params, $apiContext, $restCall = null) { From 17366817c338b3c789a3a8197badbd2276786447 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 08:53:11 +0200 Subject: [PATCH 12/17] Reporting transactions: coding style. --- lib/PayPal/Api/ReportingTransactions.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/PayPal/Api/ReportingTransactions.php b/lib/PayPal/Api/ReportingTransactions.php index 4ef633fb..c616cc91 100644 --- a/lib/PayPal/Api/ReportingTransactions.php +++ b/lib/PayPal/Api/ReportingTransactions.php @@ -7,6 +7,7 @@ use PayPal\Validation\ArgumentValidator; use PayPal\Rest\ApiContext; + /** * Class ReportingTransactions * @@ -17,8 +18,6 @@ */ class ReportingTransactions extends PayPalResourceModel { - protected $startDate; - protected $endDate; protected $transactionDetails = array(); protected $totalItems; @@ -45,6 +44,7 @@ public function setTotalItems($totalItems) * @return \PayPal\Api\ReportingTransactionDetails[] */ public function getTransactionDetails() + { return $this->transactionDetails; } @@ -58,7 +58,21 @@ public function setTransactionDetails($transactionDetails) $this->transactionDetails = array_merge($this->transactionDetails, $transactionDetails); } - public static function get($params, $apiContext, $restCall = null) { + /** + * Lists transactions. Specify one or more query parameters to filter the transaction that appear in the response. + * + * @param array $params + * Query parameters. + * @param \PayPal\Rest\ApiContext $apiContext + * Api context. + * @param \PayPal\Transport\PayPalRestCall $restCall + * Rest call. + * + * @return \PayPal\Api\ReportingTransactions + * Reporting transactions response. + */ + public static function get($params, $apiContext, $restCall = null) + { ArgumentValidator::validate($params, 'params'); $payLoad = ""; $allowedParams = array( @@ -87,8 +101,8 @@ public static function get($params, $apiContext, $restCall = null) { return $reporting_transactions; } - public static function all($params, $apiContext) { - + public static function all($params, $apiContext) + { } } From 7eb4dee541e010f4b14da969cdaa6992873607a1 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 09:42:25 +0200 Subject: [PATCH 13/17] Reporting transactions: method to get all transactions. --- lib/PayPal/Api/ReportingTransactions.php | 69 ++++++++++++++++++++---- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/lib/PayPal/Api/ReportingTransactions.php b/lib/PayPal/Api/ReportingTransactions.php index c616cc91..da067311 100644 --- a/lib/PayPal/Api/ReportingTransactions.php +++ b/lib/PayPal/Api/ReportingTransactions.php @@ -7,7 +7,6 @@ use PayPal\Validation\ArgumentValidator; use PayPal\Rest\ApiContext; - /** * Class ReportingTransactions * @@ -18,7 +17,6 @@ */ class ReportingTransactions extends PayPalResourceModel { - protected $transactionDetails = array(); protected $totalItems; @@ -59,7 +57,9 @@ public function setTransactionDetails($transactionDetails) } /** - * Lists transactions. Specify one or more query parameters to filter the transaction that appear in the response. + * Main request to get the transactions. + * + * Api endpoint: /v1/reporting/transactions. * * @param array $params * Query parameters. @@ -68,10 +68,10 @@ public function setTransactionDetails($transactionDetails) * @param \PayPal\Transport\PayPalRestCall $restCall * Rest call. * - * @return \PayPal\Api\ReportingTransactions - * Reporting transactions response. + * @return string + * API response. */ - public static function get($params, $apiContext, $restCall = null) + protected static function requestTransactions($params, $apiContext, $restCall = null) { ArgumentValidator::validate($params, 'params'); $payLoad = ""; @@ -88,7 +88,7 @@ public static function get($params, $apiContext, $restCall = null) 'page_size' => 1, 'page' => 1, ); - $json = self::executeCall( + return self::executeCall( "/v1/reporting/transactions?" . http_build_query(array_intersect_key($params, $allowedParams)), "GET", $payLoad, @@ -96,13 +96,64 @@ public static function get($params, $apiContext, $restCall = null) $apiContext, $restCall ); + } + + /** + * Lists transactions. Specify one or more query parameters to filter the transaction that appear in the response. + * + * @param array $params + * Query parameters. + * @param \PayPal\Rest\ApiContext $apiContext + * Api context. + * @param \PayPal\Transport\PayPalRestCall $restCall + * Rest call. + * + * @return \PayPal\Api\ReportingTransactions + * Reporting transactions response. + */ + public static function get($params, $apiContext, $restCall = null) + { + $json = self::requestTransactions($params, $apiContext, $restCall); $reporting_transactions = new ReportingTransactions(); $reporting_transactions->fromJson($json); return $reporting_transactions; } - public static function all($params, $apiContext) + /** + * Lists all transactions, making iterating each page. + * + * @param array $params + * Query parameters. + * @param \PayPal\Rest\ApiContext $apiContext + * Api context. + * @param \PayPal\Transport\PayPalRestCall $restCall + * Rest call. + * + * @return \PayPal\Api\ReportingTransactions + * Reporting transactions response. + */ + public static function all($params, $apiContext, $restCall = null) { + $completed = false; + $params['page'] = 1; + $reporting_transactions = null; + while (!$completed) { + $json = self::requestTransactions($params, $apiContext, $restCall); + if (empty($reporting_transactions)) { + $reporting_transactions = new ReportingTransactions(); + $reporting_transactions->fromJson($json); + } else { + $json_decode = json_decode($json); + $transaction_details = array_map(function ($transaction_detail) { + $reporting_transaction_detail = new ReportingTransactionDetails(); + $reporting_transaction_detail->fromJson(json_encode($transaction_detail)); + return $reporting_transaction_detail; + }, $json_decode->transaction_details); + $reporting_transactions->setTransactionDetails($transaction_details); + } + $completed = $reporting_transactions->getTotalItems() == count($reporting_transactions->getTransactionDetails()); + $params['page']++; + } + return $reporting_transactions; } - } From 2e84b091994269d577f95932e2ce5c25e26143e7 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 09:42:48 +0200 Subject: [PATCH 14/17] List transactions sample: set page size parameter, it was set a bad one. --- sample/reporting-transactions/ListTransactions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/reporting-transactions/ListTransactions.php b/sample/reporting-transactions/ListTransactions.php index 5fe6f52d..a0ebd392 100644 --- a/sample/reporting-transactions/ListTransactions.php +++ b/sample/reporting-transactions/ListTransactions.php @@ -11,7 +11,7 @@ // Refer the method doc for valid values for keys // (See bootstrap.php for more on `ApiContext`) try { - $params = array('count' => 100, 'start_date' => '2019-03-20T00:00:00-0700', 'end_date' => '2019-04-10T23:59:59-0700', 'fields' => 'all', 'page' => 1); + $params = array('page_size' => 100, 'start_date' => '2019-03-20T00:00:00-0700', 'end_date' => '2019-04-10T23:59:59-0700', 'fields' => 'all', 'page' => 1); $response = ReportingTransactions::get($params, $apiContext); } catch (Exception $ex) { From a365e7c5faa0fa15e5e695aa0dccfd74c77597e8 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 09:43:15 +0200 Subject: [PATCH 15/17] Reporting transaction test coding style. --- tests/PayPal/Test/Api/ReportingTransactionsTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/PayPal/Test/Api/ReportingTransactionsTest.php b/tests/PayPal/Test/Api/ReportingTransactionsTest.php index 0b88b968..950d45e6 100644 --- a/tests/PayPal/Test/Api/ReportingTransactionsTest.php +++ b/tests/PayPal/Test/Api/ReportingTransactionsTest.php @@ -54,6 +54,12 @@ public function testGet($obj, $mockApiContext) $this->assertEquals($transaction_info->getPaypalAccountId(), '1234'); } + /** + * Get the mock json for paypal sync api response. + * + * @return string + * Json encoded. + */ public static function getJson() { return '{"transaction_details":[{"transaction_info":{"paypal_account_id":"1234","transaction_id":"61041S","transaction_event_code":"T0007","transaction_initiation_date":"2019-04-03T11:33:04+0000","transaction_updated_date":"2019-04-03T11:33:04+0000","transaction_amount":{"currency_code":"EUR","value":"50.00"},"fee_amount":{"currency_code":"EUR","value":"-42.31"},"transaction_status":"S","ending_balance":{"currency_code":"EUR","value":"50.00"},"available_balance":{"currency_code":"EUR","value":"50.00"},"invoice_id":"22892-1554291060","protection_eligibility":"01"},"payer_info":{"account_id":"1234","email_address":"debug@example.com","address_status":"N","payer_status":"Y","payer_name":{"given_name":"test","surname":"buyer","alternate_full_name":"test buyer"},"country_code":"ES"},"shipping_info":{"name":"test, buyer"},"cart_info":{"item_details":[{"item_name":"Example","item_quantity":"1","item_unit_price":{"currency_code":"EUR","value":"50.00"},"item_amount":{"currency_code":"EUR","value":"50.00"},"total_item_amount":{"currency_code":"EUR","value":"50.00"},"invoice_number":"22892-1554291060","checkout_options":[{"checkout_option_name":"Product quantity","checkout_option_value":"1"}]}]},"store_info":{},"auction_info":{},"incentive_info":{}}],"account_number":"12345Y","start_date":"2019-03-20T07:00:00+0000","end_date":"2019-04-04T08:59:59+0000","last_refreshed_datetime":"2019-04-04T08:59:59+0000","page":1,"total_items":2,"total_pages":1,"links":[{"href":"https://api.sandbox.paypal.com/v1/reporting/transactions?start_date=2019-03-20T00%3A00%3A00-0700&end_date=2019-04-10T23%3A59%3A59-0700&fields=all&page_size=100&page=1","rel":"self","method":"GET"}]}'; @@ -68,6 +74,12 @@ public static function getObject() return new ReportingTransactions(self::getJson()); } + /** + * Data provider to get the mocked http client. + * + * @return array + * Data provider parameters. + */ public function mockProvider() { $obj = self::getObject(); From 8ce79d937132c1e08f480a1ee5cf8e433d82f1e8 Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 09:43:25 +0200 Subject: [PATCH 16/17] Reporting transaction test for all method. --- .../Test/Api/ReportingTransactionsTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/PayPal/Test/Api/ReportingTransactionsTest.php b/tests/PayPal/Test/Api/ReportingTransactionsTest.php index 950d45e6..7cfd6efc 100644 --- a/tests/PayPal/Test/Api/ReportingTransactionsTest.php +++ b/tests/PayPal/Test/Api/ReportingTransactionsTest.php @@ -9,6 +9,7 @@ class ReportingTransactionsTest extends TestCase { + /** * @dataProvider mockProvider * @param Payment $obj @@ -29,6 +30,42 @@ public function testGet($obj, $mockApiContext) /** @var \PayPal\Api\ReportingTransactions $transactions */ $transactions = $obj->get($params, $mockApiContext, $mockPPRestCall); + $this->assertTransactionValuesAreCorrect($transactions); + } + + /** + * @dataProvider mockProvider + * @param Payment $obj + */ + public function testAll($obj, $mockApiContext) + { + $mockPPRestCall = $this->getMockBuilder('\PayPal\Transport\PayPalRestCall') + ->disableOriginalConstructor() + ->getMock(); + + $mockPPRestCall->expects($this->any()) + ->method('execute') + ->will($this->returnValue( + ReportingTransactionsTest::getJson() + )); + + $params = array('count' => 100, 'start_date' => '2019-03-20T00:00:00-0700', 'end_date' => '2019-04-10T23:59:59-0700', 'fields' => 'all', 'page' => 1); + + /** @var \PayPal\Api\ReportingTransactions $transactions */ + $transactions = $obj->get($params, $mockApiContext, $mockPPRestCall); + $this->assertTransactionValuesAreCorrect($transactions); + } + + /** + * Make all assertions for the reporting transactions response. + * + * Used for ::get and ::all. + * + * @param \PayPal\Api\ReportingTransactions $transactions + * Transactions. + */ + public function assertTransactionValuesAreCorrect($transactions) + { $this->assertInstanceOf('\PayPal\Api\ReportingTransactions', $transactions); $this->assertEquals(2, $transactions->getTotalItems()); $transaction_details = $transactions->getTransactionDetails(); From b4dddd580b05fd1f000aa4e1a56afdfb789b732e Mon Sep 17 00:00:00 2001 From: Omar Mohamad - El Hassan Lopesino Date: Fri, 5 Apr 2019 09:43:57 +0200 Subject: [PATCH 17/17] Example to list all transactions with paypal sync api. --- .../ListTransactionsAll.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 sample/reporting-transactions/ListTransactionsAll.php diff --git a/sample/reporting-transactions/ListTransactionsAll.php b/sample/reporting-transactions/ListTransactionsAll.php new file mode 100644 index 00000000..d3de1112 --- /dev/null +++ b/sample/reporting-transactions/ListTransactionsAll.php @@ -0,0 +1,23 @@ + 1, 'start_date' => '2019-03-20T00:00:00-0700', 'end_date' => '2019-04-10T23:59:59-0700', 'fields' => 'all'); + + $response = ReportingTransactions::all($params, $apiContext); +} catch (Exception $ex) { + // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY + ResultPrinter::printError("List Payments", "Payment", null, $params, $ex); + exit(1); +} + +// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY +ResultPrinter::printResult("List transactions", "Payment", null, $params, $response); +ResultPrinter::printResult("Transaction details", "Payment", null, $params, $response->getTransactionDetails());