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 = [
28 'billingStreetAddress' => TRUE,
29 'billingSupplementalAddress1' => TRUE,
30 'billingSupplementalAddress2' => TRUE,
31 'billingSupplementalAddress3' => TRUE,
32 'billingCity' => TRUE,
33 'billingPostalCode' => TRUE,
34 'billingCounty' => TRUE,
35 'billingCountry' => TRUE,
37 'contact_id' => 'contactID',
38 'contributionID' => TRUE,
39 'contribution_id' => 'contributionID',
40 'contributionRecurID' => TRUE,
41 'contribution_recur_id' => 'contributionRecurID',
43 'currencyID' => 'currency',
44 'description' => TRUE,
47 'fee_amount' => 'feeAmount',
48 'first_name' => 'firstName',
51 'invoice_id' => 'invoiceID',
52 'isBackOffice' => TRUE,
53 'is_back_office' => 'isBackOffice',
55 'is_recur' => 'isRecur',
56 'last_name' => 'lastName',
58 'paymentToken' => TRUE,
59 'payment_token' => 'paymentToken',
61 'recurFrequencyInterval' => TRUE,
62 'frequency_interval' => 'recurFrequencyInterval',
63 'recurFrequencyUnit' => TRUE,
64 'frequency_unit' => 'recurFrequencyUnit',
65 'recurInstallments' => TRUE,
66 'installments' => 'recurInstallments',
67 'subscriptionId' => 'recurProcessorID',
68 'recurProcessorID' => TRUE,
69 'transactionID' => TRUE,
70 'transaction_id' => 'transactionID',
71 'trxnResultCode' => TRUE,
72 'isNotifyProcessorOnCancelRecur' => TRUE,
78 * Temporary, internal variable to help ease transition to PropertyBag.
79 * Used by cast() to suppress legacy warnings.
80 * For paymentprocessors that have not converted to propertyBag we need to support "legacy" properties - eg. "is_recur"
81 * without warnings. Setting this allows us to pass a propertyBag into doPayment() and expect it to "work" with
82 * existing payment processors.
84 protected $suppressLegacyWarnings = TRUE;
87 * Get the value of the suppressLegacyWarnings parameter
90 public function getSuppressLegacyWarnings() {
91 return $this->suppressLegacyWarnings
;
95 * Set the suppressLegacyWarnings parameter - useful for unit tests.
96 * Eg. you could set to FALSE for unit tests on a paymentprocessor to capture use of legacy keys in that processor
98 * @param bool $suppressLegacyWarnings
100 public function setSuppressLegacyWarnings(bool $suppressLegacyWarnings) {
101 $this->suppressLegacyWarnings
= $suppressLegacyWarnings;
105 * Get the property bag.
107 * This allows us to swap a 'might be an array might be a property bag'
108 * variable for a known PropertyBag.
110 * @param \Civi\Payment\PropertyBag|array $params
112 * @return \Civi\Payment\PropertyBag
114 public static function cast($params) {
115 if ($params instanceof self
) {
118 $propertyBag = new self();
119 $propertyBag->mergeLegacyInputParams($params);
124 * Just for unit testing.
131 * Implements ArrayAccess::offsetExists
133 * @param mixed $offset
134 * @return bool TRUE if we have that value (on our default store)
136 public function offsetExists ($offset): bool {
137 $prop = $this->handleLegacyPropNames($offset, TRUE);
138 // If there's no prop, assume it's a custom property.
139 $prop = $prop ??
$offset;
140 return array_key_exists($prop, $this->props
['default']);
144 * Implements ArrayAccess::offsetGet
146 * @param mixed $offset
149 public function offsetGet($offset) {
151 $prop = $this->handleLegacyPropNames($offset);
153 catch (InvalidArgumentException
$e) {
155 CRM_Core_Error
::deprecatedFunctionWarning(
156 "proper getCustomProperty('$offset') for non-core properties. "
158 "PropertyBag array access to get '$offset'"
162 return $this->getCustomProperty($offset, 'default');
164 catch (BadMethodCallException
$e) {
165 CRM_Core_Error
::deprecatedFunctionWarning(
166 "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."
168 "PropertyBag array access to get unset property '$offset'"
174 CRM_Core_Error
::deprecatedFunctionWarning(
175 "get" . ucfirst($offset) . "()",
176 "PropertyBag array access for core property '$offset'"
178 return $this->get($prop, 'default');
182 * Implements ArrayAccess::offsetSet
184 * @param mixed $offset
185 * @param mixed $value
187 public function offsetSet($offset, $value) {
189 $prop = $this->handleLegacyPropNames($offset);
191 catch (InvalidArgumentException
$e) {
192 // We need to make a lot of noise here, we're being asked to merge in
193 // something we can't validate because we don't know what this property is.
194 // This is fine if it's something particular to a payment processor
195 // (which should be using setCustomProperty) however it could also lead to
196 // things like 'my_weirly_named_contact_id'.
198 // From 5.28 we suppress this when using PropertyBag::cast() to ease transition.
199 if (!$this->suppressLegacyWarnings
) {
200 CRM_Core_Error
::deprecatedFunctionWarning(
201 "proper setCustomProperty('$offset', \$value) for non-core properties. "
203 "PropertyBag array access to set '$offset'"
206 $this->setCustomProperty($offset, $value, 'default');
210 // Coerce legacy values that were in use but shouldn't be in our new way of doing things.
211 if ($prop === 'feeAmount' && $value === '') {
212 // At least the following classes pass in ZLS for feeAmount.
213 // CRM_Core_Payment_AuthorizeNetTest::testCreateSingleNowDated
214 // CRM_Core_Payment_AuthorizeNetTest::testCreateSinglePostDated
218 // These lines are here (and not in try block) because the catch must only
219 // catch the case when the prop is custom.
220 $setter = 'set' . ucfirst($prop);
221 if (!$this->suppressLegacyWarnings
) {
222 CRM_Core_Error
::deprecatedFunctionWarning(
224 "PropertyBag array access to set core property '$offset'"
227 $this->$setter($value, 'default');
231 * Implements ArrayAccess::offsetUnset
233 * @param mixed $offset
235 public function offsetUnset ($offset) {
236 $prop = $this->handleLegacyPropNames($offset);
237 unset($this->props
['default'][$prop]);
241 * @param string $prop
242 * @param bool $silent if TRUE return NULL instead of throwing an exception. This is because offsetExists should be safe and not throw exceptions.
243 * @return string canonical name.
244 * @throws \InvalidArgumentException if prop name not known.
246 protected function handleLegacyPropNames($prop, $silent = FALSE) {
247 $newName = static::$propMap[$prop] ??
NULL;
248 if ($newName === TRUE) {
249 // Good, modern name.
252 // Handling for legacy addition of billing details.
253 if ($newName === NULL && substr($prop, -2) === '-' . \CRM_Core_BAO_LocationType
::getBilling()
254 && isset(static::$propMap[substr($prop, 0, -2)])
256 $newName = substr($prop, 0, -2);
259 if ($newName === NULL) {
261 // Only for use by offsetExists
264 throw new \
InvalidArgumentException("Unknown property '$prop'.");
266 // Remaining case is legacy name that's been translated.
267 if (!$this->suppressLegacyWarnings
) {
268 CRM_Core_Error
::deprecatedFunctionWarning("Canonical property name '$newName'", "Legacy property name '$prop'");
277 * @param mixed $prop Valid property name
278 * @param string $label e.g. 'default'
282 protected function get($prop, $label) {
283 if (array_key_exists($prop, $this->props
[$label] ??
[])) {
284 return $this->props
[$label][$prop];
286 throw new \
BadMethodCallException("Property '$prop' has not been set.");
292 * @param mixed $prop Valid property name
293 * @param string $label e.g. 'default'
294 * @param mixed $value
296 * @return PropertyBag $this object so you can chain set setters.
298 protected function set($prop, $label = 'default', $value) {
299 $this->props
[$label][$prop] = $value;
306 protected function coercePseudoConstantStringToInt(string $baoName, string $field, $input) {
307 if (is_numeric($input)) {
308 // We've been given a numeric ID.
311 elseif (is_string($input)) {
312 // We've been given a named instrument.
313 $_ = (int) CRM_Core_PseudoConstant
::getKey($baoName, $field, $input);
316 throw new InvalidArgumentException("Expected an integer ID or a String name for $field.");
319 throw new InvalidArgumentException("Expected an integer greater than 0 for $field.");
326 public function has($prop, $label = 'default') {
327 // We do NOT translate legacy prop names since only new code should be
328 // using this method, and new code should be using canonical names.
329 // $prop = $this->handleLegacyPropNames($prop);
330 return isset($this->props
[$label][$prop]);
334 * This is used to merge values from an array.
335 * It's a transitional, internal function and should not be used!
339 public function mergeLegacyInputParams($data) {
340 // Suppress legacy warnings for merging an array of data as this
341 // suits our migration plan at this moment. Future behaviour may differ.
342 // @see https://github.com/civicrm/civicrm-core/pull/17643
343 $this->suppressLegacyWarnings
= TRUE;
344 foreach ($data as $key => $value) {
345 if ($value !== NULL && $value !== '') {
346 $this->offsetSet($key, $value);
349 $this->suppressLegacyWarnings
= FALSE;
353 * Throw an exception if any of the props is unset.
355 * @param array $props Array of proper property names (no legacy aliases allowed).
357 * @return PropertyBag
359 public function require(array $props) {
361 foreach ($props as $prop) {
362 if (!isset($this->props
['default'][$prop])) {
367 throw new \
InvalidArgumentException("Required properties missing: " . implode(', ', $missing));
372 // Public getters, setters.
375 * Get a property by its name (but still using its getter).
377 * @param string $prop valid property name, like contactID
378 * @param bool $allowUnset If TRUE, return the default value if the property is
379 * not set - normal behaviour would be to throw an exception.
380 * @param mixed $default
381 * @param string $label e.g. 'default' or 'old' or 'new'
385 public function getter($prop, $allowUnset = FALSE, $default = NULL, $label = 'default') {
387 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
388 // This is a standard property that will have a getter method.
389 $getter = 'get' . ucfirst($prop);
390 return (!$allowUnset ||
$this->has($prop, $label))
391 ?
$this->$getter($label)
395 // This is not a property name we know, but they could be requesting a
397 return (!$allowUnset ||
$this->has($prop, $label))
398 ?
$this->getCustomProperty($prop, $label)
403 * Set a property by its name (but still using its setter).
405 * @param string $prop valid property name, like contactID
406 * @param mixed $value
407 * @param string $label e.g. 'default' or 'old' or 'new'
411 public function setter($prop, $value = NULL, $label = 'default') {
412 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
413 // This is a standard property.
414 $setter = 'set' . ucfirst($prop);
415 return $this->$setter($value, $label);
417 // We don't allow using the setter for custom properties.
418 throw new \
BadMethodCallException("Cannot use generic setter with non-standard properties; you must use setCustomProperty for custom properties.");
422 * Get the monetary amount.
424 public function getAmount($label = 'default') {
425 return $this->get('amount', $label);
429 * Set the monetary amount.
431 * - We expect to be called with a string amount with optional decimals using
432 * a '.' as the decimal point (not a ',').
434 * - We're ok with floats/ints being passed in, too, but we'll cast them to a
437 * - Negatives are fine.
439 * @see https://github.com/civicrm/civicrm-core/pull/18219
441 * @param string|float|int $value
442 * @param string $label
444 public function setAmount($value, $label = 'default') {
445 if (!is_numeric($value)) {
446 throw new \
InvalidArgumentException("setAmount requires a numeric amount value");
448 return $this->set('amount', $label, filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT
, FILTER_FLAG_ALLOW_FRACTION
));
452 * BillingStreetAddress getter.
456 public function getBillingStreetAddress($label = 'default') {
457 return $this->get('billingStreetAddress', $label);
461 * BillingStreetAddress setter.
463 * @param string $input
464 * @param string $label e.g. 'default'
466 public function setBillingStreetAddress($input, $label = 'default') {
467 return $this->set('billingStreetAddress', $label, (string) $input);
471 * BillingSupplementalAddress1 getter.
475 public function getBillingSupplementalAddress1($label = 'default') {
476 return $this->get('billingSupplementalAddress1', $label);
480 * BillingSupplementalAddress1 setter.
482 * @param string $input
483 * @param string $label e.g. 'default'
485 public function setBillingSupplementalAddress1($input, $label = 'default') {
486 return $this->set('billingSupplementalAddress1', $label, (string) $input);
490 * BillingSupplementalAddress2 getter.
494 public function getBillingSupplementalAddress2($label = 'default') {
495 return $this->get('billingSupplementalAddress2', $label);
499 * BillingSupplementalAddress2 setter.
501 * @param string $input
502 * @param string $label e.g. 'default'
504 public function setBillingSupplementalAddress2($input, $label = 'default') {
505 return $this->set('billingSupplementalAddress2', $label, (string) $input);
509 * BillingSupplementalAddress3 getter.
513 public function getBillingSupplementalAddress3($label = 'default') {
514 return $this->get('billingSupplementalAddress3', $label);
518 * BillingSupplementalAddress3 setter.
520 * @param string $input
521 * @param string $label e.g. 'default'
523 public function setBillingSupplementalAddress3($input, $label = 'default') {
524 return $this->set('billingSupplementalAddress3', $label, (string) $input);
528 * BillingCity getter.
532 public function getBillingCity($label = 'default') {
533 return $this->get('billingCity', $label);
537 * BillingCity setter.
539 * @param string $input
540 * @param string $label e.g. 'default'
542 * @return \Civi\Payment\PropertyBag
544 public function setBillingCity($input, $label = 'default') {
545 return $this->set('billingCity', $label, (string) $input);
549 * BillingPostalCode getter.
553 public function getBillingPostalCode($label = 'default') {
554 return $this->get('billingPostalCode', $label);
558 * BillingPostalCode setter.
560 * @param string $input
561 * @param string $label e.g. 'default'
563 public function setBillingPostalCode($input, $label = 'default') {
564 return $this->set('billingPostalCode', $label, (string) $input);
568 * BillingCounty getter.
572 public function getBillingCounty($label = 'default') {
573 return $this->get('billingCounty', $label);
577 * BillingCounty setter.
579 * Nb. we can't validate this unless we have the country ID too, so we don't.
581 * @param string $input
582 * @param string $label e.g. 'default'
584 public function setBillingCounty($input, $label = 'default') {
585 return $this->set('billingCounty', $label, (string) $input);
589 * BillingCountry getter.
593 public function getBillingCountry($label = 'default') {
594 return $this->get('billingCountry', $label);
598 * BillingCountry setter.
600 * Nb. We require and we store a 2 character country code.
602 * @param string $input
603 * @param string $label e.g. 'default'
605 public function setBillingCountry($input, $label = 'default') {
606 if (!is_string($input) ||
strlen($input) !== 2) {
607 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
609 if (!CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'country_id', $input)) {
610 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
612 return $this->set('billingCountry', $label, (string) $input);
618 public function getContactID($label = 'default'): int {
619 return $this->get('contactID', $label);
623 * @param int $contactID
624 * @param string $label
626 public function setContactID($contactID, $label = 'default') {
627 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
628 if (!($contactID > 0)) {
629 throw new InvalidArgumentException("ContactID must be a positive integer");
632 return $this->set('contactID', $label, (int) $contactID);
636 * Getter for contributionID.
639 * @param string $label
641 public function getContributionID($label = 'default') {
642 return $this->get('contributionID', $label);
646 * @param int $contributionID
647 * @param string $label e.g. 'default'
649 public function setContributionID($contributionID, $label = 'default') {
650 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
651 if (!($contributionID > 0)) {
652 throw new InvalidArgumentException("ContributionID must be a positive integer");
655 return $this->set('contributionID', $label, (int) $contributionID);
659 * Getter for contributionRecurID.
662 * @param string $label
664 public function getContributionRecurID($label = 'default') {
665 return $this->get('contributionRecurID', $label);
669 * @param int $contributionRecurID
670 * @param string $label e.g. 'default'
672 * @return \Civi\Payment\PropertyBag
674 public function setContributionRecurID($contributionRecurID, $label = 'default') {
675 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
676 if (!($contributionRecurID > 0)) {
677 throw new InvalidArgumentException('ContributionRecurID must be a positive integer');
680 return $this->set('contributionRecurID', $label, (int) $contributionRecurID);
684 * Three character currency code.
686 * https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
688 * @param string $label e.g. 'default'
690 public function getCurrency($label = 'default') {
691 return $this->get('currency', $label);
695 * Three character currency code.
697 * @param string $value
698 * @param string $label e.g. 'default'
700 public function setCurrency($value, $label = 'default') {
701 if (!preg_match('/^[A-Z]{3}$/', $value)) {
702 throw new \
InvalidArgumentException("Attemted to setCurrency with a value that was not an ISO 3166-1 alpha 3 currency code");
704 return $this->set('currency', $label, $value);
709 * @param string $label
713 public function getDescription($label = 'default') {
714 return $this->get('description', $label);
718 * @param string $description
719 * @param string $label e.g. 'default'
721 public function setDescription($description, $label = 'default') {
722 // @todo this logic was copied from a commit that then got deleted. Is it good?
723 $uninformativeStrings = [
724 ts('Online Event Registration: '),
725 ts('Online Contribution: '),
727 $cleanedDescription = str_replace($uninformativeStrings, '', $description);
728 return $this->set('description', $label, $cleanedDescription);
736 public function getEmail($label = 'default') {
737 return $this->get('email', $label);
743 * @param string $email
744 * @param string $label e.g. 'default'
746 public function setEmail($email, $label = 'default') {
747 return $this->set('email', $label, (string) $email);
751 * Amount of money charged in fees by the payment processor.
753 * This is notified by (some) payment processers.
755 * @param string $label
759 public function getFeeAmount($label = 'default') {
760 return $this->get('feeAmount', $label);
764 * @param string $feeAmount
765 * @param string $label e.g. 'default'
767 public function setFeeAmount($feeAmount, $label = 'default') {
768 if (!is_numeric($feeAmount)) {
769 throw new \
InvalidArgumentException("feeAmount must be a number.");
771 return $this->set('feeAmount', $label, (float) $feeAmount);
779 public function getFirstName($label = 'default') {
780 return $this->get('firstName', $label);
786 * @param string $firstName
787 * @param string $label e.g. 'default'
789 public function setFirstName($firstName, $label = 'default') {
790 return $this->set('firstName', $label, (string) $firstName);
794 * Getter for invoiceID.
796 * @param string $label
798 * @return string|null
800 public function getInvoiceID($label = 'default') {
801 return $this->get('invoiceID', $label);
805 * @param string $invoiceID
806 * @param string $label e.g. 'default'
808 public function setInvoiceID($invoiceID, $label = 'default') {
809 return $this->set('invoiceID', $label, $invoiceID);
813 * Getter for isBackOffice.
815 * @param string $label
819 public function getIsBackOffice($label = 'default'):bool {
820 // @todo should this return FALSE instead of exception to keep current situation?
821 return $this->get('isBackOffice', $label);
825 * @param bool $isBackOffice
826 * @param string $label e.g. 'default'
828 public function setIsBackOffice($isBackOffice, $label = 'default') {
829 if (is_null($isBackOffice)) {
830 throw new \
InvalidArgumentException("isBackOffice must be a bool, received NULL.");
832 return $this->set('isBackOffice', $label, (bool) $isBackOffice);
836 * Getter for isRecur.
838 * @param string $label
842 public function getIsRecur($label = 'default'):bool {
843 if (!$this->has('isRecur')) {
846 return $this->get('isRecur', $label);
850 * @param bool $isRecur
851 * @param string $label e.g. 'default'
853 public function setIsRecur($isRecur, $label = 'default') {
854 if (is_null($isRecur)) {
855 throw new \
InvalidArgumentException("isRecur must be a bool, received NULL.");
857 return $this->set('isRecur', $label, (bool) $isRecur);
861 * Set whether the user has selected to notify the processor of a cancellation request.
863 * When cancelling the user may be presented with an option to notify the processor. The payment
864 * processor can take their response, if present, into account.
867 * @param string $label e.g. 'default'
869 * @return \Civi\Payment\PropertyBag
871 public function setIsNotifyProcessorOnCancelRecur($value, $label = 'default') {
872 return $this->set('isNotifyProcessorOnCancelRecur', $label, (bool) $value);
876 * Get whether the user has selected to notify the processor of a cancellation request.
878 * When cancelling the user may be presented with an option to notify the processor. The payment
879 * processor can take their response, if present, into account.
881 * @param string $label e.g. 'default'
883 * @return \Civi\Payment\PropertyBag
885 public function getIsNotifyProcessorOnCancelRecur($label = 'default') {
886 return $this->get('isNotifyProcessorOnCancelRecur', $label);
894 public function getLastName($label = 'default') {
895 return $this->get('lastName', $label);
901 * @param string $lastName
902 * @param string $label e.g. 'default'
904 public function setLastName($lastName, $label = 'default') {
905 return $this->set('lastName', $label, (string) $lastName);
909 * Getter for payment processor generated string for charging.
911 * A payment token could be a single use token (e.g generated by
912 * a client side script) or a token that permits recurring or on demand charging.
914 * The key thing is it is passed to the processor in lieu of card details to
915 * initiate a payment.
917 * Generally if a processor is going to pass in a payment token generated through
918 * javascript it would add 'payment_token' to the array it returns in it's
919 * implementation of getPaymentFormFields. This will add a hidden 'payment_token' field to
920 * the form. A good example is client side encryption where credit card details are replaced by
921 * an encrypted token using a gateway provided javascript script. In this case the javascript will
922 * remove the credit card details from the form before submitting and populate the payment_token field.
924 * A more complex example is used by paypal checkout where the payment token is generated
925 * via a pre-approval process. In that case the doPreApproval function is called on the processor
926 * class to get information to then interact with paypal via js, finally getting a payment token.
927 * (at this stage the pre-approve api is not in core but that is likely to change - we just need
928 * to think about the permissions since we don't want to expose to anonymous user without thinking
929 * through any risk of credit-card testing using it.
931 * If the token is not totally transient it would be saved to civicrm_payment_token.token.
933 * @param string $label
935 * @return string|null
937 public function getPaymentToken($label = 'default') {
938 return $this->get('paymentToken', $label);
942 * @param string $paymentToken
943 * @param string $label e.g. 'default'
945 public function setPaymentToken($paymentToken, $label = 'default') {
946 return $this->set('paymentToken', $label, $paymentToken);
954 public function getPhone($label = 'default') {
955 return $this->get('phone', $label);
961 * @param string $phone
962 * @param string $label e.g. 'default'
964 public function setPhone($phone, $label = 'default') {
965 return $this->set('phone', $label, (string) $phone);
969 * Combined with recurFrequencyUnit this gives how often billing will take place.
971 * e.g every if this is 1 and recurFrequencyUnit is 'month' then it is every 1 month.
974 public function getRecurFrequencyInterval($label = 'default') {
975 return $this->get('recurFrequencyInterval', $label);
979 * @param int $recurFrequencyInterval
980 * @param string $label e.g. 'default'
982 public function setRecurFrequencyInterval($recurFrequencyInterval, $label = 'default') {
983 if (!($recurFrequencyInterval > 0)) {
984 throw new InvalidArgumentException("recurFrequencyInterval must be a positive integer");
987 return $this->set('recurFrequencyInterval', $label, (int) $recurFrequencyInterval);
991 * Getter for recurFrequencyUnit.
992 * Combined with recurFrequencyInterval this gives how often billing will take place.
994 * e.g every if this is 'month' and recurFrequencyInterval is 1 then it is every 1 month.
997 * @param string $label
999 * @return string month|day|year
1001 public function getRecurFrequencyUnit($label = 'default') {
1002 return $this->get('recurFrequencyUnit', $label);
1006 * @param string $recurFrequencyUnit month|day|week|year
1007 * @param string $label e.g. 'default'
1009 public function setRecurFrequencyUnit($recurFrequencyUnit, $label = 'default') {
1010 if (!preg_match('/^day|week|month|year$/', $recurFrequencyUnit)) {
1011 throw new \
InvalidArgumentException("recurFrequencyUnit must be day|week|month|year");
1013 return $this->set('recurFrequencyUnit', $label, $recurFrequencyUnit);
1017 * @param string $label
1021 public function getRecurInstallments($label = 'default') {
1022 return $this->get('recurInstallments', $label);
1026 * @param int $recurInstallments
1027 * @param string $label
1029 * @return \Civi\Payment\PropertyBag
1030 * @throws \CRM_Core_Exception
1032 public function setRecurInstallments($recurInstallments, $label = 'default') {
1033 // Counts zero as positive which is ok - means no installments
1034 if (!\CRM_Utils_Type
::validate($recurInstallments, 'Positive')) {
1035 throw new InvalidArgumentException('recurInstallments must be 0 or a positive integer');
1038 return $this->set('recurInstallments', $label, (int) $recurInstallments);
1042 * Set the unique payment processor service provided ID for a particular subscription.
1044 * Nb. this is stored in civicrm_contribution_recur.processor_id and is NOT
1045 * in any way related to the payment processor ID.
1047 * @param string $label
1049 * @return string|null
1051 public function getRecurProcessorID($label = 'default') {
1052 return $this->get('recurProcessorID', $label);
1056 * Set the unique payment processor service provided ID for a particular
1059 * See https://github.com/civicrm/civicrm-core/pull/17292 for discussion
1060 * of how this function accepting NULL fits with standard / planned behaviour.
1062 * @param string|null $input
1063 * @param string $label e.g. 'default'
1065 * @return \Civi\Payment\PropertyBag
1067 public function setRecurProcessorID($input, $label = 'default') {
1068 if ($input === '') {
1071 if (strlen($input) > 255 ||
in_array($input, [FALSE, 0], TRUE)) {
1072 throw new \
InvalidArgumentException('processorID field has max length of 255');
1074 return $this->set('recurProcessorID', $label, $input);
1078 * Getter for payment processor generated string for the transaction ID.
1080 * Note some gateways generate a reference for the order and one for the
1081 * payment. This is for the payment reference and is saved to
1082 * civicrm_financial_trxn.trxn_id.
1084 * @param string $label
1086 * @return string|null
1088 public function getTransactionID($label = 'default') {
1089 return $this->get('transactionID', $label);
1093 * @param string $transactionID
1094 * @param string $label e.g. 'default'
1096 public function setTransactionID($transactionID, $label = 'default') {
1097 return $this->set('transactionID', $label, $transactionID);
1101 * Getter for trxnResultCode.
1103 * Additional information returned by the payment processor regarding the
1106 * This would normally be saved in civicrm_financial_trxn.trxn_result_code.
1109 * @param string $label
1111 * @return string|null
1113 public function getTrxnResultCode($label = 'default') {
1114 return $this->get('trxnResultCode', $label);
1118 * @param string $trxnResultCode
1119 * @param string $label e.g. 'default'
1121 public function setTrxnResultCode($trxnResultCode, $label = 'default') {
1122 return $this->set('trxnResultCode', $label, $trxnResultCode);
1125 // Custom Properties.
1128 * Sometimes we may need to pass in things that are specific to the Payment
1131 * @param string $prop
1132 * @param string $label e.g. 'default' or 'old' or 'new'
1135 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1137 public function getCustomProperty($prop, $label = 'default') {
1138 if (isset(static::$propMap[$prop])) {
1139 throw new \
InvalidArgumentException("Attempted to get '$prop' via getCustomProperty - must use using its getter.");
1142 if (!array_key_exists($prop, $this->props
[$label] ??
[])) {
1143 throw new \
BadMethodCallException("Property '$prop' has not been set.");
1145 return $this->props
[$label][$prop] ??
NULL;
1149 * We have to leave validation to the processor, but we can still give them a
1150 * way to store their data on this PropertyBag
1152 * @param string $prop
1153 * @param mixed $value
1154 * @param string $label e.g. 'default' or 'old' or 'new'
1156 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1158 public function setCustomProperty($prop, $value, $label = 'default') {
1159 if (isset(static::$propMap[$prop])) {
1160 throw new \
InvalidArgumentException("Attempted to set '$prop' via setCustomProperty - must use using its setter.");
1162 $this->props
[$label][$prop] = $value;