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 'total_amount' => 'amount',
30 'billingStreetAddress' => TRUE,
31 'billing_street_address' => 'billingStreetAddress',
32 'street_address' => 'billingStreetAddress',
33 'billingSupplementalAddress1' => TRUE,
34 'billingSupplementalAddress2' => TRUE,
35 'billingSupplementalAddress3' => TRUE,
36 'billingCity' => TRUE,
37 'billing_city' => 'billingCity',
38 'city' => 'billingCity',
39 'billingPostalCode' => TRUE,
40 'billing_postal_code' => 'billingPostalCode',
41 'postal_code' => 'billingPostalCode',
42 'billingCounty' => TRUE,
43 'billingStateProvince' => TRUE,
44 'billing_state_province' => 'billingStateProvince',
45 'state_province' => 'billingStateProvince',
46 'billingCountry' => TRUE,
48 'contact_id' => 'contactID',
49 'contributionID' => TRUE,
50 'contribution_id' => 'contributionID',
51 'contributionRecurID' => TRUE,
52 'contribution_recur_id' => 'contributionRecurID',
54 'currencyID' => 'currency',
55 'description' => TRUE,
58 'fee_amount' => 'feeAmount',
59 'first_name' => 'firstName',
62 'invoice_id' => 'invoiceID',
63 'isBackOffice' => TRUE,
64 'is_back_office' => 'isBackOffice',
66 'is_recur' => 'isRecur',
67 'last_name' => 'lastName',
69 'paymentToken' => TRUE,
70 'payment_token' => 'paymentToken',
72 'recurFrequencyInterval' => TRUE,
73 'frequency_interval' => 'recurFrequencyInterval',
74 'recurFrequencyUnit' => TRUE,
75 'frequency_unit' => 'recurFrequencyUnit',
76 'recurInstallments' => TRUE,
77 'installments' => 'recurInstallments',
78 'subscriptionId' => 'recurProcessorID',
79 'recurProcessorID' => TRUE,
80 'transactionID' => TRUE,
81 'transaction_id' => 'transactionID',
82 'trxnResultCode' => TRUE,
83 'isNotifyProcessorOnCancelRecur' => TRUE,
89 * Temporary, internal variable to help ease transition to PropertyBag.
90 * Used by cast() to suppress legacy warnings.
91 * For paymentprocessors that have not converted to propertyBag we need to support "legacy" properties - eg. "is_recur"
92 * without warnings. Setting this allows us to pass a propertyBag into doPayment() and expect it to "work" with
93 * existing payment processors.
95 protected $suppressLegacyWarnings = TRUE;
98 * Get the value of the suppressLegacyWarnings parameter
101 public function getSuppressLegacyWarnings() {
102 return $this->suppressLegacyWarnings
;
106 * Set the suppressLegacyWarnings parameter - useful for unit tests.
107 * Eg. you could set to FALSE for unit tests on a paymentprocessor to capture use of legacy keys in that processor
109 * @param bool $suppressLegacyWarnings
111 public function setSuppressLegacyWarnings(bool $suppressLegacyWarnings) {
112 $this->suppressLegacyWarnings
= $suppressLegacyWarnings;
116 * Get the property bag.
118 * This allows us to swap a 'might be an array might be a property bag'
119 * variable for a known PropertyBag.
121 * @param \Civi\Payment\PropertyBag|array $params
123 * @return \Civi\Payment\PropertyBag
125 public static function cast($params) {
126 if ($params instanceof self
) {
129 $propertyBag = new self();
130 $propertyBag->mergeLegacyInputParams($params);
135 * Just for unit testing.
142 * Implements ArrayAccess::offsetExists
144 * @param mixed $offset
145 * @return bool TRUE if we have that value (on our default store)
147 public function offsetExists ($offset): bool {
148 $prop = $this->handleLegacyPropNames($offset, TRUE);
149 // If there's no prop, assume it's a custom property.
150 $prop = $prop ??
$offset;
151 return array_key_exists($prop, $this->props
['default']);
155 * Implements ArrayAccess::offsetGet
157 * @param mixed $offset
160 public function offsetGet($offset) {
162 $prop = $this->handleLegacyPropNames($offset);
164 catch (InvalidArgumentException
$e) {
166 if (!$this->getSuppressLegacyWarnings()) {
167 CRM_Core_Error
::deprecatedFunctionWarning(
168 "proper getCustomProperty('$offset') for non-core properties. "
170 "PropertyBag array access to get '$offset'"
175 return $this->getCustomProperty($offset, 'default');
177 catch (BadMethodCallException
$e) {
178 CRM_Core_Error
::deprecatedFunctionWarning(
179 "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."
181 "PropertyBag array access to get unset property '$offset'"
187 if (!$this->getSuppressLegacyWarnings()) {
188 CRM_Core_Error
::deprecatedFunctionWarning(
189 "get" . ucfirst($offset) . "()",
190 "PropertyBag array access for core property '$offset'"
193 return $this->get($prop, 'default');
197 * Implements ArrayAccess::offsetSet
199 * @param mixed $offset
200 * @param mixed $value
202 public function offsetSet($offset, $value) {
204 $prop = $this->handleLegacyPropNames($offset);
206 catch (InvalidArgumentException
$e) {
207 // We need to make a lot of noise here, we're being asked to merge in
208 // something we can't validate because we don't know what this property is.
209 // This is fine if it's something particular to a payment processor
210 // (which should be using setCustomProperty) however it could also lead to
211 // things like 'my_weirly_named_contact_id'.
213 // From 5.28 we suppress this when using PropertyBag::cast() to ease transition.
214 if (!$this->suppressLegacyWarnings
) {
215 CRM_Core_Error
::deprecatedFunctionWarning(
216 "proper setCustomProperty('$offset', \$value) for non-core properties. "
218 "PropertyBag array access to set '$offset'"
221 $this->setCustomProperty($offset, $value, 'default');
225 // Coerce legacy values that were in use but shouldn't be in our new way of doing things.
226 if ($prop === 'feeAmount' && $value === '') {
227 // At least the following classes pass in ZLS for feeAmount.
228 // CRM_Core_Payment_AuthorizeNetTest::testCreateSingleNowDated
229 // CRM_Core_Payment_AuthorizeNetTest::testCreateSinglePostDated
233 // These lines are here (and not in try block) because the catch must only
234 // catch the case when the prop is custom.
235 $setter = 'set' . ucfirst($prop);
236 if (!$this->suppressLegacyWarnings
) {
237 CRM_Core_Error
::deprecatedFunctionWarning(
239 "PropertyBag array access to set core property '$offset'"
242 $this->$setter($value, 'default');
246 * Implements ArrayAccess::offsetUnset
248 * @param mixed $offset
250 public function offsetUnset ($offset) {
251 $prop = $this->handleLegacyPropNames($offset);
252 unset($this->props
['default'][$prop]);
256 * @param string $prop
257 * @param bool $silent if TRUE return NULL instead of throwing an exception. This is because offsetExists should be safe and not throw exceptions.
258 * @return string canonical name.
259 * @throws \InvalidArgumentException if prop name not known.
261 protected function handleLegacyPropNames($prop, $silent = FALSE) {
262 $newName = static::$propMap[$prop] ??
NULL;
263 if ($newName === TRUE) {
264 // Good, modern name.
267 // Handling for legacy addition of billing details.
268 if ($newName === NULL && substr($prop, -2) === '-' . \CRM_Core_BAO_LocationType
::getBilling()
269 && isset(static::$propMap[substr($prop, 0, -2)])
271 $newName = substr($prop, 0, -2);
274 if ($newName === NULL) {
276 // Only for use by offsetExists
279 throw new \
InvalidArgumentException("Unknown property '$prop'.");
281 // Remaining case is legacy name that's been translated.
282 if (!$this->getSuppressLegacyWarnings()) {
283 CRM_Core_Error
::deprecatedFunctionWarning("Canonical property name '$newName'", "Legacy property name '$prop'");
292 * @param mixed $prop Valid property name
293 * @param string $label e.g. 'default'
297 protected function get($prop, $label) {
298 if (array_key_exists($prop, $this->props
[$label] ??
[])) {
299 return $this->props
[$label][$prop];
301 throw new \
BadMethodCallException("Property '$prop' has not been set.");
307 * @param mixed $prop Valid property name
308 * @param string $label e.g. 'default'
309 * @param mixed $value
311 * @return PropertyBag $this object so you can chain set setters.
313 protected function set($prop, $label = 'default', $value) {
314 $this->props
[$label][$prop] = $value;
321 protected function coercePseudoConstantStringToInt(string $baoName, string $field, $input) {
322 if (is_numeric($input)) {
323 // We've been given a numeric ID.
326 elseif (is_string($input)) {
327 // We've been given a named instrument.
328 $_ = (int) CRM_Core_PseudoConstant
::getKey($baoName, $field, $input);
331 throw new InvalidArgumentException("Expected an integer ID or a String name for $field.");
334 throw new InvalidArgumentException("Expected an integer greater than 0 for $field.");
341 public function has($prop, $label = 'default') {
342 // We do NOT translate legacy prop names since only new code should be
343 // using this method, and new code should be using canonical names.
344 // $prop = $this->handleLegacyPropNames($prop);
345 return isset($this->props
[$label][$prop]);
349 * This is used to merge values from an array.
350 * It's a transitional, internal function and should not be used!
354 public function mergeLegacyInputParams($data) {
355 // Suppress legacy warnings for merging an array of data as this
356 // suits our migration plan at this moment. Future behaviour may differ.
357 // @see https://github.com/civicrm/civicrm-core/pull/17643
358 $suppressLegacyWarnings = $this->getSuppressLegacyWarnings();
359 $this->setSuppressLegacyWarnings(TRUE);
360 foreach ($data as $key => $value) {
361 if ($value !== NULL && $value !== '') {
362 $this->offsetSet($key, $value);
365 $this->setSuppressLegacyWarnings($suppressLegacyWarnings);
369 * Throw an exception if any of the props is unset.
371 * @param array $props Array of proper property names (no legacy aliases allowed).
373 * @return PropertyBag
375 public function require(array $props) {
377 foreach ($props as $prop) {
378 if (!isset($this->props
['default'][$prop])) {
383 throw new \
InvalidArgumentException("Required properties missing: " . implode(', ', $missing));
388 // Public getters, setters.
391 * Get a property by its name (but still using its getter).
393 * @param string $prop valid property name, like contactID
394 * @param bool $allowUnset If TRUE, return the default value if the property is
395 * not set - normal behaviour would be to throw an exception.
396 * @param mixed $default
397 * @param string $label e.g. 'default' or 'old' or 'new'
401 public function getter($prop, $allowUnset = FALSE, $default = NULL, $label = 'default') {
403 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
404 // This is a standard property that will have a getter method.
405 $getter = 'get' . ucfirst($prop);
406 return (!$allowUnset ||
$this->has($prop, $label))
407 ?
$this->$getter($label)
411 // This is not a property name we know, but they could be requesting a
413 return (!$allowUnset ||
$this->has($prop, $label))
414 ?
$this->getCustomProperty($prop, $label)
419 * Set a property by its name (but still using its setter).
421 * @param string $prop valid property name, like contactID
422 * @param mixed $value
423 * @param string $label e.g. 'default' or 'old' or 'new'
427 public function setter($prop, $value = NULL, $label = 'default') {
428 if ((static::$propMap[$prop] ??
NULL) === TRUE) {
429 // This is a standard property.
430 $setter = 'set' . ucfirst($prop);
431 return $this->$setter($value, $label);
433 // We don't allow using the setter for custom properties.
434 throw new \
BadMethodCallException("Cannot use generic setter with non-standard properties; you must use setCustomProperty for custom properties.");
438 * Get the monetary amount.
440 public function getAmount($label = 'default') {
441 return $this->get('amount', $label);
445 * Set the monetary amount.
447 * - We expect to be called with a string amount with optional decimals using
448 * a '.' as the decimal point (not a ',').
450 * - We're ok with floats/ints being passed in, too, but we'll cast them to a
453 * - Negatives are fine.
455 * @see https://github.com/civicrm/civicrm-core/pull/18219
457 * @param string|float|int $value
458 * @param string $label
460 public function setAmount($value, $label = 'default') {
461 if (!is_numeric($value)) {
462 throw new \
InvalidArgumentException("setAmount requires a numeric amount value");
464 return $this->set('amount', $label, filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT
, FILTER_FLAG_ALLOW_FRACTION
));
468 * BillingStreetAddress getter.
472 public function getBillingStreetAddress($label = 'default') {
473 return $this->get('billingStreetAddress', $label);
477 * BillingStreetAddress setter.
479 * @param string $input
480 * @param string $label e.g. 'default'
482 public function setBillingStreetAddress($input, $label = 'default') {
483 return $this->set('billingStreetAddress', $label, (string) $input);
487 * BillingSupplementalAddress1 getter.
491 public function getBillingSupplementalAddress1($label = 'default') {
492 return $this->get('billingSupplementalAddress1', $label);
496 * BillingSupplementalAddress1 setter.
498 * @param string $input
499 * @param string $label e.g. 'default'
501 public function setBillingSupplementalAddress1($input, $label = 'default') {
502 return $this->set('billingSupplementalAddress1', $label, (string) $input);
506 * BillingSupplementalAddress2 getter.
510 public function getBillingSupplementalAddress2($label = 'default') {
511 return $this->get('billingSupplementalAddress2', $label);
515 * BillingSupplementalAddress2 setter.
517 * @param string $input
518 * @param string $label e.g. 'default'
520 public function setBillingSupplementalAddress2($input, $label = 'default') {
521 return $this->set('billingSupplementalAddress2', $label, (string) $input);
525 * BillingSupplementalAddress3 getter.
529 public function getBillingSupplementalAddress3($label = 'default') {
530 return $this->get('billingSupplementalAddress3', $label);
534 * BillingSupplementalAddress3 setter.
536 * @param string $input
537 * @param string $label e.g. 'default'
539 public function setBillingSupplementalAddress3($input, $label = 'default') {
540 return $this->set('billingSupplementalAddress3', $label, (string) $input);
544 * BillingCity getter.
548 public function getBillingCity($label = 'default') {
549 return $this->get('billingCity', $label);
553 * BillingCity setter.
555 * @param string $input
556 * @param string $label e.g. 'default'
558 * @return \Civi\Payment\PropertyBag
560 public function setBillingCity($input, $label = 'default') {
561 return $this->set('billingCity', $label, (string) $input);
565 * BillingPostalCode getter.
569 public function getBillingPostalCode($label = 'default') {
570 return $this->get('billingPostalCode', $label);
574 * BillingPostalCode setter.
576 * @param string $input
577 * @param string $label e.g. 'default'
579 public function setBillingPostalCode($input, $label = 'default') {
580 return $this->set('billingPostalCode', $label, (string) $input);
584 * BillingCounty getter.
588 public function getBillingCounty($label = 'default') {
589 return $this->get('billingCounty', $label);
593 * BillingCounty setter.
595 * Nb. we can't validate this unless we have the country ID too, so we don't.
597 * @param string $input
598 * @param string $label e.g. 'default'
600 public function setBillingCounty($input, $label = 'default') {
601 return $this->set('billingCounty', $label, (string) $input);
605 * BillingStateProvince getter.
609 public function getBillingStateProvince($label = 'default') {
610 return $this->get('billingStateProvince', $label);
614 * BillingStateProvince setter.
616 * Nb. we can't validate this unless we have the country ID too, so we don't.
618 * @param string $input
619 * @param string $label e.g. 'default'
621 public function setBillingStateProvince($input, $label = 'default') {
622 return $this->set('billingStateProvince', $label, (string) $input);
626 * BillingCountry getter.
630 public function getBillingCountry($label = 'default') {
631 return $this->get('billingCountry', $label);
635 * BillingCountry setter.
637 * Nb. We require and we store a 2 character country code.
639 * @param string $input
640 * @param string $label e.g. 'default'
642 public function setBillingCountry($input, $label = 'default') {
643 if (!is_string($input) ||
strlen($input) !== 2) {
644 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
646 if (!CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Address', 'country_id', $input)) {
647 throw new \
InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
649 return $this->set('billingCountry', $label, (string) $input);
655 public function getContactID($label = 'default'): int {
656 return $this->get('contactID', $label);
660 * @param int $contactID
661 * @param string $label
663 public function setContactID($contactID, $label = 'default') {
664 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
665 if (!($contactID > 0)) {
666 throw new InvalidArgumentException("ContactID must be a positive integer");
669 return $this->set('contactID', $label, (int) $contactID);
673 * Getter for contributionID.
676 * @param string $label
678 public function getContributionID($label = 'default') {
679 return $this->get('contributionID', $label);
683 * @param int $contributionID
684 * @param string $label e.g. 'default'
686 public function setContributionID($contributionID, $label = 'default') {
687 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
688 if (!($contributionID > 0)) {
689 throw new InvalidArgumentException("ContributionID must be a positive integer");
692 return $this->set('contributionID', $label, (int) $contributionID);
696 * Getter for contributionRecurID.
699 * @param string $label
701 public function getContributionRecurID($label = 'default') {
702 return $this->get('contributionRecurID', $label);
706 * @param int $contributionRecurID
707 * @param string $label e.g. 'default'
709 * @return \Civi\Payment\PropertyBag
711 public function setContributionRecurID($contributionRecurID, $label = 'default') {
712 // We don't use this because it counts zero as positive: CRM_Utils_Type::validate($contactID, 'Positive');
713 if (!($contributionRecurID > 0)) {
714 throw new InvalidArgumentException('ContributionRecurID must be a positive integer');
717 return $this->set('contributionRecurID', $label, (int) $contributionRecurID);
721 * Three character currency code.
723 * https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
725 * @param string $label e.g. 'default'
727 public function getCurrency($label = 'default') {
728 return $this->get('currency', $label);
732 * Three character currency code.
734 * @param string $value
735 * @param string $label e.g. 'default'
737 public function setCurrency($value, $label = 'default') {
738 if (!preg_match('/^[A-Z]{3}$/', $value)) {
739 throw new \
InvalidArgumentException("Attemted to setCurrency with a value that was not an ISO 3166-1 alpha 3 currency code");
741 return $this->set('currency', $label, $value);
746 * @param string $label
750 public function getDescription($label = 'default') {
751 return $this->get('description', $label);
755 * @param string $description
756 * @param string $label e.g. 'default'
758 public function setDescription($description, $label = 'default') {
759 // @todo this logic was copied from a commit that then got deleted. Is it good?
760 $uninformativeStrings = [
761 ts('Online Event Registration: '),
762 ts('Online Contribution: '),
764 $cleanedDescription = str_replace($uninformativeStrings, '', $description);
765 return $this->set('description', $label, $cleanedDescription);
773 public function getEmail($label = 'default') {
774 return $this->get('email', $label);
780 * @param string $email
781 * @param string $label e.g. 'default'
783 public function setEmail($email, $label = 'default') {
784 return $this->set('email', $label, (string) $email);
788 * Amount of money charged in fees by the payment processor.
790 * This is notified by (some) payment processers.
792 * @param string $label
796 public function getFeeAmount($label = 'default') {
797 return $this->get('feeAmount', $label);
801 * @param string $feeAmount
802 * @param string $label e.g. 'default'
804 public function setFeeAmount($feeAmount, $label = 'default') {
805 if (!is_numeric($feeAmount)) {
806 throw new \
InvalidArgumentException("feeAmount must be a number.");
808 return $this->set('feeAmount', $label, (float) $feeAmount);
816 public function getFirstName($label = 'default') {
817 return $this->get('firstName', $label);
823 * @param string $firstName
824 * @param string $label e.g. 'default'
826 public function setFirstName($firstName, $label = 'default') {
827 return $this->set('firstName', $label, (string) $firstName);
831 * Getter for invoiceID.
833 * @param string $label
835 * @return string|null
837 public function getInvoiceID($label = 'default') {
838 return $this->get('invoiceID', $label);
842 * @param string $invoiceID
843 * @param string $label e.g. 'default'
845 public function setInvoiceID($invoiceID, $label = 'default') {
846 return $this->set('invoiceID', $label, $invoiceID);
850 * Getter for isBackOffice.
852 * @param string $label
856 public function getIsBackOffice($label = 'default'):bool {
857 // @todo should this return FALSE instead of exception to keep current situation?
858 return $this->get('isBackOffice', $label);
862 * @param bool $isBackOffice
863 * @param string $label e.g. 'default'
865 public function setIsBackOffice($isBackOffice, $label = 'default') {
866 if (is_null($isBackOffice)) {
867 throw new \
InvalidArgumentException("isBackOffice must be a bool, received NULL.");
869 return $this->set('isBackOffice', $label, (bool) $isBackOffice);
873 * Getter for isRecur.
875 * @param string $label
879 public function getIsRecur($label = 'default'):bool {
880 if (!$this->has('isRecur')) {
883 return $this->get('isRecur', $label);
887 * @param bool $isRecur
888 * @param string $label e.g. 'default'
890 public function setIsRecur($isRecur, $label = 'default') {
891 if (is_null($isRecur)) {
892 throw new \
InvalidArgumentException("isRecur must be a bool, received NULL.");
894 return $this->set('isRecur', $label, (bool) $isRecur);
898 * Set whether the user has selected to notify the processor of a cancellation request.
900 * When cancelling the user may be presented with an option to notify the processor. The payment
901 * processor can take their response, if present, into account.
904 * @param string $label e.g. 'default'
906 * @return \Civi\Payment\PropertyBag
908 public function setIsNotifyProcessorOnCancelRecur($value, $label = 'default') {
909 return $this->set('isNotifyProcessorOnCancelRecur', $label, (bool) $value);
913 * Get whether the user has selected to notify the processor of a cancellation request.
915 * When cancelling the user may be presented with an option to notify the processor. The payment
916 * processor can take their response, if present, into account.
918 * @param string $label e.g. 'default'
920 * @return \Civi\Payment\PropertyBag
922 public function getIsNotifyProcessorOnCancelRecur($label = 'default') {
923 return $this->get('isNotifyProcessorOnCancelRecur', $label);
931 public function getLastName($label = 'default') {
932 return $this->get('lastName', $label);
938 * @param string $lastName
939 * @param string $label e.g. 'default'
941 public function setLastName($lastName, $label = 'default') {
942 return $this->set('lastName', $label, (string) $lastName);
946 * Getter for payment processor generated string for charging.
948 * A payment token could be a single use token (e.g generated by
949 * a client side script) or a token that permits recurring or on demand charging.
951 * The key thing is it is passed to the processor in lieu of card details to
952 * initiate a payment.
954 * Generally if a processor is going to pass in a payment token generated through
955 * javascript it would add 'payment_token' to the array it returns in it's
956 * implementation of getPaymentFormFields. This will add a hidden 'payment_token' field to
957 * the form. A good example is client side encryption where credit card details are replaced by
958 * an encrypted token using a gateway provided javascript script. In this case the javascript will
959 * remove the credit card details from the form before submitting and populate the payment_token field.
961 * A more complex example is used by paypal checkout where the payment token is generated
962 * via a pre-approval process. In that case the doPreApproval function is called on the processor
963 * class to get information to then interact with paypal via js, finally getting a payment token.
964 * (at this stage the pre-approve api is not in core but that is likely to change - we just need
965 * to think about the permissions since we don't want to expose to anonymous user without thinking
966 * through any risk of credit-card testing using it.
968 * If the token is not totally transient it would be saved to civicrm_payment_token.token.
970 * @param string $label
972 * @return string|null
974 public function getPaymentToken($label = 'default') {
975 return $this->get('paymentToken', $label);
979 * @param string $paymentToken
980 * @param string $label e.g. 'default'
982 public function setPaymentToken($paymentToken, $label = 'default') {
983 return $this->set('paymentToken', $label, $paymentToken);
991 public function getPhone($label = 'default') {
992 return $this->get('phone', $label);
998 * @param string $phone
999 * @param string $label e.g. 'default'
1001 public function setPhone($phone, $label = 'default') {
1002 return $this->set('phone', $label, (string) $phone);
1006 * Combined with recurFrequencyUnit this gives how often billing will take place.
1008 * e.g every if this is 1 and recurFrequencyUnit is 'month' then it is every 1 month.
1011 public function getRecurFrequencyInterval($label = 'default') {
1012 return $this->get('recurFrequencyInterval', $label);
1016 * @param int $recurFrequencyInterval
1017 * @param string $label e.g. 'default'
1019 public function setRecurFrequencyInterval($recurFrequencyInterval, $label = 'default') {
1020 if (!($recurFrequencyInterval > 0)) {
1021 throw new InvalidArgumentException("recurFrequencyInterval must be a positive integer");
1024 return $this->set('recurFrequencyInterval', $label, (int) $recurFrequencyInterval);
1028 * Getter for recurFrequencyUnit.
1029 * Combined with recurFrequencyInterval this gives how often billing will take place.
1031 * e.g every if this is 'month' and recurFrequencyInterval is 1 then it is every 1 month.
1034 * @param string $label
1036 * @return string month|day|year
1038 public function getRecurFrequencyUnit($label = 'default') {
1039 return $this->get('recurFrequencyUnit', $label);
1043 * @param string $recurFrequencyUnit month|day|week|year
1044 * @param string $label e.g. 'default'
1046 public function setRecurFrequencyUnit($recurFrequencyUnit, $label = 'default') {
1047 if (!preg_match('/^day|week|month|year$/', $recurFrequencyUnit)) {
1048 throw new \
InvalidArgumentException("recurFrequencyUnit must be day|week|month|year");
1050 return $this->set('recurFrequencyUnit', $label, $recurFrequencyUnit);
1054 * @param string $label
1058 public function getRecurInstallments($label = 'default') {
1059 return $this->get('recurInstallments', $label);
1063 * @param int $recurInstallments
1064 * @param string $label
1066 * @return \Civi\Payment\PropertyBag
1067 * @throws \CRM_Core_Exception
1069 public function setRecurInstallments($recurInstallments, $label = 'default') {
1070 // Counts zero as positive which is ok - means no installments
1071 if (!\CRM_Utils_Type
::validate($recurInstallments, 'Positive')) {
1072 throw new InvalidArgumentException('recurInstallments must be 0 or a positive integer');
1075 return $this->set('recurInstallments', $label, (int) $recurInstallments);
1079 * Set the unique payment processor service provided ID for a particular subscription.
1081 * Nb. this is stored in civicrm_contribution_recur.processor_id and is NOT
1082 * in any way related to the payment processor ID.
1084 * @param string $label
1086 * @return string|null
1088 public function getRecurProcessorID($label = 'default') {
1089 return $this->get('recurProcessorID', $label);
1093 * Set the unique payment processor service provided ID for a particular
1096 * See https://github.com/civicrm/civicrm-core/pull/17292 for discussion
1097 * of how this function accepting NULL fits with standard / planned behaviour.
1099 * @param string|null $input
1100 * @param string $label e.g. 'default'
1102 * @return \Civi\Payment\PropertyBag
1104 public function setRecurProcessorID($input, $label = 'default') {
1105 if ($input === '') {
1108 if (strlen($input) > 255 ||
in_array($input, [FALSE, 0], TRUE)) {
1109 throw new \
InvalidArgumentException('processorID field has max length of 255');
1111 return $this->set('recurProcessorID', $label, $input);
1115 * Getter for payment processor generated string for the transaction ID.
1117 * Note some gateways generate a reference for the order and one for the
1118 * payment. This is for the payment reference and is saved to
1119 * civicrm_financial_trxn.trxn_id.
1121 * @param string $label
1123 * @return string|null
1125 public function getTransactionID($label = 'default') {
1126 return $this->get('transactionID', $label);
1130 * @param string $transactionID
1131 * @param string $label e.g. 'default'
1133 public function setTransactionID($transactionID, $label = 'default') {
1134 return $this->set('transactionID', $label, $transactionID);
1138 * Getter for trxnResultCode.
1140 * Additional information returned by the payment processor regarding the
1143 * This would normally be saved in civicrm_financial_trxn.trxn_result_code.
1146 * @param string $label
1148 * @return string|null
1150 public function getTrxnResultCode($label = 'default') {
1151 return $this->get('trxnResultCode', $label);
1155 * @param string $trxnResultCode
1156 * @param string $label e.g. 'default'
1158 public function setTrxnResultCode($trxnResultCode, $label = 'default') {
1159 return $this->set('trxnResultCode', $label, $trxnResultCode);
1162 // Custom Properties.
1165 * Sometimes we may need to pass in things that are specific to the Payment
1168 * @param string $prop
1169 * @param string $label e.g. 'default' or 'old' or 'new'
1172 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1174 public function getCustomProperty($prop, $label = 'default') {
1175 if (isset(static::$propMap[$prop])) {
1176 throw new \
InvalidArgumentException("Attempted to get '$prop' via getCustomProperty - must use using its getter.");
1179 if (!array_key_exists($prop, $this->props
[$label] ??
[])) {
1180 throw new \
BadMethodCallException("Property '$prop' has not been set.");
1182 return $this->props
[$label][$prop] ??
NULL;
1186 * We have to leave validation to the processor, but we can still give them a
1187 * way to store their data on this PropertyBag
1189 * @param string $prop
1190 * @param mixed $value
1191 * @param string $label e.g. 'default' or 'old' or 'new'
1193 * @throws InvalidArgumentException if trying to use this against a non-custom property.
1195 public function setCustomProperty($prop, $value, $label = 'default') {
1196 if (isset(static::$propMap[$prop])) {
1197 throw new \
InvalidArgumentException("Attempted to set '$prop' via setCustomProperty - must use using its setter.");
1199 $this->props
[$label][$prop] = $value;