From 58be8bd5d3d0cb6f01644eeafd4a046d99c9f76b Mon Sep 17 00:00:00 2001 From: Chaitanya Kuber Date: Tue, 3 Feb 2015 12:19:34 +1100 Subject: [PATCH] added transaction fee support to sale class --- lib/PayPal/Api/Sale.php | 111 +++++++++++-------- lib/PayPal/Api/TransactionFee.php | 70 ++++++++++++ tests/PayPal/Test/Api/SaleTest.php | 22 ++-- tests/PayPal/Test/Api/TransactionFeeTest.php | 18 +++ 4 files changed, 164 insertions(+), 57 deletions(-) create mode 100644 lib/PayPal/Api/TransactionFee.php create mode 100644 tests/PayPal/Test/Api/TransactionFeeTest.php diff --git a/lib/PayPal/Api/Sale.php b/lib/PayPal/Api/Sale.php index fefeee6..9fa5392 100644 --- a/lib/PayPal/Api/Sale.php +++ b/lib/PayPal/Api/Sale.php @@ -2,9 +2,9 @@ namespace PayPal\Api; +use PayPal\Api\Refund; use PayPal\Common\PayPalResourceModel; use PayPal\Rest\ApiContext; -use PayPal\Api\Refund; use PayPal\Transport\PayPalRestCall; use PayPal\Validation\ArgumentValidator; @@ -13,7 +13,6 @@ use PayPal\Validation\ArgumentValidator; * * A sale transaction. * - * @package PayPal\Api * * @property string id * @property string create_time @@ -27,15 +26,31 @@ use PayPal\Validation\ArgumentValidator; * @property string protection_eligibility_type * @property string clearing_time * @property string parent_payment + * @property \PayPal\Api\TransactionFee $transactionFee + * @package PayPal\Api */ class Sale extends PayPalResourceModel { + public function setTransactionFee(\PayPal\Api\TransactionFee $transactionFee) + { + $this->transactionFee = $transactionFee; + return $this; + } + + /** + * @return \PayPal\Api\TransactionFee + */ + public function getTransactionFee() + { + return $this->transactionFee; + } + /** * Identifier of the authorization transaction. - * * - * @param string $id - * + * + * + * @param string $id * @return $this */ public function setId($id) @@ -56,10 +71,10 @@ class Sale extends PayPalResourceModel /** * Time the resource was created. - * * - * @param string $create_time - * + * + * + * @param string $create_time * @return $this */ public function setCreateTime($create_time) @@ -80,10 +95,10 @@ class Sale extends PayPalResourceModel /** * Time the resource was last updated. - * * - * @param string $update_time - * + * + * + * @param string $update_time * @return $this */ public function setUpdateTime($update_time) @@ -104,10 +119,10 @@ class Sale extends PayPalResourceModel /** * Amount being collected. - * * - * @param \PayPal\Api\Amount $amount - * + * + * + * @param \PayPal\Api\Amount $amount * @return $this */ public function setAmount($amount) @@ -128,10 +143,10 @@ class Sale extends PayPalResourceModel /** * specifies payment mode of the transaction - * Valid Values: ["INSTANT_TRANSFER", "MANUAL_BANK_TRANSFER", "DELAYED_TRANSFER", "ECHECK"] + * Valid Values: ["INSTANT_TRANSFER", "MANUAL_BANK_TRANSFER", "DELAYED_TRANSFER", "ECHECK"] * - * @param string $payment_mode - * + * + * @param string $payment_mode * @return $this */ public function setPaymentMode($payment_mode) @@ -152,10 +167,10 @@ class Sale extends PayPalResourceModel /** * Reason of Pending transaction. - * * - * @param string $pending_reason - * + * + * + * @param string $pending_reason * @return $this */ public function setPendingReason($pending_reason) @@ -176,10 +191,10 @@ class Sale extends PayPalResourceModel /** * State of the sale transaction. - * Valid Values: ["pending", "completed", "refunded", "partially_refunded"] + * Valid Values: ["pending", "completed", "refunded", "partially_refunded"] * - * @param string $state - * + * + * @param string $state * @return $this */ public function setState($state) @@ -200,10 +215,10 @@ class Sale extends PayPalResourceModel /** * Reason code for the transaction state being Pending or Reversed. - * Valid Values: ["CHARGEBACK", "GUARANTEE", "BUYER_COMPLAINT", "REFUND", "UNCONFIRMED_SHIPPING_ADDRESS", "ECHECK", "INTERNATIONAL_WITHDRAWAL", "RECEIVING_PREFERENCE_MANDATES_MANUAL_ACTION", "PAYMENT_REVIEW", "REGULATORY_REVIEW", "UNILATERAL", "VERIFICATION_REQUIRED"] + * Valid Values: ["CHARGEBACK", "GUARANTEE", "BUYER_COMPLAINT", "REFUND", "UNCONFIRMED_SHIPPING_ADDRESS", "ECHECK", "INTERNATIONAL_WITHDRAWAL", "RECEIVING_PREFERENCE_MANDATES_MANUAL_ACTION", "PAYMENT_REVIEW", "REGULATORY_REVIEW", "UNILATERAL", "VERIFICATION_REQUIRED"] * - * @param string $reason_code - * + * + * @param string $reason_code * @return $this */ public function setReasonCode($reason_code) @@ -223,11 +238,11 @@ class Sale extends PayPalResourceModel } /** - * Protection Eligibility of the Payer - * Valid Values: ["ELIGIBLE", "PARTIALLY_ELIGIBLE", "INELIGIBLE"] + * Protection Eligibility of the Payer + * Valid Values: ["ELIGIBLE", "PARTIALLY_ELIGIBLE", "INELIGIBLE"] * - * @param string $protection_eligibility - * + * + * @param string $protection_eligibility * @return $this */ public function setProtectionEligibility($protection_eligibility) @@ -237,7 +252,7 @@ class Sale extends PayPalResourceModel } /** - * Protection Eligibility of the Payer + * Protection Eligibility of the Payer * * @return string */ @@ -247,11 +262,11 @@ class Sale extends PayPalResourceModel } /** - * Protection Eligibility Type of the Payer - * Valid Values: ["ELIGIBLE", "ITEM_NOT_RECEIVED_ELIGIBLE", "INELIGIBLE", "UNAUTHORIZED_PAYMENT_ELIGIBLE"] + * Protection Eligibility Type of the Payer + * Valid Values: ["ELIGIBLE", "ITEM_NOT_RECEIVED_ELIGIBLE", "INELIGIBLE", "UNAUTHORIZED_PAYMENT_ELIGIBLE"] * - * @param string $protection_eligibility_type - * + * + * @param string $protection_eligibility_type * @return $this */ public function setProtectionEligibilityType($protection_eligibility_type) @@ -261,7 +276,7 @@ class Sale extends PayPalResourceModel } /** - * Protection Eligibility Type of the Payer + * Protection Eligibility Type of the Payer * * @return string */ @@ -272,10 +287,10 @@ class Sale extends PayPalResourceModel /** * Expected clearing time for eCheck Transactions - * * - * @param string $clearing_time - * + * + * + * @param string $clearing_time * @return $this */ public function setClearingTime($clearing_time) @@ -296,10 +311,10 @@ class Sale extends PayPalResourceModel /** * ID of the Payment resource that this transaction is based on. - * * - * @param string $parent_payment - * + * + * + * @param string $parent_payment * @return $this */ public function setParentPayment($parent_payment) @@ -321,9 +336,9 @@ class Sale extends PayPalResourceModel /** * Obtain the Sale transaction resource for the given identifier. * - * @param string $saleId - * @param \PayPal\Rest\ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials. - * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls + * @param string $saleId + * @param \PayPal\Rest\ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials. + * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls * @return Sale */ public static function get($saleId, $apiContext = null, $restCall = null) @@ -347,9 +362,9 @@ class Sale extends PayPalResourceModel /** * Creates (and processes) a new Refund Transaction added as a related resource. * - * @param Refund $refund - * @param \PayPal\Rest\ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials. - * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls + * @param Refund $refund + * @param \PayPal\Rest\ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials. + * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls * @return Refund */ public function refund($refund, $apiContext = null, $restCall = null) diff --git a/lib/PayPal/Api/TransactionFee.php b/lib/PayPal/Api/TransactionFee.php new file mode 100644 index 0000000..6c3583e --- /dev/null +++ b/lib/PayPal/Api/TransactionFee.php @@ -0,0 +1,70 @@ +currency = $currency; + return $this; + } + + /** + * 3 letter currency code + * + * @return string + */ + public function getCurrency() + { + return $this->currency; + } + + /** + * Total amount charged from the Payer account (or card) to Payee. In case of a refund, this is the refunded amount to the original Payer from Payee account. + * + * + * @param string|double $value + * + * @return $this + */ + public function setValue($value) + { + NumericValidator::validate($value, "value"); + $value = FormatConverter::formatToPrice($value, $this->getCurrency()); + $this->value = $value; + return $this; + } + + /** + * Value amount charged from the Payer account (or card) to Payee. In case of a refund, this is the refunded amount to the original Payer from Payee account. + * + * @return string + */ + public function getValue() + { + return $this->value; + } +} diff --git a/tests/PayPal/Test/Api/SaleTest.php b/tests/PayPal/Test/Api/SaleTest.php index 79972e0..b4792c5 100644 --- a/tests/PayPal/Test/Api/SaleTest.php +++ b/tests/PayPal/Test/Api/SaleTest.php @@ -1,19 +1,16 @@ setAmount(AmountTest::createAmount()); @@ -29,12 +26,18 @@ class SaleTest extends \PHPUnit_Framework_TestCase $sale->setId(self::$id); $sale->setParentPayment(self::$parentPayment); $sale->setState(self::$state); + + $this->tFee = new \PayPal\Api\TransactionFee(); + $this->tFee->setCurrency('AUD'); + $this->tFee->setValue('0.10'); + + $sale->setTransactionFee($this->tFee); return $sale; } public function setup() { - $this->sale = self::createSale(); + $this->sale = $this->createSale(); } public function testGetterSetter() @@ -44,6 +47,7 @@ class SaleTest extends \PHPUnit_Framework_TestCase $this->assertEquals(self::$parentPayment, $this->sale->getParentPayment()); $this->assertEquals(self::$state, $this->sale->getState()); $this->assertEquals(AmountTest::$currency, $this->sale->getAmount()->getCurrency()); + $this->assertEquals($this->tFee, $this->sale->getTransactionFee()); } public function testSerializeDeserialize() diff --git a/tests/PayPal/Test/Api/TransactionFeeTest.php b/tests/PayPal/Test/Api/TransactionFeeTest.php new file mode 100644 index 0000000..ba62189 --- /dev/null +++ b/tests/PayPal/Test/Api/TransactionFeeTest.php @@ -0,0 +1,18 @@ +setCurrency('AUD'); + $this->assertEquals('AUD', $tfee->getCurrency()); + + $tfee->setValue('0.10'); + $this->assertEquals('0.10', $tfee->getValue()); + } +}