2 namespace Civi\Payment
;
4 use InvalidArgumentException
;
6 use CRM_Core_PseudoConstant
;
11 * This class provides getters and setters for arguments needed by CRM_Core_Payment methods.
13 * The setters know how to validate each setting that they are responsible for.
15 * Different methods need different settings and the concept is that by passing
16 * in a property bag we can encapsulate the params needed for a particular
17 * method call, rather than setting arguments for different methods on the main
18 * CRM_Core_Payment object.
20 * This class is also supposed to help with transition away from array key naming nightmares.
23 class PropertyBag
implements \ArrayAccess
{
25 protected $props = ['default' => []];
27 protected static $propMap = [
29 'billingStreetAddress' => TRUE,
30 'billingSupplementalAddress1' => TRUE,
31 'billingSupplementalAddress2' => TRUE,
32 'billingSupplementalAddress3' => TRUE,
33 'billingCity' => TRUE,
34 'billingPostalCode' => TRUE,
35 'billingCounty' => TRUE,
36 'billingCountry' => TRUE,
38 'contact_id' => 'contactID',
39 'contributionID' => TRUE,
40 'contribution_id' => 'contributionID',
41 'contributionRecurID' => TRUE,
42 'contribution_recur_id' => 'contributionRecurID',
44 'currencyID' => 'currency',
45 'description' => TRUE,
48 'fee_amount' => 'feeAmount',
49 'first_name' => 'firstName',
52 'invoice_id' => 'invoiceID',
53 'isBackOffice' => TRUE,
54 'is_back_office' => 'isBackOffice',
56 'is_recur' => 'isRecur',
57 'last_name' => 'lastName',
59 'paymentToken' => TRUE,
60 'payment_token' => 'paymentToken',
62 'recurFrequencyInterval' => TRUE,
63 'frequency_interval' => 'recurFrequencyInterval',
64 'recurFrequencyUnit' => TRUE,
65 'frequency_unit' => 'recurFrequencyUnit',
66 'recurInstallments' => TRUE,
67 'installments' => 'recurInstallments',
68 'subscriptionId' => 'recurProcessorID',
69 'recurProcessorID' => TRUE,
70 'transactionID' => TRUE,
71 'transaction_id' => 'transactionID',
72 'trxnResultCode' => TRUE,
73 'isNotifyProcessorOnCancelRecur' => TRUE,
79 * Temporary, internal variable to help ease transition to PropertyBag.
80 * Used by cast() to suppress legacy warnings.
82 protected $suppressLegacyWarnings = FALSE;
85 * Get the property bag.
87 * This allows us to swap a 'might be an array might be a property bag'
88 * variable for a known PropertyBag.
90 * @param \Civi\Payment\PropertyBag|array $params
92 * @return \Civi\Payment\PropertyBag
94 public static function cast($params) {
95 if ($params instanceof self
) {
98 $propertyBag = new self();
99 $propertyBag->mergeLegacyInputParams($params);
104 * Just for unit testing.
111 * Implements ArrayAccess::offsetExists
113 * @param mixed $offset
114 * @return bool TRUE if we have that value (on our default store)
116 public function offsetExists ($offset): bool {
117 $prop = $this->handleLegacyPropNames($offset, TRUE);
118 // If there's no prop, assume it's a custom property.
119 $prop = $prop ??
$offset;
120 return array_key_exists($prop, $this->props
['default']);
124 * Implements ArrayAccess::offsetGet
126 * @param mixed $offset
129 public function offsetGet($offset) {
131 $prop = $this->handleLegacyPropNames($offset);
133 catch (InvalidArgumentException
$e) {
135 CRM_Core_Error
::deprecatedFunctionWarning(
136 "proper getCustomProperty('$offset') for non-core properties. "
138 "PropertyBag array access to get '$offset'"
142 return $this->getCustomProperty($offset, 'default');
144 catch (BadMethodCallException
$e) {
145 CRM_Core_Error
::deprecatedFunctionWarning(
146 "proper setCustomProperty('$offset', \$value) to store the value (since it is not a core value), then access it with getCustomProperty('$offset'). NULL is returned but in future an exception will be thrown."
148 "PropertyBag array access to get unset property '$offset'"
154 CRM_Core_Error
::deprecatedFunctionWarning(
155 "get" . ucfirst($offset) . "()",
156 "PropertyBag array access for core property '$offset'"
158 return $this->get($prop, 'default');
162 * Implements ArrayAccess::offsetSet
164 * @param mixed $offset
165 * @param mixed $value
167 public function offsetSet($offset, $value) {
169 $prop = $this->handleLegacyPropNames($offset);
171 catch (InvalidArgumentException
$e) {
172 // We need to make a lot of noise here, we're being asked to merge in
173 // something we can't validate because we don't know what this property is.
174 // This is fine if it's something particular to a payment processor
175 // (which should be using setCustomProperty) however it could also lead to
176 // things like 'my_weirly_named_contact_id'.
178 // From 5.28 we suppress this when using PropertyBag::cast() to ease transition.
179 if (!$this->suppressLegacyWarnings
) {
180 CRM_Core_Error
::deprecatedFunctionWarning(
181 "proper setCustomProperty('$offset', \$value) for non-core properties. "
183 "PropertyBag array access to set '$offset'"
186 $this->setCustomProperty($offset, $value, 'default');
190 // Coerce legacy values that were in use but shouldn't be in our new way of doing things.
191 if ($prop === 'feeAmount' && $value === '') {
192 // At least the following classes pass in ZLS for feeAmount.
193 // CRM_Core_Payment_AuthorizeNetTest::testCreateSingleNowDated
194 // CRM_Core_Payment_AuthorizeNetTest::testCreateSinglePostDated
198 // These lines are here (and not in try block) because the catch must only
199 // catch the case when the prop is custom.
200 $setter = 'set' . ucfirst($prop);
201 if (!$this->suppressLegacyWarnings
) {
202 CRM_Core_Error
::deprecatedFunctionWarning(
204 "PropertyBag array access to set core property '$offset'"
207 $this->$setter($value, 'default');
211 * Implements ArrayAccess::offsetUnset
213 * @param mixed $offset
215 public function offsetUnset ($offset) {
216 $prop = $this->handleLegacyPropNames($offset);
217 unset($this->props
['default'][$prop]);
221 * @param string $prop
222 * @param bool $silent if TRUE return NULL instead of throwing an exception. This is because offsetExists should be safe and not throw exceptions.
223 * @return string canonical name.
224 * @throws \InvalidArgumentException if prop name not known.
226 protected function handleLegacyPropNames($prop, $silent = FALSE) {
227 $newName = static::$propMap[$prop] ??
NULL;
228 if ($newName === TRUE) {
229 // Good, modern name.
232 // Handling for legacy addition of billing details.
233 if ($newName === NULL && substr($prop, -2) === '-' . \CRM_Core_BAO_LocationType
::getBilling()
234 && isset(static::$propMap[substr($prop, 0, -2)])
236 $newName = substr($prop, 0, -2);
239 if ($newName === NULL) {
241 // Only for use by offsetExists
244 throw new \
InvalidArgumentException("Unknown property '$prop'.");
246 // Remaining case is legacy name that's been translated.
247 if (!$this->suppressLegacyWarnings
) {
248 CRM_Core_Error
::deprecatedFunctionWarning("Canonical property name '$newName'", "Legacy property name '$prop'");
257 * @param mixed $prop Valid property name
258 * @param string $label e.g. 'default'
262 protected function get($prop, $label) {
263 if (array_key_exists($prop, $this->props
[$label] ??
[])) {
264 return $this->props
[$label][$prop];
266 throw new \
BadMethodCallException("Property '$prop' has not been set.");
272 * @param mixed $prop Valid property name
273 * @param string $label e.g. 'default'
274 * @param mixed $value
276 * @return PropertyBag $this object so you can chain set setters.
278 protected function set($prop, $label = 'default', $value) {
279 $this->props
[$label][$prop] = $value;
286 protected function coercePseudoConstantStringToInt(string $baoName, string $field, $input) {
287 if (is_numeric($input)) {
288 // We've been given a numeric ID.
291 elseif (is_string($input)) {
292 // We've been given a named instrument.
293 $_ = (int) CRM_Core_PseudoConstant
::getKey($baoName, $field, $input);
296 throw new InvalidArgumentException("Expected an integer ID or a String name for $field.");
299 throw new InvalidArgumentException("Expected an integer greater than 0 for $field.");
306 public function has($prop, $label = 'default') {
307 // We do NOT translate legacy prop names since only new code should be
308 // using this method, and new code should be using canonical names.
309 // $prop = $this->handleLegacyPropNames($prop);
310 return isset($this->props
[$label][$prop]);
314 * This is used to merge values from an array.
315 * It's a transitional, internal function and should not be used!
319 public function mergeLegacyInputParams($data) {
320 // Suppress legacy warnings for merging an array of data as this
321 // suits our migration plan at this moment. Future behaviour may differ.
322 // @see https://github.com/civicrm/civicrm-core/pull/17643
323 $this->suppressLegacyWarnings
= TRUE;
324 foreach ($data as $key => $value) {
325 if ($value !== NULL && $value !== '') {
326 $this->offsetSet($key, $value);
329 $this->suppressLegacyWarnings
= FALSE;
333 * Throw an exception if any of the props is unset.
335 * @param array $props Array of proper property names (no legacy aliases allowed).
337 * @return PropertyBag
339 public function require(array $props) {
341 foreach ($props as $prop) {
342 if (!isset($this->props
['default'][$prop])) {
347 throw new \
InvalidArgumentException("Required properties missing: " . implode(', ', $missing));
352 // Public getters, setters.
355 * Get a property by its name (but still using its getter).
357 * @param string $prop valid property name, like contactID
358 * @param bool $allowUnset If TRUE, return the default value if the property is
359 * not set - normal behaviour would be to throw an exception.
360 * @param mixed $default
361 * @param string $label e.g. 'default' or 'old' or 'new'
365 public function getter($prop, $allowUnset = FALSE, $default = NULL, $label = 'default') {
367 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
368 // This is a standard property that will have a getter method.
369 $getter = 'get' . ucfirst($prop);
370 return (!$allowUnset ||
$this->has($prop, $label))
371 ?
$this->$getter($label)
375 // This is not a property name we know, but they could be requesting a
377 return (!$allowUnset ||
$this->has($prop, $label))
378 ?
$this->getCustomProperty($prop, $label)
383 * Set a property by its name (but still using its setter).
385 * @param string $prop valid property name, like contactID
386 * @param mixed $value
387 * @param string $label e.g. 'default' or 'old' or 'new'
391 public function setter($prop, $value = NULL, $label = 'default') {
392 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
393 // This is a standard property.
394 $setter = 'set' . ucfirst($prop);
395 return $this->$setter($value, $label);
397 // We don't allow using the setter for custom properties.
398 throw new \
BadMethodCallException("Cannot use generic setter with non-standard properties; you must use setCustomProperty for custom properties.");
402 * Get the monetary amount.
404 public function getAmount($label = 'default') {
405 return $this->get('amount', $label);
409 * Set the monetary amount.
411 * - We expect to be called with a string amount with optional decimals using
412 * a '.' as the decimal point (not a ',').
414 * - We're ok with floats/ints being passed in, too, but we'll cast them to a
417 * - Negatives are fine.
419 * @see https://github.com/civicrm/civicrm-core/pull/18219
421 * @param string|float|int $value
422 * @param string $label
424 public function setAmount($value, $label = 'default') {
425 if (!is_numeric($value)) {
426 throw new \
InvalidArgumentException("setAmount requires a numeric amount value");
428 return $this->set('amount', $label, filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT
, FILTER_FLAG_ALLOW_FRACTION
));
432 * BillingStreetAddress getter.
436 public function getBillingStreetAddress($label = 'default') {
437 return $this->get('billingStreetAddress', $label);
441 * BillingStreetAddress setter.
443 * @param string $input
444 * @param string $label e.g. 'default'
446 public function setBillingStreetAddress($input, $label = 'default') {
447 return $this->set('billingStreetAddress', $label, (string) $input);
451 * BillingSupplementalAddress1 getter.
455 public function getBillingSupplementalAddress1($label = 'default') {
456 return $this->get('billingSupplementalAddress1', $label);
460 * BillingSupplementalAddress1 setter.
462 * @param string $input
463 * @param string $label e.g. 'default'
465 public function setBillingSupplementalAddress1($input, $label = 'default') {
466 return $this->set('billingSupplementalAddress1', $label, (string) $input);
470 * BillingSupplementalAddress2 getter.
474 public function getBillingSupplementalAddress2($label = 'default') {
475 return $this->get('billingSupplementalAddress2', $label);
479 * BillingSupplementalAddress2 setter.
481 * @param string $input
482 * @param string $label e.g. 'default'
484 public function setBillingSupplementalAddress2($input, $label = 'default') {
485 return $this->set('billingSupplementalAddress2', $label, (string) $input);
489 * BillingSupplementalAddress3 getter.
493 public function getBillingSupplementalAddress3($label = 'default') {
494 return $this->get('billingSupplementalAddress3', $label);
498 * BillingSupplementalAddress3 setter.
500 * @param string $input
501 * @param string $label e.g. 'default'
503 public function setBillingSupplementalAddress3($input, $label = 'default') {
504 return $this->set('billingSupplementalAddress3', $label, (string) $input);
508 * BillingCity getter.
512 public function getBillingCity($label = 'default') {
513 return $this->get('billingCity', $label);
517 * BillingCity setter.
519 * @param string $input
520 * @param string $label e.g. 'default'
522 * @return \Civi\Payment\PropertyBag
524 public function setBillingCity($input, $label = 'default') {
525 return $this->set('billingCity', $label, (string) $input);
529 * BillingPostalCode getter.
533 public function getBillingPostalCode($label = 'default') {
534 return $this->get('billingPostalCode', $label);
538 * BillingPostalCode setter.
540 * @param string $input
541 * @param string $label e.g. 'default'
543 public function setBillingPostalCode($input, $label = 'default') {
544 return $this->set('billingPostalCode', $label, (string) $input);
548 * BillingCounty getter.
552 public function getBillingCounty($label = 'default') {
553 return $this->get('billingCounty', $label);
557 * BillingCounty setter.
559 * Nb. we can't validate this unless we have the country ID too, so we don't.
561 * @param string $input
562 * @param string $label e.g. 'default'
564 public function setBillingCounty($input, $label = 'default') {
565 return $this->set('billingCounty', $label, (string) $input);
569 * BillingCountry getter.
573 public function getBillingCountry($label = 'default') {
574 return $this->get('billingCountry', $label);
578 * BillingCountry setter.
580 * Nb. We require and we store a 2 character country code.
582 * @param string $input
583 * @param string $label e.g. 'default'
585 public function setBillingCountry($input, $label = 'default') {
586 if (!is_string($input) ||
strlen($input) !== 2) {
587 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
589 if (!CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'country_id', $input)) {
590 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
592 return $this->set('billingCountry', $label, (string) $input);
598 public function getContactID($label = 'default'): int {
599 return $this->get('contactID', $label);
603 * @param int $contactID
604 * @param string $label
606 public function setContactID($contactID, $label = 'default') {
607 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
608 if (!($contactID > 0)) {
609 throw new InvalidArgumentException("ContactID must be a positive integer");
612 return $this->set('contactID', $label, (int) $contactID);
616 * Getter for contributionID.
619 * @param string $label
621 public function getContributionID($label = 'default') {
622 return $this->get('contributionID', $label);
626 * @param int $contributionID
627 * @param string $label e.g. 'default'
629 public function setContributionID($contributionID, $label = 'default') {
630 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
631 if (!($contributionID > 0)) {
632 throw new InvalidArgumentException("ContributionID must be a positive integer");
635 return $this->set('contributionID', $label, (int) $contributionID);
639 * Getter for contributionRecurID.
642 * @param string $label
644 public function getContributionRecurID($label = 'default') {
645 return $this->get('contributionRecurID', $label);
649 * @param int $contributionRecurID
650 * @param string $label e.g. 'default'
652 * @return \Civi\Payment\PropertyBag
654 public function setContributionRecurID($contributionRecurID, $label = 'default') {
655 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
656 if (!($contributionRecurID > 0)) {
657 throw new InvalidArgumentException('ContributionRecurID must be a positive integer');
660 return $this->set('contributionRecurID', $label, (int) $contributionRecurID);
664 * Three character currency code.
666 * https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
668 * @param string $label e.g. 'default'
670 public function getCurrency($label = 'default') {
671 return $this->get('currency', $label);
675 * Three character currency code.
677 * @param string $value
678 * @param string $label e.g. 'default'
680 public function setCurrency($value, $label = 'default') {
681 if (!preg_match('/^[A-Z]{3}$/', $value)) {
682 throw new \
InvalidArgumentException("Attemted to setCurrency with a value that was not an ISO 3166-1 alpha 3 currency code");
684 return $this->set('currency', $label, $value);
689 * @param string $label
693 public function getDescription($label = 'default') {
694 return $this->get('description', $label);
698 * @param string $description
699 * @param string $label e.g. 'default'
701 public function setDescription($description, $label = 'default') {
702 // @todo this logic was copied from a commit that then got deleted. Is it good?
703 $uninformativeStrings = [
704 ts('Online Event Registration: '),
705 ts('Online Contribution: '),
707 $cleanedDescription = str_replace($uninformativeStrings, '', $description);
708 return $this->set('description', $label, $cleanedDescription);
716 public function getEmail($label = 'default') {
717 return $this->get('email', $label);
723 * @param string $email
724 * @param string $label e.g. 'default'
726 public function setEmail($email, $label = 'default') {
727 return $this->set('email', $label, (string) $email);
731 * Amount of money charged in fees by the payment processor.
733 * This is notified by (some) payment processers.
735 * @param string $label
739 public function getFeeAmount($label = 'default') {
740 return $this->get('feeAmount', $label);
744 * @param string $feeAmount
745 * @param string $label e.g. 'default'
747 public function setFeeAmount($feeAmount, $label = 'default') {
748 if (!is_numeric($feeAmount)) {
749 throw new \
InvalidArgumentException("feeAmount must be a number.");
751 return $this->set('feeAmount', $label, (float) $feeAmount);
759 public function getFirstName($label = 'default') {
760 return $this->get('firstName', $label);
766 * @param string $firstName
767 * @param string $label e.g. 'default'
769 public function setFirstName($firstName, $label = 'default') {
770 return $this->set('firstName', $label, (string) $firstName);
774 * Getter for invoiceID.
776 * @param string $label
778 * @return string|null
780 public function getInvoiceID($label = 'default') {
781 return $this->get('invoiceID', $label);
785 * @param string $invoiceID
786 * @param string $label e.g. 'default'
788 public function setInvoiceID($invoiceID, $label = 'default') {
789 return $this->set('invoiceID', $label, $invoiceID);
793 * Getter for isBackOffice.
795 * @param string $label
799 public function getIsBackOffice($label = 'default'):bool {
800 // @todo should this return FALSE instead of exception to keep current situation?
801 return $this->get('isBackOffice', $label);
805 * @param bool $isBackOffice
806 * @param string $label e.g. 'default'
808 public function setIsBackOffice($isBackOffice, $label = 'default') {
809 if (is_null($isBackOffice)) {
810 throw new \
InvalidArgumentException("isBackOffice must be a bool, received NULL.");
812 return $this->set('isBackOffice', $label, (bool) $isBackOffice);
816 * Getter for isRecur.
818 * @param string $label
822 public function getIsRecur($label = 'default'):bool {
823 if (!$this->has('isRecur')) {
826 return $this->get('isRecur', $label);
830 * @param bool $isRecur
831 * @param string $label e.g. 'default'
833 public function setIsRecur($isRecur, $label = 'default') {
834 if (is_null($isRecur)) {
835 throw new \
InvalidArgumentException("isRecur must be a bool, received NULL.");
837 return $this->set('isRecur', $label, (bool) $isRecur);
841 * Set whether the user has selected to notify the processor of a cancellation request.
843 * When cancelling the user may be presented with an option to notify the processor. The payment
844 * processor can take their response, if present, into account.
847 * @param string $label e.g. 'default'
849 * @return \Civi\Payment\PropertyBag
851 public function setIsNotifyProcessorOnCancelRecur($value, $label = 'default') {
852 return $this->set('isNotifyProcessorOnCancelRecur', $label, (bool) $value);
856 * Get whether the user has selected to notify the processor of a cancellation request.
858 * When cancelling the user may be presented with an option to notify the processor. The payment
859 * processor can take their response, if present, into account.
861 * @param string $label e.g. 'default'
863 * @return \Civi\Payment\PropertyBag
865 public function getIsNotifyProcessorOnCancelRecur($label = 'default') {
866 return $this->get('isNotifyProcessorOnCancelRecur', $label);
874 public function getLastName($label = 'default') {
875 return $this->get('lastName', $label);
881 * @param string $lastName
882 * @param string $label e.g. 'default'
884 public function setLastName($lastName, $label = 'default') {
885 return $this->set('lastName', $label, (string) $lastName);
889 * Getter for payment processor generated string for charging.
891 * A payment token could be a single use token (e.g generated by
892 * a client side script) or a token that permits recurring or on demand charging.
894 * The key thing is it is passed to the processor in lieu of card details to
895 * initiate a payment.
897 * Generally if a processor is going to pass in a payment token generated through
898 * javascript it would add 'payment_token' to the array it returns in it's
899 * implementation of getPaymentFormFields. This will add a hidden 'payment_token' field to
900 * the form. A good example is client side encryption where credit card details are replaced by
901 * an encrypted token using a gateway provided javascript script. In this case the javascript will
902 * remove the credit card details from the form before submitting and populate the payment_token field.
904 * A more complex example is used by paypal checkout where the payment token is generated
905 * via a pre-approval process. In that case the doPreApproval function is called on the processor
906 * class to get information to then interact with paypal via js, finally getting a payment token.
907 * (at this stage the pre-approve api is not in core but that is likely to change - we just need
908 * to think about the permissions since we don't want to expose to anonymous user without thinking
909 * through any risk of credit-card testing using it.
911 * If the token is not totally transient it would be saved to civicrm_payment_token.token.
913 * @param string $label
915 * @return string|null
917 public function getPaymentToken($label = 'default') {
918 return $this->get('paymentToken', $label);
922 * @param string $paymentToken
923 * @param string $label e.g. 'default'
925 public function setPaymentToken($paymentToken, $label = 'default') {
926 return $this->set('paymentToken', $label, $paymentToken);
934 public function getPhone($label = 'default') {
935 return $this->get('phone', $label);
941 * @param string $phone
942 * @param string $label e.g. 'default'
944 public function setPhone($phone, $label = 'default') {
945 return $this->set('phone', $label, (string) $phone);
949 * Combined with recurFrequencyUnit this gives how often billing will take place.
951 * e.g every if this is 1 and recurFrequencyUnit is 'month' then it is every 1 month.
954 public function getRecurFrequencyInterval($label = 'default') {
955 return $this->get('recurFrequencyInterval', $label);
959 * @param int $recurFrequencyInterval
960 * @param string $label e.g. 'default'
962 public function setRecurFrequencyInterval($recurFrequencyInterval, $label = 'default') {
963 if (!($recurFrequencyInterval > 0)) {
964 throw new InvalidArgumentException("recurFrequencyInterval must be a positive integer");
967 return $this->set('recurFrequencyInterval', $label, (int) $recurFrequencyInterval);
971 * Getter for recurFrequencyUnit.
972 * Combined with recurFrequencyInterval this gives how often billing will take place.
974 * e.g every if this is 'month' and recurFrequencyInterval is 1 then it is every 1 month.
977 * @param string $label
979 * @return string month|day|year
981 public function getRecurFrequencyUnit($label = 'default') {
982 return $this->get('recurFrequencyUnit', $label);
986 * @param string $recurFrequencyUnit month|day|week|year
987 * @param string $label e.g. 'default'
989 public function setRecurFrequencyUnit($recurFrequencyUnit, $label = 'default') {
990 if (!preg_match('/^day|week|month|year$/', $recurFrequencyUnit)) {
991 throw new \
InvalidArgumentException("recurFrequencyUnit must be day|week|month|year");
993 return $this->set('recurFrequencyUnit', $label, $recurFrequencyUnit);
997 * @param string $label
1001 public function getRecurInstallments($label = 'default') {
1002 return $this->get('recurInstallments', $label);
1006 * @param int $recurInstallments
1007 * @param string $label
1009 * @return \Civi\Payment\PropertyBag
1010 * @throws \CRM_Core_Exception
1012 public function setRecurInstallments($recurInstallments, $label = 'default') {
1013 // Counts zero as positive which is ok - means no installments
1014 if (!\CRM_Utils_Type
::validate($recurInstallments, 'Positive')) {
1015 throw new InvalidArgumentException('recurInstallments must be 0 or a positive integer');
1018 return $this->set('recurInstallments', $label, (int) $recurInstallments);
1022 * Set the unique payment processor service provided ID for a particular subscription.
1024 * Nb. this is stored in civicrm_contribution_recur.processor_id and is NOT
1025 * in any way related to the payment processor ID.
1027 * @param string $label
1029 * @return string|null
1031 public function getRecurProcessorID($label = 'default') {
1032 return $this->get('recurProcessorID', $label);
1036 * Set the unique payment processor service provided ID for a particular
1039 * See https://github.com/civicrm/civicrm-core/pull/17292 for discussion
1040 * of how this function accepting NULL fits with standard / planned behaviour.
1042 * @param string|null $input
1043 * @param string $label e.g. 'default'
1045 * @return \Civi\Payment\PropertyBag
1047 public function setRecurProcessorID($input, $label = 'default') {
1048 if ($input === '') {
1051 if (strlen($input) > 255 ||
in_array($input, [FALSE, 0], TRUE)) {
1052 throw new \
InvalidArgumentException('processorID field has max length of 255');
1054 return $this->set('recurProcessorID', $label, $input);
1058 * Getter for payment processor generated string for the transaction ID.
1060 * Note some gateways generate a reference for the order and one for the
1061 * payment. This is for the payment reference and is saved to
1062 * civicrm_financial_trxn.trxn_id.
1064 * @param string $label
1066 * @return string|null
1068 public function getTransactionID($label = 'default') {
1069 return $this->get('transactionID', $label);
1073 * @param string $transactionID
1074 * @param string $label e.g. 'default'
1076 public function setTransactionID($transactionID, $label = 'default') {
1077 return $this->set('transactionID', $label, $transactionID);
1081 * Getter for trxnResultCode.
1083 * Additional information returned by the payment processor regarding the
1086 * This would normally be saved in civicrm_financial_trxn.trxn_result_code.
1089 * @param string $label
1091 * @return string|null
1093 public function getTrxnResultCode($label = 'default') {
1094 return $this->get('trxnResultCode', $label);
1098 * @param string $trxnResultCode
1099 * @param string $label e.g. 'default'
1101 public function setTrxnResultCode($trxnResultCode, $label = 'default') {
1102 return $this->set('trxnResultCode', $label, $trxnResultCode);
1105 // Custom Properties.
1108 * Sometimes we may need to pass in things that are specific to the Payment
1111 * @param string $prop
1112 * @param string $label e.g. 'default' or 'old' or 'new'
1115 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1117 public function getCustomProperty($prop, $label = 'default') {
1118 if (isset(static::$propMap[$prop])) {
1119 throw new \
InvalidArgumentException("Attempted to get '$prop' via getCustomProperty - must use using its getter.");
1122 if (!array_key_exists($prop, $this->props
[$label] ??
[])) {
1123 throw new \
BadMethodCallException("Property '$prop' has not been set.");
1125 return $this->props
[$label][$prop] ??
NULL;
1129 * We have to leave validation to the processor, but we can still give them a
1130 * way to store their data on this PropertyBag
1132 * @param string $prop
1133 * @param mixed $value
1134 * @param string $label e.g. 'default' or 'old' or 'new'
1136 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1138 public function setCustomProperty($prop, $value, $label = 'default') {
1139 if (isset(static::$propMap[$prop])) {
1140 throw new \
InvalidArgumentException("Attempted to set '$prop' via setCustomProperty - must use using its setter.");
1142 $this->props
[$label][$prop] = $value;