Commit 5ade3db1 authored by mattwire's avatar mattwire

Major refactor of core payment code. Support cancel subscription from CiviCRM

parent 2ed7f02a
This diff is collapsed.
This diff is collapsed.
......@@ -8,17 +8,17 @@ class CRM_Stripe_Customer {
* @param $params
*
* @return null|string
* @throws \CRM_Core_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public static function find($params) {
$requiredParams = ['is_live', 'processor_id'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (find): Missing required parameter: ' . $required);
throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (find): Missing required parameter: ' . $required);
}
}
if (empty($params['contact_id'])) {
throw new CRM_Core_Exception('Stripe Customer (find): contact_id is required');
throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (find): contact_id is required');
}
$queryParams = [
1 => [$params['contact_id'], 'String'],
......@@ -36,13 +36,13 @@ class CRM_Stripe_Customer {
*
* @param $params
*
* @throws \CRM_Core_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public static function add($params) {
$requiredParams = ['contact_id', 'customer_id', 'is_live', 'processor_id'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (add): Missing required parameter: ' . $required);
throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (add): Missing required parameter: ' . $required);
}
}
......@@ -56,12 +56,20 @@ class CRM_Stripe_Customer {
(contact_id, id, is_live, processor_id) VALUES (%1, %2, %3, %4)", $queryParams);
}
/**
* @param $params
* @param $paymentProcessor
*
* @return \Stripe\ApiResource
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public static function create($params, $paymentProcessor) {
$requiredParams = ['contact_id', 'card_token', 'is_live', 'processor_id'];
// $optionalParams = ['email'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (create): Missing required parameter: ' . $required);
throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (create): Missing required parameter: ' . $required);
}
}
......@@ -70,32 +78,31 @@ class CRM_Stripe_Customer {
'id' => $params['contact_id'],
]);
$sc_create_params = [
$stripeCustomerParams = [
'description' => $contactDisplayName . ' (CiviCRM)',
'card' => $params['card_token'],
'email' => CRM_Utils_Array::value('email', $params),
'metadata' => ['civicrm_contact_id' => $params['contact_id']],
];
$stripeCustomer = $paymentProcessor->stripeCatchErrors('create_customer', $sc_create_params, $params);
try {
$stripeCustomer = \Stripe\Customer::create($stripeCustomerParams);
}
catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('create_customer', $e, FALSE);
$errorMessage = CRM_Core_Payment_Stripe::handleErrorNotification($err, $params['stripe_error_url']);
throw new \Civi\Payment\Exception\PaymentProcessorException('Failed to create Stripe Customer: ' . $errorMessage);
}
// Store the relationship between CiviCRM's email address for the Contact & Stripe's Customer ID.
if (isset($stripeCustomer)) {
if ($paymentProcessor->isErrorReturn($stripeCustomer)) {
return $stripeCustomer;
}
$params = [
'contact_id' => $params['contact_id'],
'customer_id' => $stripeCustomer->id,
'is_live' => $params['is_live'],
'processor_id' => $params['processor_id'],
];
self::add($params);
$params = [
'contact_id' => $params['contact_id'],
'customer_id' => $stripeCustomer->id,
'is_live' => $params['is_live'],
'processor_id' => $params['processor_id'],
];
self::add($params);
}
else {
Throw new CRM_Core_Exception(ts('There was an error saving new customer within Stripe.'));
}
return $stripeCustomer;
}
......@@ -104,13 +111,13 @@ class CRM_Stripe_Customer {
*
* @param array $params
*
* @throws \CRM_Core_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public static function delete($params) {
$requiredParams = ['contact_id', 'is_live', 'processor_id'];
foreach ($requiredParams as $required) {
if (empty($required)) {
throw new CRM_Core_Exception('Stripe Customer (delete): Missing required parameter: ' . $required);
throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (delete): Missing required parameter: ' . $required);
}
}
......
<?php
/*--------------------------------------------------------------------+
| CiviCRM version 5.0 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+-------------------------------------------------------------------*/
/**
* This class implements hooks for Stripe
*/
class CRM_Stripe_Hook {
/**
* This hook allows modifying recurring contribution parameters
*
* @param array $recurContributionParams Recurring contribution params (ContributionRecur.create API parameters)
*
* @return mixed
*/
public static function updateRecurringContribution(&$recurContributionParams) {
return CRM_Utils_Hook::singleton()
->invoke(1, $recurContributionParams, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject,
CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, 'civicrm_stripe_updateRecurringContribution');
}
}
......@@ -12,8 +12,8 @@
<author>Matthew Wire (MJW Consulting)</author>
<email>mjw@mjwconsult.co.uk</email>
</maintainer>
<releaseDate>2018-10-18</releaseDate>
<version>5.2alpha1</version>
<releaseDate>2018-11-20</releaseDate>
<version>5.2alpha2</version>
<develStage>beta</develStage>
<compatibility>
<ver>5.3</ver>
......
......@@ -2,7 +2,7 @@
* @file
* JS Integration between CiviCRM & Stripe.
*/
(function($, CRM) {
CRM.$(function($) {
// Response from Stripe.createToken.
function stripeResponseHandler(status, response) {
......@@ -376,7 +376,7 @@
function debugging (errorCode) {
// Uncomment the following to debug unexpected returns.
console.log(new Date().toISOString() + ' civicrm_stripe.js: ' + errorCode);
//console.log(new Date().toISOString() + ' civicrm_stripe.js: ' + errorCode);
}
}(cj, CRM));
});
......@@ -107,7 +107,6 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
*
*/
function createPaymentProcessor($params = array()) {
$result = civicrm_api3('Stripe', 'setuptest', $params);
$processor = array_pop($result['values']);
$this->_sk = $processor['user_name'];
......@@ -163,7 +162,7 @@ class CRM_Stripe_BaseTest extends \PHPUnit_Framework_TestCase implements Headles
'invoiceID' => $this->_invoiceID,
), $params);
$ret = $stripe->doDirectPayment($params);
$ret = $stripe->doPayment($params);
if (array_key_exists('trxn_id', $ret)) {
$this->_trxn_id = $ret['trxn_id'];
......
......@@ -151,7 +151,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
'installments' => $this->_installments
);
// Note - this will succeed. It is very hard to test a failed transaction.
// We will manipulate the event to make it a failed transactin below.
// We will manipulate the event to make it a failed transaction below.
$this->doPayment($payment_extra_params);
// Now check to see if an event was triggered and if so, process it.
......
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