2 namespace Civi\Payment
;
4 use InvalidArgumentException
;
7 use CRM_Core_PseudoConstant
;
12 * This class provides getters and setters for arguments needed by CRM_Core_Payment methods.
14 * The setters know how to validate each setting that they are responsible for.
16 * Different methods need different settings and the concept is that by passing
17 * in a property bag we can encapsulate the params needed for a particular
18 * method call, rather than setting arguments for different methods on the main
19 * CRM_Core_Payment object.
21 * This class is also supposed to help with transition away from array key naming nightmares.
24 class PropertyBag
implements \ArrayAccess
{
26 protected $props = ['default' => []];
28 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 'subscriptionId' => 'recurProcessorID',
67 'recurProcessorID' => TRUE,
68 'transactionID' => TRUE,
69 'transaction_id' => 'transactionID',
70 'trxnResultCode' => TRUE,
71 'isNotifyProcessorOnCancelRecur' => TRUE,
77 * Temporary, internal variable to help ease transition to PropertyBag.
78 * Used by cast() to suppress legacy warnings.
80 protected $suppressLegacyWarnings = FALSE;
83 * Get the property bag.
85 * This allows us to swap a 'might be an array might be a property bag'
86 * variable for a known PropertyBag.
88 * @param \Civi\Payment\PropertyBag|array $params
90 * @return \Civi\Payment\PropertyBag
92 public static function cast($params) {
93 if ($params instanceof self
) {
96 $propertyBag = new self();
97 $propertyBag->mergeLegacyInputParams($params);
102 * Just for unit testing.
109 * Implements ArrayAccess::offsetExists
111 * @param mixed $offset
112 * @return bool TRUE if we have that value (on our default store)
114 public function offsetExists ($offset): bool {
115 $prop = $this->handleLegacyPropNames($offset, TRUE);
116 // If there's no prop, assume it's a custom property.
117 $prop = $prop ??
$offset;
118 return array_key_exists($prop, $this->props
['default']);
122 * Implements ArrayAccess::offsetGet
124 * @param mixed $offset
127 public function offsetGet($offset) {
129 $prop = $this->handleLegacyPropNames($offset);
131 catch (InvalidArgumentException
$e) {
133 CRM_Core_Error
::deprecatedFunctionWarning(
134 "proper getCustomProperty('$offset') for non-core properties. "
136 "PropertyBag array access to get '$offset'"
140 return $this->getCustomProperty($offset, 'default');
142 catch (BadMethodCallException
$e) {
143 CRM_Core_Error
::deprecatedFunctionWarning(
144 "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."
146 "PropertyBag array access to get unset property '$offset'"
152 CRM_Core_Error
::deprecatedFunctionWarning(
153 "get" . ucfirst($offset) . "()",
154 "PropertyBag array access for core property '$offset'"
156 return $this->get($prop, 'default');
160 * Implements ArrayAccess::offsetSet
162 * @param mixed $offset
163 * @param mixed $value
165 public function offsetSet($offset, $value) {
167 $prop = $this->handleLegacyPropNames($offset);
169 catch (InvalidArgumentException
$e) {
170 // We need to make a lot of noise here, we're being asked to merge in
171 // something we can't validate because we don't know what this property is.
172 // This is fine if it's something particular to a payment processor
173 // (which should be using setCustomProperty) however it could also lead to
174 // things like 'my_weirly_named_contact_id'.
176 // From 5.28 we suppress this when using PropertyBag::cast() to ease transition.
177 if (!$this->suppressLegacyWarnings
) {
178 CRM_Core_Error
::deprecatedFunctionWarning(
179 "proper setCustomProperty('$offset', \$value) for non-core properties. "
181 "PropertyBag array access to set '$offset'"
184 $this->setCustomProperty($offset, $value, 'default');
188 // Coerce legacy values that were in use but shouldn't be in our new way of doing things.
189 if ($prop === 'feeAmount' && $value === '') {
190 // At least the following classes pass in ZLS for feeAmount.
191 // CRM_Core_Payment_AuthorizeNetTest::testCreateSingleNowDated
192 // CRM_Core_Payment_AuthorizeNetTest::testCreateSinglePostDated
196 // These lines are here (and not in try block) because the catch must only
197 // catch the case when the prop is custom.
198 $setter = 'set' . ucfirst($prop);
199 if (!$this->suppressLegacyWarnings
) {
200 CRM_Core_Error
::deprecatedFunctionWarning(
202 "PropertyBag array access to set core property '$offset'"
205 $this->$setter($value, 'default');
209 * Implements ArrayAccess::offsetUnset
211 * @param mixed $offset
213 public function offsetUnset ($offset) {
214 $prop = $this->handleLegacyPropNames($offset);
215 unset($this->props
['default'][$prop]);
219 * Save any legacy warnings to log.
221 * Called as a shutdown function.
223 public static function writeLegacyWarnings() {
224 if (!empty(static::$legacyWarnings)) {
225 $message = "Civi\\Payment\\PropertyBag related deprecation warnings:\n"
226 . implode("\n", array_keys(static::$legacyWarnings));
227 Civi
::log()->warning($message, ['civi.tag' => 'deprecated']);
232 * @param string $prop
233 * @param bool $silent if TRUE return NULL instead of throwing an exception. This is because offsetExists should be safe and not throw exceptions.
234 * @return string canonical name.
235 * @throws \InvalidArgumentException if prop name not known.
237 protected function handleLegacyPropNames($prop, $silent = FALSE) {
238 $newName = static::$propMap[$prop] ??
NULL;
239 if ($newName === TRUE) {
240 // Good, modern name.
243 // Handling for legacy addition of billing details.
244 if ($newName === NULL && substr($prop, -2) === '-' . \CRM_Core_BAO_LocationType
::getBilling()
245 && isset(static::$propMap[substr($prop, 0, -2)])
247 $newName = substr($prop, 0, -2);
250 if ($newName === NULL) {
252 // Only for use by offsetExists
255 throw new \
InvalidArgumentException("Unknown property '$prop'.");
257 // Remaining case is legacy name that's been translated.
258 if (!$this->suppressLegacyWarnings
) {
259 CRM_Core_Error
::deprecatedFunctionWarning("Canonical property name '$newName'", "Legacy property name '$prop'");
268 * @param mixed $prop Valid property name
269 * @param string $label e.g. 'default'
273 protected function get($prop, $label) {
274 if (array_key_exists($prop, $this->props
[$label] ??
[])) {
275 return $this->props
[$label][$prop];
277 throw new \
BadMethodCallException("Property '$prop' has not been set.");
283 * @param mixed $prop Valid property name
284 * @param string $label e.g. 'default'
285 * @param mixed $value
287 * @return PropertyBag $this object so you can chain set setters.
289 protected function set($prop, $label = 'default', $value) {
290 $this->props
[$label][$prop] = $value;
297 protected function coercePseudoConstantStringToInt(string $baoName, string $field, $input) {
298 if (is_numeric($input)) {
299 // We've been given a numeric ID.
302 elseif (is_string($input)) {
303 // We've been given a named instrument.
304 $_ = (int) CRM_Core_PseudoConstant
::getKey($baoName, $field, $input);
307 throw new InvalidArgumentException("Expected an integer ID or a String name for $field.");
310 throw new InvalidArgumentException("Expected an integer greater than 0 for $field.");
317 public function has($prop, $label = 'default') {
318 // We do NOT translate legacy prop names since only new code should be
319 // using this method, and new code should be using canonical names.
320 // $prop = $this->handleLegacyPropNames($prop);
321 return isset($this->props
[$label][$prop]);
325 * This is used to merge values from an array.
326 * It's a transitional, internal function and should not be used!
330 public function mergeLegacyInputParams($data) {
331 // Suppress legacy warnings for merging an array of data as this
332 // suits our migration plan at this moment. Future behaviour may differ.
333 // @see https://github.com/civicrm/civicrm-core/pull/17643
334 $this->suppressLegacyWarnings
= TRUE;
335 foreach ($data as $key => $value) {
336 if ($value !== NULL && $value !== '') {
337 $this->offsetSet($key, $value);
340 $this->suppressLegacyWarnings
= FALSE;
344 * Throw an exception if any of the props is unset.
346 * @param array $props Array of proper property names (no legacy aliases allowed).
348 * @return PropertyBag
350 public function require(array $props) {
352 foreach ($props as $prop) {
353 if (!isset($this->props
['default'][$prop])) {
358 throw new \
InvalidArgumentException("Required properties missing: " . implode(', ', $missing));
363 // Public getters, setters.
366 * Get a property by its name (but still using its getter).
368 * @param string $prop valid property name, like contactID
369 * @param bool $allowUnset If TRUE, return the default value if the property is
370 * not set - normal behaviour would be to throw an exception.
371 * @param mixed $default
372 * @param string $label e.g. 'default' or 'old' or 'new'
376 public function getter($prop, $allowUnset = FALSE, $default = NULL, $label = 'default') {
378 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
379 // This is a standard property that will have a getter method.
380 $getter = 'get' . ucfirst($prop);
381 return (!$allowUnset ||
$this->has($prop, $label))
382 ?
$this->$getter($label)
386 // This is not a property name we know, but they could be requesting a
388 return (!$allowUnset ||
$this->has($prop, $label))
389 ?
$this->getCustomProperty($prop, $label)
394 * Set a property by its name (but still using its setter).
396 * @param string $prop valid property name, like contactID
397 * @param mixed $value
398 * @param string $label e.g. 'default' or 'old' or 'new'
402 public function setter($prop, $value = NULL, $label = 'default') {
403 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
404 // This is a standard property.
405 $setter = 'set' . ucfirst($prop);
406 return $this->$setter($value, $label);
408 // We don't allow using the setter for custom properties.
409 throw new \
BadMethodCallException("Cannot use generic setter with non-standard properties; you must use setCustomProperty for custom properties.");
413 * Get the monetary amount.
415 public function getAmount($label = 'default') {
416 return $this->get('amount', $label);
420 * Set the monetary amount.
422 * - We expect to be called with a string amount with optional decimals using
423 * a '.' as the decimal point (not a ',').
425 * - We're ok with floats/ints being passed in, too, but we'll cast them to a
428 * - Negatives are fine.
430 * @see https://github.com/civicrm/civicrm-core/pull/18219
432 * @param string|float|int $value
433 * @param string $label
435 public function setAmount($value, $label = 'default') {
436 if (!is_numeric($value)) {
437 throw new \
InvalidArgumentException("setAmount requires a numeric amount value");
439 return $this->set('amount', $label, filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT
, FILTER_FLAG_ALLOW_FRACTION
));
443 * BillingStreetAddress getter.
447 public function getBillingStreetAddress($label = 'default') {
448 return $this->get('billingStreetAddress', $label);
452 * BillingStreetAddress setter.
454 * @param string $input
455 * @param string $label e.g. 'default'
457 public function setBillingStreetAddress($input, $label = 'default') {
458 return $this->set('billingStreetAddress', $label, (string) $input);
462 * BillingSupplementalAddress1 getter.
466 public function getBillingSupplementalAddress1($label = 'default') {
467 return $this->get('billingSupplementalAddress1', $label);
471 * BillingSupplementalAddress1 setter.
473 * @param string $input
474 * @param string $label e.g. 'default'
476 public function setBillingSupplementalAddress1($input, $label = 'default') {
477 return $this->set('billingSupplementalAddress1', $label, (string) $input);
481 * BillingSupplementalAddress2 getter.
485 public function getBillingSupplementalAddress2($label = 'default') {
486 return $this->get('billingSupplementalAddress2', $label);
490 * BillingSupplementalAddress2 setter.
492 * @param string $input
493 * @param string $label e.g. 'default'
495 public function setBillingSupplementalAddress2($input, $label = 'default') {
496 return $this->set('billingSupplementalAddress2', $label, (string) $input);
500 * BillingSupplementalAddress3 getter.
504 public function getBillingSupplementalAddress3($label = 'default') {
505 return $this->get('billingSupplementalAddress3', $label);
509 * BillingSupplementalAddress3 setter.
511 * @param string $input
512 * @param string $label e.g. 'default'
514 public function setBillingSupplementalAddress3($input, $label = 'default') {
515 return $this->set('billingSupplementalAddress3', $label, (string) $input);
519 * BillingCity getter.
523 public function getBillingCity($label = 'default') {
524 return $this->get('billingCity', $label);
528 * BillingCity setter.
530 * @param string $input
531 * @param string $label e.g. 'default'
533 * @return \Civi\Payment\PropertyBag
535 public function setBillingCity($input, $label = 'default') {
536 return $this->set('billingCity', $label, (string) $input);
540 * BillingPostalCode getter.
544 public function getBillingPostalCode($label = 'default') {
545 return $this->get('billingPostalCode', $label);
549 * BillingPostalCode setter.
551 * @param string $input
552 * @param string $label e.g. 'default'
554 public function setBillingPostalCode($input, $label = 'default') {
555 return $this->set('billingPostalCode', $label, (string) $input);
559 * BillingCounty getter.
563 public function getBillingCounty($label = 'default') {
564 return $this->get('billingCounty', $label);
568 * BillingCounty setter.
570 * Nb. we can't validate this unless we have the country ID too, so we don't.
572 * @param string $input
573 * @param string $label e.g. 'default'
575 public function setBillingCounty($input, $label = 'default') {
576 return $this->set('billingCounty', $label, (string) $input);
580 * BillingCountry getter.
584 public function getBillingCountry($label = 'default') {
585 return $this->get('billingCountry', $label);
589 * BillingCountry setter.
591 * Nb. We require and we store a 2 character country code.
593 * @param string $input
594 * @param string $label e.g. 'default'
596 public function setBillingCountry($input, $label = 'default') {
597 if (!is_string($input) ||
strlen($input) !== 2) {
598 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
600 if (!CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'country_id', $input)) {
601 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
603 return $this->set('billingCountry', $label, (string) $input);
609 public function getContactID($label = 'default'): int {
610 return $this->get('contactID', $label);
614 * @param int $contactID
615 * @param string $label
617 public function setContactID($contactID, $label = 'default') {
618 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
619 if (!($contactID > 0)) {
620 throw new InvalidArgumentException("ContactID must be a positive integer");
623 return $this->set('contactID', $label, (int) $contactID);
627 * Getter for contributionID.
630 * @param string $label
632 public function getContributionID($label = 'default') {
633 return $this->get('contributionID', $label);
637 * @param int $contributionID
638 * @param string $label e.g. 'default'
640 public function setContributionID($contributionID, $label = 'default') {
641 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
642 if (!($contributionID > 0)) {
643 throw new InvalidArgumentException("ContributionID must be a positive integer");
646 return $this->set('contributionID', $label, (int) $contributionID);
650 * Getter for contributionRecurID.
653 * @param string $label
655 public function getContributionRecurID($label = 'default') {
656 return $this->get('contributionRecurID', $label);
660 * @param int $contributionRecurID
661 * @param string $label e.g. 'default'
663 * @return \Civi\Payment\PropertyBag
665 public function setContributionRecurID($contributionRecurID, $label = 'default') {
666 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
667 if (!($contributionRecurID > 0)) {
668 throw new InvalidArgumentException('ContributionRecurID must be a positive integer');
671 return $this->set('contributionRecurID', $label, (int) $contributionRecurID);
675 * Three character currency code.
677 * https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
679 * @param string $label e.g. 'default'
681 public function getCurrency($label = 'default') {
682 return $this->get('currency', $label);
686 * Three character currency code.
688 * @param string $value
689 * @param string $label e.g. 'default'
691 public function setCurrency($value, $label = 'default') {
692 if (!preg_match('/^[A-Z]{3}$/', $value)) {
693 throw new \
InvalidArgumentException("Attemted to setCurrency with a value that was not an ISO 3166-1 alpha 3 currency code");
695 return $this->set('currency', $label, $value);
700 * @param string $label
704 public function getDescription($label = 'default') {
705 return $this->get('description', $label);
709 * @param string $description
710 * @param string $label e.g. 'default'
712 public function setDescription($description, $label = 'default') {
713 // @todo this logic was copied from a commit that then got deleted. Is it good?
714 $uninformativeStrings = [
715 ts('Online Event Registration: '),
716 ts('Online Contribution: '),
718 $cleanedDescription = str_replace($uninformativeStrings, '', $description);
719 return $this->set('description', $label, $cleanedDescription);
727 public function getEmail($label = 'default') {
728 return $this->get('email', $label);
734 * @param string $email
735 * @param string $label e.g. 'default'
737 public function setEmail($email, $label = 'default') {
738 return $this->set('email', $label, (string) $email);
742 * Amount of money charged in fees by the payment processor.
744 * This is notified by (some) payment processers.
746 * @param string $label
750 public function getFeeAmount($label = 'default') {
751 return $this->get('feeAmount', $label);
755 * @param string $feeAmount
756 * @param string $label e.g. 'default'
758 public function setFeeAmount($feeAmount, $label = 'default') {
759 if (!is_numeric($feeAmount)) {
760 throw new \
InvalidArgumentException("feeAmount must be a number.");
762 return $this->set('feeAmount', $label, (float) $feeAmount);
770 public function getFirstName($label = 'default') {
771 return $this->get('firstName', $label);
777 * @param string $firstName
778 * @param string $label e.g. 'default'
780 public function setFirstName($firstName, $label = 'default') {
781 return $this->set('firstName', $label, (string) $firstName);
785 * Getter for invoiceID.
787 * @param string $label
789 * @return string|null
791 public function getInvoiceID($label = 'default') {
792 return $this->get('invoiceID', $label);
796 * @param string $invoiceID
797 * @param string $label e.g. 'default'
799 public function setInvoiceID($invoiceID, $label = 'default') {
800 return $this->set('invoiceID', $label, $invoiceID);
804 * Getter for isBackOffice.
806 * @param string $label
810 public function getIsBackOffice($label = 'default'):bool {
811 // @todo should this return FALSE instead of exception to keep current situation?
812 return $this->get('isBackOffice', $label);
816 * @param bool $isBackOffice
817 * @param string $label e.g. 'default'
819 public function setIsBackOffice($isBackOffice, $label = 'default') {
820 if (is_null($isBackOffice)) {
821 throw new \
InvalidArgumentException("isBackOffice must be a bool, received NULL.");
823 return $this->set('isBackOffice', $label, (bool) $isBackOffice);
827 * Getter for isRecur.
829 * @param string $label
833 public function getIsRecur($label = 'default'):bool {
834 if (!$this->has('isRecur')) {
837 return $this->get('isRecur', $label);
841 * @param bool $isRecur
842 * @param string $label e.g. 'default'
844 public function setIsRecur($isRecur, $label = 'default') {
845 if (is_null($isRecur)) {
846 throw new \
InvalidArgumentException("isRecur must be a bool, received NULL.");
848 return $this->set('isRecur', $label, (bool) $isRecur);
852 * Set whether the user has selected to notify the processor of a cancellation request.
854 * When cancelling the user may be presented with an option to notify the processor. The payment
855 * processor can take their response, if present, into account.
858 * @param string $label e.g. 'default'
860 * @return \Civi\Payment\PropertyBag
862 public function setIsNotifyProcessorOnCancelRecur($value, $label = 'default') {
863 return $this->set('isNotifyProcessorOnCancelRecur', $label, (bool) $value);
867 * Get whether the user has selected to notify the processor of a cancellation request.
869 * When cancelling the user may be presented with an option to notify the processor. The payment
870 * processor can take their response, if present, into account.
872 * @param string $label e.g. 'default'
874 * @return \Civi\Payment\PropertyBag
876 public function getIsNotifyProcessorOnCancelRecur($label = 'default') {
877 return $this->get('isNotifyProcessorOnCancelRecur', $label);
885 public function getLastName($label = 'default') {
886 return $this->get('lastName', $label);
892 * @param string $lastName
893 * @param string $label e.g. 'default'
895 public function setLastName($lastName, $label = 'default') {
896 return $this->set('lastName', $label, (string) $lastName);
900 * Getter for payment processor generated string for charging.
902 * A payment token could be a single use token (e.g generated by
903 * a client side script) or a token that permits recurring or on demand charging.
905 * The key thing is it is passed to the processor in lieu of card details to
906 * initiate a payment.
908 * Generally if a processor is going to pass in a payment token generated through
909 * javascript it would add 'payment_token' to the array it returns in it's
910 * implementation of getPaymentFormFields. This will add a hidden 'payment_token' field to
911 * the form. A good example is client side encryption where credit card details are replaced by
912 * an encrypted token using a gateway provided javascript script. In this case the javascript will
913 * remove the credit card details from the form before submitting and populate the payment_token field.
915 * A more complex example is used by paypal checkout where the payment token is generated
916 * via a pre-approval process. In that case the doPreApproval function is called on the processor
917 * class to get information to then interact with paypal via js, finally getting a payment token.
918 * (at this stage the pre-approve api is not in core but that is likely to change - we just need
919 * to think about the permissions since we don't want to expose to anonymous user without thinking
920 * through any risk of credit-card testing using it.
922 * If the token is not totally transient it would be saved to civicrm_payment_token.token.
924 * @param string $label
926 * @return string|null
928 public function getPaymentToken($label = 'default') {
929 return $this->get('paymentToken', $label);
933 * @param string $paymentToken
934 * @param string $label e.g. 'default'
936 public function setPaymentToken($paymentToken, $label = 'default') {
937 return $this->set('paymentToken', $label, $paymentToken);
945 public function getPhone($label = 'default') {
946 return $this->get('phone', $label);
952 * @param string $phone
953 * @param string $label e.g. 'default'
955 public function setPhone($phone, $label = 'default') {
956 return $this->set('phone', $label, (string) $phone);
960 * Combined with recurFrequencyUnit this gives how often billing will take place.
962 * e.g every if this is 1 and recurFrequencyUnit is 'month' then it is every 1 month.
965 public function getRecurFrequencyInterval($label = 'default') {
966 return $this->get('recurFrequencyInterval', $label);
970 * @param int $recurFrequencyInterval
971 * @param string $label e.g. 'default'
973 public function setRecurFrequencyInterval($recurFrequencyInterval, $label = 'default') {
974 if (!($recurFrequencyInterval > 0)) {
975 throw new InvalidArgumentException("recurFrequencyInterval must be a positive integer");
978 return $this->set('recurFrequencyInterval', $label, (int) $recurFrequencyInterval);
982 * Getter for recurFrequencyUnit.
983 * Combined with recurFrequencyInterval this gives how often billing will take place.
985 * e.g every if this is 'month' and recurFrequencyInterval is 1 then it is every 1 month.
988 * @param string $label
990 * @return string month|day|year
992 public function getRecurFrequencyUnit($label = 'default') {
993 return $this->get('recurFrequencyUnit', $label);
997 * @param string $recurFrequencyUnit month|day|week|year
998 * @param string $label e.g. 'default'
1000 public function setRecurFrequencyUnit($recurFrequencyUnit, $label = 'default') {
1001 if (!preg_match('/^day|week|month|year$/', $recurFrequencyUnit)) {
1002 throw new \
InvalidArgumentException("recurFrequencyUnit must be day|week|month|year");
1004 return $this->set('recurFrequencyUnit', $label, $recurFrequencyUnit);
1008 * Set the unique payment processor service provided ID for a particular subscription.
1010 * Nb. this is stored in civicrm_contribution_recur.processor_id and is NOT
1011 * in any way related to the payment processor ID.
1013 * @param string $label
1015 * @return string|null
1017 public function getRecurProcessorID($label = 'default') {
1018 return $this->get('recurProcessorID', $label);
1022 * Set the unique payment processor service provided ID for a particular
1025 * See https://github.com/civicrm/civicrm-core/pull/17292 for discussion
1026 * of how this function accepting NULL fits with standard / planned behaviour.
1028 * @param string|null $input
1029 * @param string $label e.g. 'default'
1031 * @return \Civi\Payment\PropertyBag
1033 public function setRecurProcessorID($input, $label = 'default') {
1034 if ($input === '') {
1037 if (strlen($input) > 255 ||
in_array($input, [FALSE, 0], TRUE)) {
1038 throw new \
InvalidArgumentException('processorID field has max length of 255');
1040 return $this->set('recurProcessorID', $label, $input);
1044 * Getter for payment processor generated string for the transaction ID.
1046 * Note some gateways generate a reference for the order and one for the
1047 * payment. This is for the payment reference and is saved to
1048 * civicrm_financial_trxn.trxn_id.
1050 * @param string $label
1052 * @return string|null
1054 public function getTransactionID($label = 'default') {
1055 return $this->get('transactionID', $label);
1059 * @param string $transactionID
1060 * @param string $label e.g. 'default'
1062 public function setTransactionID($transactionID, $label = 'default') {
1063 return $this->set('transactionID', $label, $transactionID);
1067 * Getter for trxnResultCode.
1069 * Additional information returned by the payment processor regarding the
1072 * This would normally be saved in civicrm_financial_trxn.trxn_result_code.
1075 * @param string $label
1077 * @return string|null
1079 public function getTrxnResultCode($label = 'default') {
1080 return $this->get('trxnResultCode', $label);
1084 * @param string $trxnResultCode
1085 * @param string $label e.g. 'default'
1087 public function setTrxnResultCode($trxnResultCode, $label = 'default') {
1088 return $this->set('trxnResultCode', $label, $trxnResultCode);
1091 // Custom Properties.
1094 * Sometimes we may need to pass in things that are specific to the Payment
1097 * @param string $prop
1098 * @param string $label e.g. 'default' or 'old' or 'new'
1101 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1103 public function getCustomProperty($prop, $label = 'default') {
1104 if (isset(static::$propMap[$prop])) {
1105 throw new \
InvalidArgumentException("Attempted to get '$prop' via getCustomProperty - must use using its getter.");
1108 if (!array_key_exists($prop, $this->props
[$label] ??
[])) {
1109 throw new \
BadMethodCallException("Property '$prop' has not been set.");
1111 return $this->props
[$label][$prop] ??
NULL;
1115 * We have to leave validation to the processor, but we can still give them a
1116 * way to store their data on this PropertyBag
1118 * @param string $prop
1119 * @param mixed $value
1120 * @param string $label e.g. 'default' or 'old' or 'new'
1122 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1124 public function setCustomProperty($prop, $value, $label = 'default') {
1125 if (isset(static::$propMap[$prop])) {
1126 throw new \
InvalidArgumentException("Attempted to set '$prop' via setCustomProperty - must use using its setter.");
1128 $this->props
[$label][$prop] = $value;