Commit a6536825 authored by mattwire's avatar mattwire

Switch all instances of setAPIKey to use shared function so we ensure that API...

Switch all instances of setAPIKey to use shared function so we ensure that API version and info is always set
parent d4ef68af
......@@ -33,10 +33,11 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
*
* @param string $mode
* The mode of operation: live or test.
* @param array $paymentProcessor
*
* @return void
*/
public function __construct($mode, &$paymentProcessor) {
public function __construct($mode, $paymentProcessor) {
$this->_mode = $mode;
$this->_paymentProcessor = $paymentProcessor;
$this->_processorName = ts('Stripe');
......
......@@ -16,7 +16,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
protected $transaction_id;
// By default, always retrieve the event from stripe to ensure we are
// not being fed garbage. However, allow an override so when we are
// not being fed garbage. However, allow an override so when we are
// testing, we can properly test a failed recurring contribution.
protected $verify_event = TRUE;
......@@ -39,7 +39,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
protected $frequency_unit = NULL;
protected $plan_name = NULL;
protected $plan_start = NULL;
// Derived properties.
protected $contribution_recur_id = NULL;
protected $event_id = NULL;
......@@ -119,10 +119,10 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
// Determine the proper Stripe Processor ID so we can get the secret key
// and initialize Stripe.
$this->getPaymentProcessor();
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->_paymentProcessor['id']]));
$processor->setAPIParams();
// Now re-retrieve the data from Stripe to ensure it's legit.
\Stripe\Stripe::setApiKey($this->_paymentProcessor['user_name']);
// Special case if this is the test webhook
if (substr($parameters->id, -15, 15) === '_00000000000000') {
http_response_code(200);
......@@ -194,7 +194,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
));
}
// Successful charge & more to come.
// Successful charge & more to come.
civicrm_api3('ContributionRecur', 'create', array(
'id' => $this->contribution_recur_id,
'failure_count' => 0,
......@@ -353,7 +353,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
* Gather and set info as class properties.
*
* Given the data passed to us via the Stripe Event, try to determine
* as much as we can about this event and set that information as
* as much as we can about this event and set that information as
* properties to be used later.
*
* @throws \CRM_Core_Exception
......
This diff is collapsed.
......@@ -26,7 +26,9 @@ class CRM_Stripe_Webhook {
$messageTexts = [];
$webhook_path = self::getWebhookPath($paymentProcessor['id']);
\Stripe\Stripe::setApiKey(CRM_Core_Payment_Stripe::getSecretKey($paymentProcessor));
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $paymentProcessor['id']]));
$processor->setAPIParams();
try {
$webhooks = \Stripe\WebhookEndpoint::all(["limit" => 100]);
}
......@@ -118,7 +120,8 @@ class CRM_Stripe_Webhook {
* @param int $paymentProcessorId
*/
public static function createWebhook($paymentProcessorId) {
\Stripe\Stripe::setApiKey(CRM_Core_Payment_Stripe::getSecretKeyById($paymentProcessorId));
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $paymentProcessorId]));
$processor->setAPIParams();
$params = [
'enabled_events' => self::getDefaultEnabledEvents(),
......
<?php
/**
* This api allows you to replay Stripe events.
* This api allows you to replay Stripe events.
*
* You can either pass the id of an entry in the System Log (which can
* be populated with the Stripe.PopulateLog call) or you can pass a
* event id from Stripe directly.
*
* When processing an event, the event will always be re-fetched from the
* Stripe server first, so this will not work while offline or with
* Stripe server first, so this will not work while offline or with
* events that were not generated by the Stripe server.
*/
*/
/**
* Stripe.Ipn API specification
......@@ -57,12 +57,9 @@ function civicrm_api3_stripe_Ipn($params) {
throw new API_Exception('Please pass the payment processor id (ppid) if using evtid.', 3236);
}
$ppid = $params['ppid'];
$results = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $ppid));
// YES! I know, password and user are backwards. wtf??
$sk = $results['user_name'];
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $ppid]));
$processor->setAPIParams();
require_once ("vendor/stripe/stripe-php/init.php");
\Stripe\Stripe::setApiKey($sk);
$object = \Stripe\Event::retrieve($params['evtid']);
}
// Avoid a SQL error if this one has been processed already.
......@@ -74,7 +71,7 @@ function civicrm_api3_stripe_Ipn($params) {
return civicrm_api3_create_error("Ipn already processed.");
}
if (class_exists('CRM_Core_Payment_StripeIPN')) {
// The $_GET['processor_id'] value is normally set by
// The $_GET['processor_id'] value is normally set by
// CRM_Core_Payment::handlePaymentMethod
$_GET['processor_id'] = $ppid;
$ipnClass = new CRM_Core_Payment_StripeIPN($object);
......
......@@ -9,7 +9,7 @@
/**
* Stripe.ListEvents API specification
*
*
*
* @param array $spec description of fields supported by this API call
* @return void
......@@ -17,12 +17,13 @@
*/
function _civicrm_api3_stripe_ListEvents_spec(&$spec) {
$spec['ppid']['title'] = ts("Use the given Payment Processor ID");
$spec['ppid']['type'] = CRM_Utils_Type::T_INT;
$spec['ppid']['type'] = CRM_Utils_Type::T_INT;
$spec['ppid']['api.required'] = TRUE;
$spec['type']['title'] = ts("Limit to the given Stripe events type, defaults to invoice.payment_succeeded.");
$spec['type']['api.default'] = 'invoice.payment_succeeded';
$spec['limit']['title'] = ts("Limit number of results returned (100 is max)");
$spec['starting_after']['title'] = ts("Only return results after this event id.");
$spec['output']['api.default'] = 'brief';
$spec['output']['api.default'] = 'brief';
$spec['output']['title'] = ts("How to format the output, brief or raw. Defaults to brief.");
}
......@@ -122,18 +123,17 @@ function civicrm_api3_stripe_VerifyEventType($eventType) {
* Process parameters to determine ppid and sk.
*
* @param array $params
*
* @return array
* @throws \API_Exception
*/
function civicrm_api3_stripe_ProcessParams($params) {
$ppid = NULL;
$type = NULL;
$created = NULL;
$limit = NULL;
$starting_after = NULL;
$sk = NULL;
if (array_key_exists('ppid', $params) ) {
$ppid = $params['ppid'];
}
if (array_key_exists('created', $params) ) {
$created = $params['created'];
}
......@@ -144,29 +144,6 @@ function civicrm_api3_stripe_ProcessParams($params) {
$starting_after = $params['starting_after'];
}
// Select the right payment processor to use.
if ($ppid) {
$query_params = array('id' => $ppid);
}
else {
// By default, select the live stripe processor (we expect there to be
// only one).
$query_params = array('class_name' => 'Payment_Stripe', 'is_test' => 0);
}
try {
$results = civicrm_api3('PaymentProcessor', 'getsingle', $query_params);
// YES! I know, password and user are backwards. wtf??
$sk = $results['user_name'];
}
catch (CiviCRM_API3_Exception $e) {
if(preg_match('/Expected one PaymentProcessor but/', $e->getMessage())) {
throw new API_Exception("Expected one live Stripe payment processor, but found none or more than one. Please specify ppid=.", 1234);
}
else {
throw new API_Exception("Error getting the Stripe Payment Processor to use", 1235);
}
}
// Check to see if we should filter by type.
if (array_key_exists('type', $params) ) {
// Validate - since we will be appending this to an URL.
......@@ -185,7 +162,7 @@ function civicrm_api3_stripe_ProcessParams($params) {
throw new API_Exception("Created can only be passed in programatically as an array", 1237);
}
}
return array('sk' => $sk, 'type' => $type, 'created' => $created, 'limit' => $limit, 'starting_after' => $starting_after);
return ['type' => $type, 'created' => $created, 'limit' => $limit, 'starting_after' => $starting_after];
}
/**
......@@ -199,7 +176,6 @@ function civicrm_api3_stripe_ProcessParams($params) {
*/
function civicrm_api3_stripe_Listevents($params) {
$parsed = civicrm_api3_stripe_ProcessParams($params);
$sk = $parsed['sk'];
$type = $parsed['type'];
$created = $parsed['created'];
$limit = $parsed['limit'];
......@@ -218,9 +194,10 @@ function civicrm_api3_stripe_Listevents($params) {
if ($starting_after) {
$args['starting_after'] = $starting_after;
}
require_once ("vendor/stripe/stripe-php/init.php");
\Stripe\Stripe::setApiKey($sk);
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['ppid']]));
$processor->setAPIParams();
$data_list = \Stripe\Event::all($args);
if (array_key_exists('error', $data_list)) {
$err = $data_list['error'];
......
......@@ -149,9 +149,10 @@ function civicrm_api3_stripe_subscription_import($params) {
$paymentProcessor = \Civi\Payment\System::singleton()->getById($params['payment_processor_id'])->getPaymentProcessor();
// Now re-retrieve the data from Stripe to ensure it's legit.
\Stripe\Stripe::setApiKey($paymentProcessor['user_name']);
$processor = new CRM_Core_Payment_Stripe('', $paymentProcessor);
$processor->setAPIParams();
// Now re-retrieve the data from Stripe to ensure it's legit.
$stripeSubscription = \Stripe\Subscription::retrieve($params['subscription_id']);
// Create the stripe customer in CiviCRM
......
......@@ -39,7 +39,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
// Secret/public keys are PTP test keys.
protected $_sk = 'sk_test_TlGdeoi8e1EOPC3nvcJ4q5UZ';
protected $_pk = 'pk_test_k2hELLGpBLsOJr6jZ2z9RaYh';
protected $_cc = NULL;
protected $_cc = NULL;
public function setUpHeadless() {
// Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
......@@ -111,8 +111,8 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
$processor = array_pop($result['values']);
$this->_sk = $processor['user_name'];
$this->_pk = $processor['password'];
$this->_paymentProcessor = $processor;
$this->_paymentProcessorID = $result['id'];
$this->_paymentProcessor = $processor;
$this->_paymentProcessorID = $result['id'];
}
/**
......@@ -132,9 +132,9 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
), $params);
$result = civicrm_api3('ContributionPage', 'create', $params);
$this->assertEquals(0, $result['is_error']);
$this->_contributionPageID = $result['id'];
$this->_contributionPageID = $result['id'];
}
/**
* Submit to stripe
*/
......@@ -179,8 +179,9 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
public function assertValidTrxn() {
$this->assertNotEmpty($this->_trxn_id, "A trxn id was assigned");
\Stripe\Stripe::setApiKey($this->_sk);
$found = FALSE;
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->_paymentProcessorID]));
$processor->setAPIParams();
try {
$results = \Stripe\Charge::retrieve(array( "id" => $this->_trxn_id));
$found = TRUE;
......@@ -188,10 +189,10 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
catch (Stripe_Error $e) {
$found = FALSE;
}
$this->assertTrue($found, 'Assigned trxn_id is valid.');
}
}
/**
* Create contribition
*/
......@@ -213,7 +214,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
), $params));
$this->assertEquals(0, $contribution['is_error']);
$this->_contributionID = $contribution['id'];
}
}
public function createOrganization() {
if (!empty($this->_orgID)) {
......@@ -230,7 +231,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
CRM_Member_PseudoConstant::flush('membershipType');
CRM_Core_Config::clearDBCache();
$this->createOrganization();
$params = array(
$params = array(
'name' => 'General',
'duration_unit' => 'year',
'duration_interval' => 1,
......@@ -251,5 +252,5 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
CRM_Utils_Cache::singleton()->flush();
}
}
......@@ -27,7 +27,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
protected $_frequency_interval = 1;
protected $_membershipID;
// This test is particularly dirty for some reason so we have to
// This test is particularly dirty for some reason so we have to
// force a reset.
public function setUpHeadless() {
$force = TRUE;
......@@ -71,13 +71,15 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->doPayment($payment_extra_params);
// Now check to see if an event was triggered and if so, process it.
$payment_object = $this->getEvent('invoice.payment_succeeded');
$payment_object = $this->getEvent('invoice.payment_succeeded');
if ($payment_object) {
$this->ipn($payment_object);
}
// Now that we have a recurring contribution, let's update it.
\Stripe\Stripe::setApiKey($this->_sk);
// Now that we have a recurring contribution, let's update it.
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->_paymentProcessorID]));
$processor->setAPIParams();
$sub = \Stripe\Subscription::retrieve($this->_subscriptionID);
// Create a new plan if it doesn't yet exist.
......@@ -110,7 +112,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$sub->save();
// Now check to see if an event was triggered and if so, process it.
$payment_object = $this->getEvent('customer.subscription.updated');
$payment_object = $this->getEvent('customer.subscription.updated');
if ($payment_object) {
$this->ipn($payment_object);
}
......@@ -123,8 +125,8 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
'return' => array('id'),
);
$result = civicrm_api3('ContributionRecur', 'getsingle', $params);
$newContributionRecurID = $result['id'];
$newContributionRecurID = $result['id'];
// Now ensure that the membership record is updated to have this
// new recurring contribution id.
$membership_contribution_recur_id = civicrm_api3('Membership', 'getvalue', array(
......@@ -135,7 +137,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
// Delete the new plan so we can cleanly run the next time.
$plan->delete();
}
/**
......@@ -155,7 +157,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->doPayment($payment_extra_params);
// Now check to see if an event was triggered and if so, process it.
$payment_object = $this->getEvent('invoice.payment_succeeded');
$payment_object = $this->getEvent('invoice.payment_succeeded');
if ($payment_object) {
// Now manipulate the transaction so it appears to be a failed one.
$payment_object->type = 'invoice.payment_failed';
......@@ -192,7 +194,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->doPayment($payment_extra_params);
// Now check to see if an event was triggered and if so, process it.
$payment_object = $this->getEvent('invoice.payment_succeeded');
$payment_object = $this->getEvent('invoice.payment_succeeded');
if ($payment_object) {
$this->ipn($payment_object);
}
......@@ -201,15 +203,17 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->assertEquals(1, $contribution_status_id, "Recurring payment was properly processed via a stripe event.");
// Now, cancel the subscription and ensure it is properly cancelled.
\Stripe\Stripe::setApiKey($this->_sk);
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->_paymentProcessorID]));
$processor->setAPIParams();
$sub = \Stripe\Subscription::retrieve($this->_subscriptionID);
$sub->cancel();
$sub_object = $this->getEvent('customer.subscription.deleted');
$sub_object = $this->getEvent('customer.subscription.deleted');
if ($sub_object) {
$this->ipn($sub_object);
}
$this->assertContributionRecurIsCancelled();
$this->assertContributionRecurIsCancelled();
}
public function assertContributionRecurIsCancelled() {
......@@ -239,7 +243,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$params['output'] = 'raw';
// Now try to retrieve this transaction.
$transactions = civicrm_api3('Stripe', 'listevents', $params );
$transactions = civicrm_api3('Stripe', 'listevents', $params );
foreach($transactions['values']['data'] as $transaction) {
if ($transaction->data->object->$property == $this->_subscriptionID) {
return $transaction;
......@@ -254,7 +258,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
*
*/
public function ipn($data, $verify = TRUE) {
// The $_GET['processor_id'] value is normally set by
// The $_GET['processor_id'] value is normally set by
// CRM_Core_Payment::handlePaymentMethod
$_GET['processor_id'] = $this->_paymentProcessorID;
$ipnClass = new CRM_Core_Payment_StripeIPN($data, $verify);
......@@ -272,7 +276,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
'amount' => $this->_total,
'sequential' => 1,
'installments' => $this->_installments,
'frequency_unit' => $this->_frequency_unit,
'frequency_unit' => $this->_frequency_unit,
'frequency_interval' => $this->_frequency_interval,
'invoice_id' => $this->_invoiceID,
'contribution_status_id' => 2,
......@@ -293,5 +297,5 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
$this->assertEquals(0, $contributionRecur['is_error']);
$this->_contributionRecurID = $contributionRecur['id'];
$this->_contributionID = $contributionRecur['values']['0']['api.contribution.create']['id'];
}
}
}
......@@ -51,7 +51,8 @@ while ($dao->fetch()) {
if (!$paymentProcessor) {
echo "Failed to find a stripe payment processor for recurring contrib $dao->contribution_recur_id\n";
}
Stripe::setApiKey(CRM_Core_Payment_Stripe::getSecretKey($paymentProcessor));
$processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $paymentProcessor['id']]));
$processor->setAPIParams();
try {
$results = Charge::retrieve(['id' => $dao->trxn_id]);
......
Markdown is supported
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