Commit 595beb17 authored by mattwire's avatar mattwire
Browse files

Set Stripe API params in class constructor

parent 6738614d
......@@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/
use Civi\Api4\PaymentprocessorWebhook;
use CRM_Stripe_ExtensionUtil as E;
use Civi\Payment\PropertyBag;
......@@ -44,6 +45,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$secretKey = self::getSecretKey($this->_paymentProcessor);
// You can configure only one of live/test so don't initialize StripeClient if keys are blank
if (!empty($secretKey)) {
$this->setAPIParams();
$this->stripeClient = new \Stripe\StripeClient($secretKey);
}
}
......@@ -513,9 +515,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$newParams = [];
CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $newParams);
// Set our Stripe API parameters
$this->setAPIParams();
$amountFormattedForStripe = self::getAmount($params);
$email = $this->getBillingEmail($params, $propertyBag->getContactID());
......@@ -893,8 +892,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
}
}
$this->setAPIParams();
$refundParams = [
'charge' => $params['trxn_id'],
];
......@@ -1131,8 +1128,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
return ['message' => E::ts('Successfully cancelled the subscription in CiviCRM ONLY.')];
}
$this->setAPIParams();
if (!$propertyBag->has('recurProcessorID')) {
$errorMessage = E::ts('The recurring contribution cannot be cancelled (No reference (trxn_id) found).');
\Civi::log()->error($errorMessage);
......
......@@ -98,7 +98,6 @@ class CRM_Core_Payment_StripeIPN {
public function setPaymentProcessor($paymentProcessorID) {
try {
$this->_paymentProcessor = \Civi\Payment\System::singleton()->getById($paymentProcessorID);
$this->_paymentProcessor->setAPIParams();
}
catch (Exception $e) {
$this->exception('Failed to get payment processor');
......
......@@ -109,8 +109,6 @@ class CRM_Stripe_PaymentIntent {
}
}
$stripe->setAPIParams();
$intent = $stripe->stripeClient->paymentIntents->retrieve($params['id']);
$intent->cancel();
}
......@@ -129,8 +127,6 @@ class CRM_Stripe_PaymentIntent {
}
}
$stripe->setAPIParams();
$intent = $stripe->stripeClient->paymentIntents->retrieve($params['id']);
$paymentIntent = self::get($params);
$params['status'] = $intent->status;
......
......@@ -46,7 +46,6 @@ class CRM_Stripe_Webhook {
// This means we only configured live OR test and not both.
continue;
}
$processor->setAPIParams();
try {
$webhooks = $processor->stripeClient->webhookEndpoints->all(["limit" => 100]);
......@@ -202,7 +201,6 @@ class CRM_Stripe_Webhook {
*/
public static function createWebhook($paymentProcessorId) {
$processor = \Civi\Payment\System::singleton()->getById($paymentProcessorId);
$processor->setAPIParams();
$params = [
'enabled_events' => self::getDefaultEnabledEvents(),
......
......@@ -45,7 +45,6 @@ function civicrm_api3_job_process_stripe($params) {
try {
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = Civi\Payment\System::singleton()->getById($detail['payment_processor_id']);
$paymentProcessor->setAPIParams();
$intent = $paymentProcessor->stripeClient->paymentIntents->retrieve($detail['stripe_intent_id']);
$intent->cancel(['cancellation_reason' => 'abandoned']);
} catch (Exception $e) {
......
......@@ -59,7 +59,6 @@ function civicrm_api3_stripe_importcharge($params) {
// Get the payment processor and activate the Stripe API
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($params['ppid']);
$paymentProcessor->setAPIParams();
// Retrieve the Stripe charge.
$stripeCharge = $paymentProcessor->stripeClient->charges->retrieve($params['charge']);
......
......@@ -38,7 +38,6 @@ function civicrm_api3_stripe_importcustomer($params) {
// Get the payment processor and activate the Stripe API
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($params['ppid']);
$paymentProcessor->setAPIParams();
$customer = $paymentProcessor->stripeClient->customers->retrieve($params['customer']);
......
......@@ -44,7 +44,6 @@ function civicrm_api3_stripe_importcustomers($params) {
// Get the payment processor and activate the Stripe API
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($params['ppid']);
$paymentProcessor->setAPIParams();
// Prepare an array to collect the results
$results = [
......
......@@ -31,7 +31,6 @@ function civicrm_api3_stripe_importsubscription($params) {
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($params['ppid']);
$paymentProcessor->setAPIParams();
// Now re-retrieve the data from Stripe to ensure it's legit.
$stripeSubscription = $paymentProcessor->stripeClient->subscriptions->retrieve($params['subscription']);
......
......@@ -44,7 +44,6 @@ function civicrm_api3_stripe_importsubscriptions($params) {
// Get the payment processor and activate the Stripe API
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($params['ppid']);
$paymentProcessor->setAPIParams();
// Get the subscriptions from Stripe
$args = [
......
......@@ -61,7 +61,6 @@ function civicrm_api3_stripe_Ipn($params) {
if (preg_match('/processor_id=([0-9]+)$/', $data['message'], $matches)) {
$paymentProcessorID = $matches[1];
$paymentProcessor = \Civi\Payment\System::singleton()->getById($paymentProcessorID);
$paymentProcessor->setAPIParams();
}
else {
throw new API_Exception('Failed to find payment processor id in system log', 3235);
......@@ -74,7 +73,6 @@ function civicrm_api3_stripe_Ipn($params) {
}
$paymentProcessorID = $params['ppid'];
$paymentProcessor = \Civi\Payment\System::singleton()->getById($paymentProcessorID);
$paymentProcessor->setAPIParams();
$stripeObject = $paymentProcessor->stripeClient->events->retrieve($params['evtid']);
}
else {
......
......@@ -292,7 +292,6 @@ function civicrm_api3_stripe_Listevents($params) {
// Now query stripe directly to see if there are any that system log didn't record.
$processor = \Civi\Payment\System::singleton()->getById($params['ppid']);
$processor->setAPIParams();
$invoices = $processor->stripeClient->invoices->all(['subscription' => $subscription]);
$seen_invoices = [];
foreach($invoices['data'] as $invoice) {
......@@ -318,7 +317,6 @@ function civicrm_api3_stripe_Listevents($params) {
elseif ($source == 'stripe') {
// Here we need to get a singleton xxx
$processor = \Civi\Payment\System::singleton()->getById($params['ppid']);
$processor->setAPIParams();
$args = [];
if ($type) {
$args['type'] = $type;
......
......@@ -250,11 +250,10 @@ function civicrm_api3_stripe_customer_updatestripemetadata($params) {
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($customerParams['processor_id']);
$paymentProcessor->setAPIParams();
// Get the stripe customer from stripe
try {
$stripeCustomer = $paymentProcessor->stripeClient->customers->retrieve($customerId);
$paymentProcessor->stripeClient->customers->retrieve($customerId);
} catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('retrieve_customer', $e, FALSE);
$errorMessage = $paymentProcessor->handleErrorNotification($err, NULL);
......
......@@ -191,7 +191,6 @@ function civicrm_api3_stripe_paymentintent_process($params) {
/** @var CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($paymentProcessorID);
($paymentProcessor->getPaymentProcessor()['class_name'] === 'Payment_Stripe') ?: _civicrm_api3_stripe_paymentintent_returnInvalid();
$paymentProcessor->setAPIParams();
$intentParams['confirmation_method'] = 'manual';
if (empty($paymentIntentID) && empty($paymentMethodID)) {
......
......@@ -60,9 +60,6 @@ class CRM_Stripe_ApiTest extends CRM_Stripe_BaseTest {
*/
public function testImportSubscription() {
$this->mockStripeSubscription();
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($this->paymentProcessorID);
$paymentProcessor->setAPIParams();
$result = civicrm_api3('Stripe', 'importsubscription', [
'subscription' => 'sub_mock',
......@@ -111,9 +108,6 @@ class CRM_Stripe_ApiTest extends CRM_Stripe_BaseTest {
*/
public function testImportSubscriptionWithNoInvoice() {
$this->mockStripeSubscription(['hasPaidInvoice' => FALSE]);
/** @var \CRM_Core_Payment_Stripe $paymentProcessor */
$paymentProcessor = \Civi\Payment\System::singleton()->getById($this->paymentProcessorID);
$paymentProcessor->setAPIParams();
$result = civicrm_api3('Stripe', 'importsubscription', [
'subscription' => 'sub_mock',
......
......@@ -121,8 +121,6 @@ abstract class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implement
$this->paymentProcessor = $processor;
$this->paymentProcessorID = $result['id'];
$this->paymentObject = \Civi\Payment\System::singleton()->getById($result['id']);
// Set params, creates stripeClient
$this->paymentObject->setAPIParams();
}
/**
......@@ -205,7 +203,6 @@ abstract class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implement
$this->assertNotEmpty($this->trxn_id, "A trxn id was assigned");
$processor = \Civi\Payment\System::singleton()->getById($this->paymentProcessorID);
$processor->setAPIParams();
try {
$processor->stripeClient->charges->retrieve($this->trxn_id);
......
......@@ -52,7 +52,6 @@ while ($dao->fetch()) {
echo "Failed to find a stripe payment processor for recurring contrib $dao->contribution_recur_id\n";
}
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $paymentProcessor['id']]));
$processor->setAPIParams();
try {
$results = Charge::retrieve(['id' => $dao->trxn_id]);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment