Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
add cli and some pre-release work
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Lott committed Nov 16, 2018
1 parent e745c9f commit 84f8fe6
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 13 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ Visit the [Releases page](https://github.com/artfulrobot/uk.artfulrobot.civicrm.

### 1b. Install it the Difficult way (developers)

This extension requires the GoCardlessPro PHP library. Here's how to install from the \*nix command line. You need [composer](https://getcomposer.org/download/)
The packaged version of this extension include the GoCardlessPro PHP libraries
and exclude some dev-only bits including the `bin`, `cli` and `tests`
directories.

This extension requires the GoCardlessPro PHP library. Here's how to install
from the \*nix command line. You need
[composer](https://getcomposer.org/download/)

$ cd /path/to/your/extensions/dir
$ git clone https://github.com/artfulrobot/uk.artfulrobot.civicrm.gocardless.git
$ cd uk.artfulrobot.civicrm.gocardless
$ which composer >/dev/null && composer install || echo You need composer, pls see https://getcomposer.org/download/ Then repeat this command. (i.e. composer install)

That should then bring in the GoCardlessPro dependency.
That should then bring in the GoCardlessPro dependency and you should be good to
go.

### <a name="createpp" id="createpp"></a> 2. Install the extension and create a payment processor

Expand Down Expand Up @@ -221,8 +228,27 @@ some.



## Developers: Importing from GoCardless

If you clone from the github repo, you'll see a cli directory. This contains a
script I used as a one-off to import some pre-existing GoCardless subscriptions.
It's not a fully fledged tool but it may help others with one-off import tasks
to build a tool for their own needs from that.

## Change log

- 1.6 "stable"!

- Membership now supported thanks to work by William Mortada @wmortada and
Aidan Saunders @aydun, and this extension is in production use by quite a few
organisations so calling this release stable.

- GoCardless forms are now pre-filled with address, email, phone numbers if
you have collected those details before passing on to GoCardless.

- Updated GoCardlessPro library to 1.7.0 just to keep up-to-date.


- 1.5beta Should now respect a limited number of installments. Previous
versions will set up an open-ended subscription. You may not have wanted that
;-) Also updated GoCardlessPro library from 1.2.0 to 1.6.0
Expand Down
2 changes: 1 addition & 1 deletion bin/package-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cd ../ || { echo "X hmm. failed to go up dir."; exit 1; }
# Remove any old versions.
rm -f "$PROJECT_NAME".zip "$PROJECT_NAME".tgz

tar czf "$PROJECT_NAME".tgz "$PROJECT_NAME" --exclude='**/.git*' --exclude='**/bin' --exclude='**/tests'
tar czf "$PROJECT_NAME".tgz "$PROJECT_NAME" --exclude='**/.git*' --exclude='**/bin' --exclude='**/tests' --exclude='**/cli'

# PR wanted: if you can get zip to behave the same way as tar, please replace this clugey hack!
mkdir temp && cd temp || { echo "X failed making temp dir to create zip file"; exit 1; }
Expand Down
146 changes: 146 additions & 0 deletions cli/import-from-gc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* @file
* This is a script provided for **developers** wanting to import existing
* GoCardless subscriptions into CiviCRM, e.g. if you were using GoCardless
* with a non-CiviCRM based system before.
*
* It is **not** ready to run, but is provided as a basis for your own import needs.
*
* You should definitely be doing this on a development copy of the site first!
* And you should definitely be doing backups etc. But you know that, of course you do.
*
* Nb. This will **not** work as a migration route from Veda's GoCardless extension.
*
* Run this using:
*
* cv scr import-from-gocardless.php
*
* It loops all (100) subscriptions at GC and looks them up in CiviCRM. It will
* report details for any not found in CiviCRM.
*
* Assumptions and notes (**important**)
*
* - It assumes that all the customers in GoCardless can be found in CiviCRM by
* looking up their email. If this is not the case either change this script or
* do a separate import first.
*
* - It does not import failed payments. (You could easily change it to do this.)
*
*/

// You may want to set a suitable limit here, especially while testing.
const GC_IMPORT_SUBSCRIPTIONS_LIMIT=100;
const GC_IMPORT_PAYMENT_INSTRUMENT_ID=23; // xxx set this to whatever the direct_debit_gc is on your site.
const GC_IMPORT_FINANCIAL_TYPE_ID=20; // xxx set this to whatever you need.


if (php_sapi_name() != 'cli') {
exit;
}

// Get GC api.
civicrm_initialize();
$gc_api = CRM_GoCardlessUtils::getApi(FALSE);
$subscriptions = $gc_api->subscriptions()->list(['params' => [
'limit' => SUBSCRIPTIONS_LIMIT,
'status' => 'active',
]]);
print count($subscriptions->records) . " active subscriptions\n";

foreach ($subscriptions->records as $subscription) {

// Try to find the ContributionRecur record. The GoCardless subscription ID
// is stored as the recurring contrib's `trxn_id`.
$recur = civicrm_api3('ContributionRecur', 'get', [
'trxn_id' => $subscription->id,
]);

if ($recur['count'] == 0) {
// CiviCRM does not know this subscription.
$mandate = $gc_api->mandates()->get($subscription->links->mandate);
$customer = $gc_api->customers()->get($mandate->links->customer);

// Find Contact in CiviCRM by the email. (See assumptions.)
$contact = civicrm_api3('Contact', 'get', ['email' => $customer->email, 'sequential' => 1]);
print "No recur record for subscription $subscription->id $customer->email $customer->given_name $customer->family_name. Found $contact[count] in CiviCRM.\n";
$yn = '';
if ($contact['count'] == 1) {
print "Create recurring contribution? (N)";
$yn = strtoupper(trim(fgets(STDIN)));
}
if ($yn != 'Y') {
continue;
}

// Create recurring contribution and related successful contributions.
print "looking up payments\n";
$payments = $gc_api->payments()->list(['params' => [
'subscription' => $subscription->id,
]]);
$payments_to_copy = [];
foreach ($payments->records as $payment) {
if ($payment->status == 'confirmed' || $payment->status == 'paid_out') {
$payments_to_copy[] = [
'receive_date' => $payment->charge_date,
'total_amount' => $payment->amount/100,
];
}
else print "Not importing $payment->status payment $payment->id\n";
}

$contact = $contact['values'][0];
$params = [
'contact_id' => $contact['id'],
'amount' => $subscription->amount / 100,
'currency' => 'GBP',
"frequency_unit" => preg_replace('/ly$/', '', $subscription->interval_unit),
"frequency_interval" => $subscription->interval,
"start_date" => $subscription->start_date,
"create_date" => $subscription->start_date,
"modified_date" => $subscription->start_date,
"end_date" => $subscription->end_date,
"trxn_id" => $subscription->id,
"contribution_status_id" => 1, // 1:Completed, 2: pending, 3: cancelled, 4: failed, 5: in progress ...)
"is_test" => 0,
"cycle_day" => 1,
"payment_processor_id" => 3, // GoCardless, Live
"financial_type_id" => 20, // Donation (regular)
"payment_instrument_id" => 23, // named direct_debit_gc
];
print "creating...with " . json_encode($params, JSON_PRETTY_PRINT);
$result = civicrm_api3('ContributionRecur', 'create', $params);
if ($result['id']) {
$recur_id = $result['id'];
print "✔ Created ContributionRecur $recur_id\n";
foreach ($payments_to_copy as $_) {
$_ += [
'contact_id' => $contact['id'],
"payment_instrument_id" => GC_IMPORT_PAYMENT_INSTRUMENT_ID,
//"trxn_id" => '', // Think this should probably be set to something. xxx
'currency' => 'GBP',
"financial_type_id" => GC_IMPORT_FINANCIAL_TYPE_ID,
'contribution_recur_id' => $recur_id,
'is_test' => 0,
'contribution_status_id' => 1, // 1: Completed.
];
print json_encode($_, JSON_PRETTY_PRINT);
$result = civicrm_api3('Contribution', 'create', $_);
if (!$result['is_error']) {
print "✔ Created payment $result[id]\n";
}
else {
print_r($result);
exit;
}
}
}
else {
print_r($result);
exit;
}
}
else {
//print json_encode($recur['values'][$recur['id']], JSON_PRETTY_PRINT);
}
}
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
<url desc="Support">https://github.com/artfulrobot/uk.artfulrobot.civicrm.gocardless</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2017-01-16</releaseDate>
<version>1.5-beta</version>
<develStage>beta</develStage>
<releaseDate>2018-11-16</releaseDate>
<version>1.6</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
<ver>5.0</ver>
<ver>5.3</ver>
<ver>5.8</ver>
</compatibility>
<comments>This is a stand-alone extension. It may conflict with Veda
Consulting's UK Direct Debit and GoCardlessDD extensions; uninstall those
before installing this. Note that uninstalling a payment processor that has
payments will kill your CiviCRM install(!). You can just disable it
instead. If you need SmartDebit as well as GoCardless then you'll need the
UK Direct Debit one, but running these together is untested.</comments>
UK Direct Debit one, but running these together is untested.
</comments>
<civix>
<namespace>CRM/GoCardless</namespace>
</civix>
Expand Down

0 comments on commit 84f8fe6

Please sign in to comment.