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
Branches
Tags
1 merge request!2096.8
......@@ -57,7 +57,7 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer {
* @throws \CiviCRM_API3_Exception
* @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'];
foreach ($requiredParams as $required) {
if (empty($params[$required])) {
......@@ -81,24 +81,27 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer {
/**
* Update the metadata at Stripe for a given contactid
*
* @param int $contactId
* @param int $processorId optional
* @param int $contactID
*
* @return void
*/
public static function updateMetadataForContact(int $contactId, int $processorId = NULL): void {
public static function updateMetadataForContact(int $contactID): void {
$customers = StripeCustomer::get(FALSE)
->addWhere('contact_id', '=', $contactId);
if ($processorId) {
$customers = $customers->addWhere('processor_id', '=', $processorId);
}
$customers = $customers->execute();
->addWhere('contact_id', '=', $contactID)
->execute();
// Could be multiple customer_id's and/or stripe processors
foreach ($customers as $customer) {
/** @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']);
CRM_Stripe_BAO_StripeCustomer::updateMetadata(
['contact_id' => $contactId, 'processor_id' => $customer['processor_id']],
['contact_id' => $contactID, 'processor_id' => $customer['processor_id']],
$stripe,
$customer['customer_id']
);
......
......@@ -87,8 +87,6 @@ class UpdateStripe extends \Civi\Api4\Generic\AbstractAction {
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$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);
$result->exchangeArray($stripeCustomer->toArray());
......
......@@ -20,4 +20,14 @@ class StripeCustomer extends Generic\DAOEntity {
->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) {
* Implements hook_civicrm_post().
*/
function stripe_civicrm_post($op, $objectName, $objectId, &$objectRef) {
try {
if ($objectName == 'Contact' && $op == 'merge') {
CRM_Stripe_BAO_StripeCustomer::updateMetadataForContact($objectId);
}
}
catch (Exception $e) {
\Civi::log(E::SHORT_NAME)->error('Stripe Contact Merge failed: ' . $e->getMessage());
switch ($objectName) {
case 'Contact':
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());
}
}
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 {
->willReturn(
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
......
......@@ -388,6 +388,10 @@ class PropertySpy implements ArrayAccess, Iterator, Countable, JsonSerializable
return array_key_exists(key($this->_props), $this->_props);
}
public function toArray() {
return $this->_props;
}
public function __construct($name, $props) {
$this->_name = $name;
foreach ($props as $k => $v) {
......
......@@ -21,11 +21,12 @@ use Civi\Test\CiviEnvBuilder;
* @group headless
*/
require_once('BaseTest.php');
require_once('ApiTest.php');
class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest {
/**
* Test contact merging
*
*
* So far, only looks at the Civi side of things
*/
public function testMerge():void {
......@@ -83,6 +84,7 @@ class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest {
->first();
$this->assertEquals($res['is_deleted'], 1);
// update saved contact_id
$this->contactID = $new_id;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment