Skip to content
Snippets Groups Projects
Commit 4a83f40d authored by mattwire's avatar mattwire
Browse files

Update tests and move some functions to new StripeCustomer BAO

parent f8a8ec65
No related branches found
No related tags found
1 merge request!2096.8
...@@ -57,7 +57,7 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer { ...@@ -57,7 +57,7 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer {
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException * @throws \Civi\Payment\Exception\PaymentProcessorException
*/ */
public static function updateMetadata($params, $stripe, $stripeCustomerID) { public static function updateMetadata(array $params, \CRM_Core_Payment_Stripe $stripe, string $stripeCustomerID) {
$requiredParams = ['contact_id']; $requiredParams = ['contact_id'];
foreach ($requiredParams as $required) { foreach ($requiredParams as $required) {
if (empty($params[$required])) { if (empty($params[$required])) {
...@@ -81,24 +81,27 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer { ...@@ -81,24 +81,27 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer {
/** /**
* Update the metadata at Stripe for a given contactid * Update the metadata at Stripe for a given contactid
* *
* @param int $contactId * @param int $contactID
* @param int $processorId optional *
* @return void * @return void
*/ */
public static function updateMetadataForContact(int $contactId, int $processorId = NULL): void { public static function updateMetadataForContact(int $contactID): void {
$customers = StripeCustomer::get(FALSE) $customers = StripeCustomer::get(FALSE)
->addWhere('contact_id', '=', $contactId); ->addWhere('contact_id', '=', $contactID)
if ($processorId) { ->execute();
$customers = $customers->addWhere('processor_id', '=', $processorId);
}
$customers = $customers->execute();
// Could be multiple customer_id's and/or stripe processors // Could be multiple customer_id's and/or stripe processors
foreach ($customers as $customer) { foreach ($customers as $customer) {
/** @var CRM_Core_Payment_Stripe $stripe */ /** @var CRM_Core_Payment_Stripe $stripe */
\Civi\Api4\StripeCustomer::updateStripe(FALSE)
->setPaymentProcessorID($customer['processor_id'])
->setContactID($contactID)
->setCustomerID($customer['customer_id'])
->execute()
->first();
$stripe = \Civi\Payment\System::singleton()->getById($customer['processor_id']); $stripe = \Civi\Payment\System::singleton()->getById($customer['processor_id']);
CRM_Stripe_BAO_StripeCustomer::updateMetadata( CRM_Stripe_BAO_StripeCustomer::updateMetadata(
['contact_id' => $contactId, 'processor_id' => $customer['processor_id']], ['contact_id' => $contactID, 'processor_id' => $customer['processor_id']],
$stripe, $stripe,
$customer['customer_id'] $customer['customer_id']
); );
......
...@@ -87,8 +87,6 @@ class UpdateStripe extends \Civi\Api4\Generic\AbstractAction { ...@@ -87,8 +87,6 @@ class UpdateStripe extends \Civi\Api4\Generic\AbstractAction {
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */ /** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($this->paymentProcessorID); $paymentProcessor = \Civi\Payment\System::singleton()->getById($this->paymentProcessorID);
// $stripeCustomer = $paymentProcessor->stripeClient->customers->retrieve($this->customerID);
$stripeCustomer = \CRM_Stripe_BAO_StripeCustomer::updateMetadata(['contact_id' => $this->contactID, 'email' => $this->email, 'description' => $this->description], $paymentProcessor, $this->customerID); $stripeCustomer = \CRM_Stripe_BAO_StripeCustomer::updateMetadata(['contact_id' => $this->contactID, 'email' => $this->email, 'description' => $this->description], $paymentProcessor, $this->customerID);
$result->exchangeArray($stripeCustomer->toArray()); $result->exchangeArray($stripeCustomer->toArray());
......
...@@ -20,4 +20,14 @@ class StripeCustomer extends Generic\DAOEntity { ...@@ -20,4 +20,14 @@ class StripeCustomer extends Generic\DAOEntity {
->setCheckPermissions($checkPermissions); ->setCheckPermissions($checkPermissions);
} }
/**
* @param bool $checkPermissions
*
* @return \Civi\Api4\Action\StripeCustomer\UpdateStripe
*/
public static function updateStripe($checkPermissions = TRUE) {
return (new Action\StripeCustomer\UpdateStripe(__CLASS__, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}
} }
...@@ -260,12 +260,28 @@ function stripe_civicrm_permission(&$permissions) { ...@@ -260,12 +260,28 @@ function stripe_civicrm_permission(&$permissions) {
* Implements hook_civicrm_post(). * Implements hook_civicrm_post().
*/ */
function stripe_civicrm_post($op, $objectName, $objectId, &$objectRef) { function stripe_civicrm_post($op, $objectName, $objectId, &$objectRef) {
try { switch ($objectName) {
if ($objectName == 'Contact' && $op == 'merge') { case 'Contact':
CRM_Stripe_BAO_StripeCustomer::updateMetadataForContact($objectId); if ($op === 'merge') {
} try {
} CRM_Stripe_BAO_StripeCustomer::updateMetadataForContact($objectId);
catch (Exception $e) { }
\Civi::log(E::SHORT_NAME)->error('Stripe Contact Merge failed: ' . $e->getMessage()); catch (Exception $e) {
\Civi::log(E::SHORT_NAME)->error('Stripe Contact Merge failed: ' . $e->getMessage());
}
}
case 'Email':
if ($op === 'edit') {
try {
CRM_Stripe_BAO_StripeCustomer::updateMetadataForContact($objectRef->contact_id);
}
catch (Exception $e) {
\Civi::log(E::SHORT_NAME)->error('Stripe Contact update email failed: ' . $e->getMessage());
}
}
default:
return;
} }
} }
...@@ -184,6 +184,12 @@ class CRM_Stripe_ApiTest extends CRM_Stripe_BaseTest { ...@@ -184,6 +184,12 @@ class CRM_Stripe_ApiTest extends CRM_Stripe_BaseTest {
->willReturn( ->willReturn(
new PropertySpy('customers.retrieve', ['id' => 'cus_mock']) new PropertySpy('customers.retrieve', ['id' => 'cus_mock'])
); );
$stripeClient->customers
->method('update')
->with($this->equalTo('cus_mock'))
->willReturn(
new PropertySpy('customers.update', ['id' => 'cus_mock'])
);
$mockPlan = $this->createMock('Stripe\\Plan'); $mockPlan = $this->createMock('Stripe\\Plan');
$mockPlan $mockPlan
......
...@@ -388,6 +388,10 @@ class PropertySpy implements ArrayAccess, Iterator, Countable, JsonSerializable ...@@ -388,6 +388,10 @@ class PropertySpy implements ArrayAccess, Iterator, Countable, JsonSerializable
return array_key_exists(key($this->_props), $this->_props); return array_key_exists(key($this->_props), $this->_props);
} }
public function toArray() {
return $this->_props;
}
public function __construct($name, $props) { public function __construct($name, $props) {
$this->_name = $name; $this->_name = $name;
foreach ($props as $k => $v) { foreach ($props as $k => $v) {
......
...@@ -21,11 +21,12 @@ use Civi\Test\CiviEnvBuilder; ...@@ -21,11 +21,12 @@ use Civi\Test\CiviEnvBuilder;
* @group headless * @group headless
*/ */
require_once('BaseTest.php'); require_once('BaseTest.php');
require_once('ApiTest.php');
class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest { class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest {
/** /**
* Test contact merging * Test contact merging
* *
* So far, only looks at the Civi side of things * So far, only looks at the Civi side of things
*/ */
public function testMerge():void { public function testMerge():void {
...@@ -83,6 +84,7 @@ class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest { ...@@ -83,6 +84,7 @@ class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest {
->first(); ->first();
$this->assertEquals($res['is_deleted'], 1); $this->assertEquals($res['is_deleted'], 1);
// update saved contact_id // update saved contact_id
$this->contactID = $new_id; $this->contactID = $new_id;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment