diff --git a/README.md b/README.md index 3ae704f..425d549 100644 --- a/README.md +++ b/README.md @@ -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. ### 2. Install the extension and create a payment processor @@ -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 diff --git a/bin/package-release.sh b/bin/package-release.sh index f00b3e2..175d075 100755 --- a/bin/package-release.sh +++ b/bin/package-release.sh @@ -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; } diff --git a/cli/import-from-gc.php b/cli/import-from-gc.php new file mode 100644 index 0000000..cc59326 --- /dev/null +++ b/cli/import-from-gc.php @@ -0,0 +1,146 @@ +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); + } +} diff --git a/composer.lock b/composer.lock index d8cf30e..0330f96 100644 --- a/composer.lock +++ b/composer.lock @@ -1,23 +1,23 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], "content-hash": "dba616a41007c57462c7b6719bee1897", "packages": [ { "name": "gocardless/gocardless-pro", - "version": "1.6.0", + "version": "1.7.0", "source": { "type": "git", "url": "https://github.com/gocardless/gocardless-pro-php.git", - "reference": "a459565e2392886adacf05ad369c56ef432216ca" + "reference": "39413c73a6a798a62c855833e85b65c8cb9b3fbf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/gocardless/gocardless-pro-php/zipball/a459565e2392886adacf05ad369c56ef432216ca", - "reference": "a459565e2392886adacf05ad369c56ef432216ca", + "url": "https://api.github.com/repos/gocardless/gocardless-pro-php/zipball/39413c73a6a798a62c855833e85b65c8cb9b3fbf", + "reference": "39413c73a6a798a62c855833e85b65c8cb9b3fbf", "shasum": "" }, "require": { @@ -56,7 +56,7 @@ "direct debit", "gocardless" ], - "time": "2018-04-26T10:37:17+00:00" + "time": "2018-05-22T12:28:03+00:00" }, { "name": "guzzlehttp/guzzle", diff --git a/info.xml b/info.xml index 2e25c9f..2dc4376 100644 --- a/info.xml +++ b/info.xml @@ -16,18 +16,22 @@ https://github.com/artfulrobot/uk.artfulrobot.civicrm.gocardless http://www.gnu.org/licenses/agpl-3.0.html - 2017-01-16 - 1.5-beta - beta + 2018-11-16 + 1.6 + stable 4.7 + 5.0 + 5.3 + 5.8 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. + UK Direct Debit one, but running these together is untested. + CRM/GoCardless