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.
81 * For paymentprocessors that have not converted to propertyBag we need to support "legacy" properties - eg. "is_recur"
82 * without warnings. Setting this allows us to pass a propertyBag into doPayment() and expect it to "work" with
83 * existing payment processors.
85 protected $suppressLegacyWarnings = TRUE;
88 * Get the value of the suppressLegacyWarnings parameter
91 public function getSuppressLegacyWarnings() {
92 return $this->suppressLegacyWarnings
;
96 * Set the suppressLegacyWarnings parameter - useful for unit tests.
97 * Eg. you could set to FALSE for unit tests on a paymentprocessor to capture use of legacy keys in that processor
99 * @param bool $suppressLegacyWarnings
101 public function setSuppressLegacyWarnings(bool $suppressLegacyWarnings) {
102 $this->suppressLegacyWarnings
= $suppressLegacyWarnings;
106 * Get the property bag.
108 * This allows us to swap a 'might be an array might be a property bag'
109 * variable for a known PropertyBag.
111 * @param \Civi\Payment\PropertyBag|array $params
113 * @return \Civi\Payment\PropertyBag
115 public static function cast($params) {
116 if ($params instanceof self
) {
119 $propertyBag = new self();
120 $propertyBag->mergeLegacyInputParams($params);
125 * Just for unit testing.
132 * Implements ArrayAccess::offsetExists
134 * @param mixed $offset
135 * @return bool TRUE if we have that value (on our default store)
137 public function offsetExists ($offset): bool {
138 $prop = $this->handleLegacyPropNames($offset, TRUE);
139 // If there's no prop, assume it's a custom property.
140 $prop = $prop ??
$offset;
141 return array_key_exists($prop, $this->props
['default']);
145 * Implements ArrayAccess::offsetGet
147 * @param mixed $offset
150 public function offsetGet($offset) {
152 $prop = $this->handleLegacyPropNames($offset);
154 catch (InvalidArgumentException
$e) {
156 CRM_Core_Error
::deprecatedFunctionWarning(
157 "proper getCustomProperty('$offset') for non-core properties. "
159 "PropertyBag array access to get '$offset'"
163 return $this->getCustomProperty($offset, 'default');
165 catch (BadMethodCallException
$e) {
166 CRM_Core_Error
::deprecatedFunctionWarning(
167 "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."
169 "PropertyBag array access to get unset property '$offset'"
175 CRM_Core_Error
::deprecatedFunctionWarning(
176 "get" . ucfirst($offset) . "()",
177 "PropertyBag array access for core property '$offset'"
179 return $this->get($prop, 'default');
183 * Implements ArrayAccess::offsetSet
185 * @param mixed $offset
186 * @param mixed $value
188 public function offsetSet($offset, $value) {
190 $prop = $this->handleLegacyPropNames($offset);
192 catch (InvalidArgumentException
$e) {
193 // We need to make a lot of noise here, we're being asked to merge in
194 // something we can't validate because we don't know what this property is.
195 // This is fine if it's something particular to a payment processor
196 // (which should be using setCustomProperty) however it could also lead to
197 // things like 'my_weirly_named_contact_id'.
199 // From 5.28 we suppress this when using PropertyBag::cast() to ease transition.
200 if (!$this->suppressLegacyWarnings
) {
201 CRM_Core_Error
::deprecatedFunctionWarning(
202 "proper setCustomProperty('$offset', \$value) for non-core properties. "
204 "PropertyBag array access to set '$offset'"
207 $this->setCustomProperty($offset, $value, 'default');
211 // Coerce legacy values that were in use but shouldn't be in our new way of doing things.
212 if ($prop === 'feeAmount' && $value === '') {
213 // At least the following classes pass in ZLS for feeAmount.
214 // CRM_Core_Payment_AuthorizeNetTest::testCreateSingleNowDated
215 // CRM_Core_Payment_AuthorizeNetTest::testCreateSinglePostDated
219 // These lines are here (and not in try block) because the catch must only
220 // catch the case when the prop is custom.
221 $setter = 'set' . ucfirst($prop);
222 if (!$this->suppressLegacyWarnings
) {
223 CRM_Core_Error
::deprecatedFunctionWarning(
225 "PropertyBag array access to set core property '$offset'"
228 $this->$setter($value, 'default');
232 * Implements ArrayAccess::offsetUnset
234 * @param mixed $offset
236 public function offsetUnset ($offset) {
237 $prop = $this->handleLegacyPropNames($offset);
238 unset($this->props
['default'][$prop]);
242 * @param string $prop
243 * @param bool $silent if TRUE return NULL instead of throwing an exception. This is because offsetExists should be safe and not throw exceptions.
244 * @return string canonical name.
245 * @throws \InvalidArgumentException if prop name not known.
247 protected function handleLegacyPropNames($prop, $silent = FALSE) {
248 $newName = static::$propMap[$prop] ??
NULL;
249 if ($newName === TRUE) {
250 // Good, modern name.
253 // Handling for legacy addition of billing details.
254 if ($newName === NULL && substr($prop, -2) === '-' . \CRM_Core_BAO_LocationType
::getBilling()
255 && isset(static::$propMap[substr($prop, 0, -2)])
257 $newName = substr($prop, 0, -2);
260 if ($newName === NULL) {
262 // Only for use by offsetExists
265 throw new \
InvalidArgumentException("Unknown property '$prop'.");
267 // Remaining case is legacy name that's been translated.
268 if (!$this->suppressLegacyWarnings
) {
269 CRM_Core_Error
::deprecatedFunctionWarning("Canonical property name '$newName'", "Legacy property name '$prop'");
278 * @param mixed $prop Valid property name
279 * @param string $label e.g. 'default'
283 protected function get($prop, $label) {
284 if (array_key_exists($prop, $this->props
[$label] ??
[])) {
285 return $this->props
[$label][$prop];
287 throw new \
BadMethodCallException("Property '$prop' has not been set.");
293 * @param mixed $prop Valid property name
294 * @param string $label e.g. 'default'
295 * @param mixed $value
297 * @return PropertyBag $this object so you can chain set setters.
299 protected function set($prop, $label, $value) {
300 $this->props
[$label][$prop] = $value;
307 protected function coercePseudoConstantStringToInt(string $baoName, string $field, $input) {
308 if (is_numeric($input)) {
309 // We've been given a numeric ID.
312 elseif (is_string($input)) {
313 // We've been given a named instrument.
314 $_ = (int) CRM_Core_PseudoConstant
::getKey($baoName, $field, $input);
317 throw new InvalidArgumentException("Expected an integer ID or a String name for $field.");
320 throw new InvalidArgumentException("Expected an integer greater than 0 for $field.");
327 public function has($prop, $label = 'default') {
328 // We do NOT translate legacy prop names since only new code should be
329 // using this method, and new code should be using canonical names.
330 // $prop = $this->handleLegacyPropNames($prop);
331 return isset($this->props
[$label][$prop]);
335 * This is used to merge values from an array.
336 * It's a transitional, internal function and should not be used!
340 public function mergeLegacyInputParams($data) {
341 // Suppress legacy warnings for merging an array of data as this
342 // suits our migration plan at this moment. Future behaviour may differ.
343 // @see https://github.com/civicrm/civicrm-core/pull/17643
344 $this->suppressLegacyWarnings
= TRUE;
345 foreach ($data as $key => $value) {
346 if ($value !== NULL && $value !== '') {
347 $this->offsetSet($key, $value);
350 $this->suppressLegacyWarnings
= FALSE;
354 * Throw an exception if any of the props is unset.
356 * @param array $props Array of proper property names (no legacy aliases allowed).
358 * @return PropertyBag
360 public function require(array $props) {
362 foreach ($props as $prop) {
363 if (!isset($this->props
['default'][$prop])) {
368 throw new \
InvalidArgumentException("Required properties missing: " . implode(', ', $missing));
373 // Public getters, setters.
376 * Get a property by its name (but still using its getter).
378 * @param string $prop valid property name, like contactID
379 * @param bool $allowUnset If TRUE, return the default value if the property is
380 * not set - normal behaviour would be to throw an exception.
381 * @param mixed $default
382 * @param string $label e.g. 'default' or 'old' or 'new'
386 public function getter($prop, $allowUnset = FALSE, $default = NULL, $label = 'default') {
388 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
389 // This is a standard property that will have a getter method.
390 $getter = 'get' . ucfirst($prop);
391 return (!$allowUnset ||
$this->has($prop, $label))
392 ?
$this->$getter($label)
396 // This is not a property name we know, but they could be requesting a
398 return (!$allowUnset ||
$this->has($prop, $label))
399 ?
$this->getCustomProperty($prop, $label)
404 * Set a property by its name (but still using its setter).
406 * @param string $prop valid property name, like contactID
407 * @param mixed $value
408 * @param string $label e.g. 'default' or 'old' or 'new'
412 public function setter($prop, $value = NULL, $label = 'default') {
413 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
414 // This is a standard property.
415 $setter = 'set' . ucfirst($prop);
416 return $this->$setter($value, $label);
418 // We don't allow using the setter for custom properties.
419 throw new \
BadMethodCallException("Cannot use generic setter with non-standard properties; you must use setCustomProperty for custom properties.");
423 * Get the monetary amount.
425 public function getAmount($label = 'default') {
426 return $this->get('amount', $label);
430 * Set the monetary amount.
432 * - We expect to be called with a string amount with optional decimals using
433 * a '.' as the decimal point (not a ',').
435 * - We're ok with floats/ints being passed in, too, but we'll cast them to a
438 * - Negatives are fine.
440 * @see https://github.com/civicrm/civicrm-core/pull/18219
442 * @param string|float|int $value
443 * @param string $label
445 public function setAmount($value, $label = 'default') {
446 if (!is_numeric($value)) {
447 throw new \
InvalidArgumentException("setAmount requires a numeric amount value");
449 return $this->set('amount', $label, filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT
, FILTER_FLAG_ALLOW_FRACTION
));
453 * BillingStreetAddress getter.
457 public function getBillingStreetAddress($label = 'default') {
458 return $this->get('billingStreetAddress', $label);
462 * BillingStreetAddress setter.
464 * @param string $input
465 * @param string $label e.g. 'default'
467 public function setBillingStreetAddress($input, $label = 'default') {
468 return $this->set('billingStreetAddress', $label, (string) $input);
472 * BillingSupplementalAddress1 getter.
476 public function getBillingSupplementalAddress1($label = 'default') {
477 return $this->get('billingSupplementalAddress1', $label);
481 * BillingSupplementalAddress1 setter.
483 * @param string $input
484 * @param string $label e.g. 'default'
486 public function setBillingSupplementalAddress1($input, $label = 'default') {
487 return $this->set('billingSupplementalAddress1', $label, (string) $input);
491 * BillingSupplementalAddress2 getter.
495 public function getBillingSupplementalAddress2($label = 'default') {
496 return $this->get('billingSupplementalAddress2', $label);
500 * BillingSupplementalAddress2 setter.
502 * @param string $input
503 * @param string $label e.g. 'default'
505 public function setBillingSupplementalAddress2($input, $label = 'default') {
506 return $this->set('billingSupplementalAddress2', $label, (string) $input);
510 * BillingSupplementalAddress3 getter.
514 public function getBillingSupplementalAddress3($label = 'default') {
515 return $this->get('billingSupplementalAddress3', $label);
519 * BillingSupplementalAddress3 setter.
521 * @param string $input
522 * @param string $label e.g. 'default'
524 public function setBillingSupplementalAddress3($input, $label = 'default') {
525 return $this->set('billingSupplementalAddress3', $label, (string) $input);
529 * BillingCity getter.
533 public function getBillingCity($label = 'default') {
534 return $this->get('billingCity', $label);
538 * BillingCity setter.
540 * @param string $input
541 * @param string $label e.g. 'default'
543 * @return \Civi\Payment\PropertyBag
545 public function setBillingCity($input, $label = 'default') {
546 return $this->set('billingCity', $label, (string) $input);
550 * BillingPostalCode getter.
554 public function getBillingPostalCode($label = 'default') {
555 return $this->get('billingPostalCode', $label);
559 * BillingPostalCode setter.
561 * @param string $input
562 * @param string $label e.g. 'default'
564 public function setBillingPostalCode($input, $label = 'default') {
565 return $this->set('billingPostalCode', $label, (string) $input);
569 * BillingCounty getter.
573 public function getBillingCounty($label = 'default') {
574 return $this->get('billingCounty', $label);
578 * BillingCounty setter.
580 * Nb. we can't validate this unless we have the country ID too, so we don't.
582 * @param string $input
583 * @param string $label e.g. 'default'
585 public function setBillingCounty($input, $label = 'default') {
586 return $this->set('billingCounty', $label, (string) $input);
590 * BillingCountry getter.
594 public function getBillingCountry($label = 'default') {
595 return $this->get('billingCountry', $label);
599 * BillingCountry setter.
601 * Nb. We require and we store a 2 character country code.
603 * @param string $input
604 * @param string $label e.g. 'default'
606 public function setBillingCountry($input, $label = 'default') {
607 if (!is_string($input) ||
strlen($input) !== 2) {
608 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
610 if (!CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'country_id', $input)) {
611 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
613 return $this->set('billingCountry', $label, (string) $input);
619 public function getContactID($label = 'default'): int {
620 return $this->get('contactID', $label);
624 * @param int $contactID
625 * @param string $label
627 public function setContactID($contactID, $label = 'default') {
628 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
629 if (!($contactID > 0)) {
630 throw new InvalidArgumentException("ContactID must be a positive integer");
633 return $this->set('contactID', $label, (int) $contactID);
637 * Getter for contributionID.
640 * @param string $label
642 public function getContributionID($label = 'default') {
643 return $this->get('contributionID', $label);
647 * @param int $contributionID
648 * @param string $label e.g. 'default'
650 public function setContributionID($contributionID, $label = 'default') {
651 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
652 if (!($contributionID > 0)) {
653 throw new InvalidArgumentException("ContributionID must be a positive integer");
656 return $this->set('contributionID', $label, (int) $contributionID);
660 * Getter for contributionRecurID.
663 * @param string $label
665 public function getContributionRecurID($label = 'default') {
666 return $this->get('contributionRecurID', $label);
670 * @param int $contributionRecurID
671 * @param string $label e.g. 'default'
673 * @return \Civi\Payment\PropertyBag
675 public function setContributionRecurID($contributionRecurID, $label = 'default') {
676 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
677 if (!($contributionRecurID > 0)) {
678 throw new InvalidArgumentException('ContributionRecurID must be a positive integer');
681 return $this->set('contributionRecurID', $label, (int) $contributionRecurID);
685 * Three character currency code.
687 * https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
689 * @param string $label e.g. 'default'
691 public function getCurrency($label = 'default') {
692 return $this->get('currency', $label);
696 * Three character currency code.
698 * @param string $value
699 * @param string $label e.g. 'default'
701 public function setCurrency($value, $label = 'default') {
702 if (!preg_match('/^[A-Z]{3}$/', $value)) {
703 throw new \
InvalidArgumentException("Attemted to setCurrency with a value that was not an ISO 3166-1 alpha 3 currency code");
705 return $this->set('currency', $label, $value);
710 * @param string $label
714 public function getDescription($label = 'default') {
715 return $this->get('description', $label);
719 * @param string $description
720 * @param string $label e.g. 'default'
722 public function setDescription($description, $label = 'default') {
723 // @todo this logic was copied from a commit that then got deleted. Is it good?
724 $uninformativeStrings = [
725 ts('Online Event Registration: '),
726 ts('Online Contribution: '),
728 $cleanedDescription = str_replace($uninformativeStrings, '', $description);
729 return $this->set('description', $label, $cleanedDescription);
737 public function getEmail($label = 'default') {
738 return $this->get('email', $label);
744 * @param string $email
745 * @param string $label e.g. 'default'
747 public function setEmail($email, $label = 'default') {
748 return $this->set('email', $label, (string) $email);
752 * Amount of money charged in fees by the payment processor.
754 * This is notified by (some) payment processers.
756 * @param string $label
760 public function getFeeAmount($label = 'default') {
761 return $this->get('feeAmount', $label);
765 * @param string $feeAmount
766 * @param string $label e.g. 'default'
768 public function setFeeAmount($feeAmount, $label = 'default') {
769 if (!is_numeric($feeAmount)) {
770 throw new \
InvalidArgumentException("feeAmount must be a number.");
772 return $this->set('feeAmount', $label, (float) $feeAmount);
780 public function getFirstName($label = 'default') {
781 return $this->get('firstName', $label);
787 * @param string $firstName
788 * @param string $label e.g. 'default'
790 public function setFirstName($firstName, $label = 'default') {
791 return $this->set('firstName', $label, (string) $firstName);
795 * Getter for invoiceID.
797 * @param string $label
799 * @return string|null
801 public function getInvoiceID($label = 'default') {
802 return $this->get('invoiceID', $label);
806 * @param string $invoiceID
807 * @param string $label e.g. 'default'
809 public function setInvoiceID($invoiceID, $label = 'default') {
810 return $this->set('invoiceID', $label, $invoiceID);
814 * Getter for isBackOffice.
816 * @param string $label
820 public function getIsBackOffice($label = 'default'):bool {
821 // @todo should this return FALSE instead of exception to keep current situation?
822 return $this->get('isBackOffice', $label);
826 * @param bool $isBackOffice
827 * @param string $label e.g. 'default'
829 public function setIsBackOffice($isBackOffice, $label = 'default') {
830 if (is_null($isBackOffice)) {
831 throw new \
InvalidArgumentException("isBackOffice must be a bool, received NULL.");
833 return $this->set('isBackOffice', $label, (bool) $isBackOffice);
837 * Getter for isRecur.
839 * @param string $label
843 public function getIsRecur($label = 'default'):bool {
844 if (!$this->has('isRecur')) {
847 return $this->get('isRecur', $label);
851 * @param bool $isRecur
852 * @param string $label e.g. 'default'
854 public function setIsRecur($isRecur, $label = 'default') {
855 if (is_null($isRecur)) {
856 throw new \
InvalidArgumentException("isRecur must be a bool, received NULL.");
858 return $this->set('isRecur', $label, (bool) $isRecur);
862 * Set whether the user has selected to notify the processor of a cancellation request.
864 * When cancelling the user may be presented with an option to notify the processor. The payment
865 * processor can take their response, if present, into account.
868 * @param string $label e.g. 'default'
870 * @return \Civi\Payment\PropertyBag
872 public function setIsNotifyProcessorOnCancelRecur($value, $label = 'default') {
873 return $this->set('isNotifyProcessorOnCancelRecur', $label, (bool) $value);
877 * Get whether the user has selected to notify the processor of a cancellation request.
879 * When cancelling the user may be presented with an option to notify the processor. The payment
880 * processor can take their response, if present, into account.
882 * @param string $label e.g. 'default'
884 * @return \Civi\Payment\PropertyBag
886 public function getIsNotifyProcessorOnCancelRecur($label = 'default') {
887 return $this->get('isNotifyProcessorOnCancelRecur', $label);
895 public function getLastName($label = 'default') {
896 return $this->get('lastName', $label);
902 * @param string $lastName
903 * @param string $label e.g. 'default'
905 public function setLastName($lastName, $label = 'default') {
906 return $this->set('lastName', $label, (string) $lastName);
910 * Getter for payment processor generated string for charging.
912 * A payment token could be a single use token (e.g generated by
913 * a client side script) or a token that permits recurring or on demand charging.
915 * The key thing is it is passed to the processor in lieu of card details to
916 * initiate a payment.
918 * Generally if a processor is going to pass in a payment token generated through
919 * javascript it would add 'payment_token' to the array it returns in it's
920 * implementation of getPaymentFormFields. This will add a hidden 'payment_token' field to
921 * the form. A good example is client side encryption where credit card details are replaced by
922 * an encrypted token using a gateway provided javascript script. In this case the javascript will
923 * remove the credit card details from the form before submitting and populate the payment_token field.
925 * A more complex example is used by paypal checkout where the payment token is generated
926 * via a pre-approval process. In that case the doPreApproval function is called on the processor
927 * class to get information to then interact with paypal via js, finally getting a payment token.
928 * (at this stage the pre-approve api is not in core but that is likely to change - we just need
929 * to think about the permissions since we don't want to expose to anonymous user without thinking
930 * through any risk of credit-card testing using it.
932 * If the token is not totally transient it would be saved to civicrm_payment_token.token.
934 * @param string $label
936 * @return string|null
938 public function getPaymentToken($label = 'default') {
939 return $this->get('paymentToken', $label);
943 * @param string $paymentToken
944 * @param string $label e.g. 'default'
946 public function setPaymentToken($paymentToken, $label = 'default') {
947 return $this->set('paymentToken', $label, $paymentToken);
955 public function getPhone($label = 'default') {
956 return $this->get('phone', $label);
962 * @param string $phone
963 * @param string $label e.g. 'default'
965 public function setPhone($phone, $label = 'default') {
966 return $this->set('phone', $label, (string) $phone);
970 * Combined with recurFrequencyUnit this gives how often billing will take place.
972 * e.g every if this is 1 and recurFrequencyUnit is 'month' then it is every 1 month.
975 public function getRecurFrequencyInterval($label = 'default') {
976 return $this->get('recurFrequencyInterval', $label);
980 * @param int $recurFrequencyInterval
981 * @param string $label e.g. 'default'
983 public function setRecurFrequencyInterval($recurFrequencyInterval, $label = 'default') {
984 if (!($recurFrequencyInterval > 0)) {
985 throw new InvalidArgumentException("recurFrequencyInterval must be a positive integer");
988 return $this->set('recurFrequencyInterval', $label, (int) $recurFrequencyInterval);
992 * Getter for recurFrequencyUnit.
993 * Combined with recurFrequencyInterval this gives how often billing will take place.
995 * e.g every if this is 'month' and recurFrequencyInterval is 1 then it is every 1 month.
998 * @param string $label
1000 * @return string month|day|year
1002 public function getRecurFrequencyUnit($label = 'default') {
1003 return $this->get('recurFrequencyUnit', $label);
1007 * @param string $recurFrequencyUnit month|day|week|year
1008 * @param string $label e.g. 'default'
1010 public function setRecurFrequencyUnit($recurFrequencyUnit, $label = 'default') {
1011 if (!preg_match('/^day|week|month|year$/', $recurFrequencyUnit)) {
1012 throw new \
InvalidArgumentException("recurFrequencyUnit must be day|week|month|year");
1014 return $this->set('recurFrequencyUnit', $label, $recurFrequencyUnit);
1018 * @param string $label
1022 public function getRecurInstallments($label = 'default') {
1023 return $this->get('recurInstallments', $label);
1027 * @param int $recurInstallments
1028 * @param string $label
1030 * @return \Civi\Payment\PropertyBag
1031 * @throws \CRM_Core_Exception
1033 public function setRecurInstallments($recurInstallments, $label = 'default') {
1034 // Counts zero as positive which is ok - means no installments
1035 if (!\CRM_Utils_Type
::validate($recurInstallments, 'Positive')) {
1036 throw new InvalidArgumentException('recurInstallments must be 0 or a positive integer');
1039 return $this->set('recurInstallments', $label, (int) $recurInstallments);
1043 * Set the unique payment processor service provided ID for a particular subscription.
1045 * Nb. this is stored in civicrm_contribution_recur.processor_id and is NOT
1046 * in any way related to the payment processor ID.
1048 * @param string $label
1050 * @return string|null
1052 public function getRecurProcessorID($label = 'default') {
1053 return $this->get('recurProcessorID', $label);
1057 * Set the unique payment processor service provided ID for a particular
1060 * See https://github.com/civicrm/civicrm-core/pull/17292 for discussion
1061 * of how this function accepting NULL fits with standard / planned behaviour.
1063 * @param string|null $input
1064 * @param string $label e.g. 'default'
1066 * @return \Civi\Payment\PropertyBag
1068 public function setRecurProcessorID($input, $label = 'default') {
1069 if ($input === '') {
1072 if (strlen($input) > 255 ||
in_array($input, [FALSE, 0], TRUE)) {
1073 throw new \
InvalidArgumentException('processorID field has max length of 255');
1075 return $this->set('recurProcessorID', $label, $input);
1079 * Getter for payment processor generated string for the transaction ID.
1081 * Note some gateways generate a reference for the order and one for the
1082 * payment. This is for the payment reference and is saved to
1083 * civicrm_financial_trxn.trxn_id.
1085 * @param string $label
1087 * @return string|null
1089 public function getTransactionID($label = 'default') {
1090 return $this->get('transactionID', $label);
1094 * @param string $transactionID
1095 * @param string $label e.g. 'default'
1097 public function setTransactionID($transactionID, $label = 'default') {
1098 return $this->set('transactionID', $label, $transactionID);
1102 * Getter for trxnResultCode.
1104 * Additional information returned by the payment processor regarding the
1107 * This would normally be saved in civicrm_financial_trxn.trxn_result_code.
1110 * @param string $label
1112 * @return string|null
1114 public function getTrxnResultCode($label = 'default') {
1115 return $this->get('trxnResultCode', $label);
1119 * @param string $trxnResultCode
1120 * @param string $label e.g. 'default'
1122 public function setTrxnResultCode($trxnResultCode, $label = 'default') {
1123 return $this->set('trxnResultCode', $label, $trxnResultCode);
1126 // Custom Properties.
1129 * Sometimes we may need to pass in things that are specific to the Payment
1132 * @param string $prop
1133 * @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 getCustomProperty($prop, $label = 'default') {
1139 if (isset(static::$propMap[$prop])) {
1140 throw new \
InvalidArgumentException("Attempted to get '$prop' via getCustomProperty - must use using its getter.");
1143 if (!array_key_exists($prop, $this->props
[$label] ??
[])) {
1144 throw new \
BadMethodCallException("Property '$prop' has not been set.");
1146 return $this->props
[$label][$prop] ??
NULL;
1150 * We have to leave validation to the processor, but we can still give them a
1151 * way to store their data on this PropertyBag
1153 * @param string $prop
1154 * @param mixed $value
1155 * @param string $label e.g. 'default' or 'old' or 'new'
1157 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1159 public function setCustomProperty($prop, $value, $label = 'default') {
1160 if (isset(static::$propMap[$prop])) {
1161 throw new \
InvalidArgumentException("Attempted to set '$prop' via setCustomProperty - must use using its setter.");
1163 $this->props
[$label][$prop] = $value;