Migrate cards on file from Commerce 1.x

Created on 26 September 2017, over 7 years ago
Updated 24 June 2023, almost 2 years ago

It should be possible to migrate card numbers and customers from Drupal 7. There is already a user migration, so it is possible to match the D7 users and the migrated D8 users. Customer/card tokens ought to basically be the same.

Opening this stub so it can be planned out.

Feature request
Status

Postponed: needs info

Version

2.0

Component

Drupal Commerce 1.x

Created by

🇳🇴Norway wizonesolutions Norway

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Started messing with this.
    There are a few things that would need to be done to make it work.

    1. The card_type on 7 is not the actual card type like visa or mastercard.
    2. Would need to get the billing profiles, as there are no billing profiles associated in Drupal 7 payment methods.
    3. The remote ids are formatted differently. on 7 they are 9999999|99999999 with a pipe in the middle.

    Here is the source plugin:

    <?php
    
    namespace Drupal\MY_MODULE\Plugin\migrate\source;
    
    use Drupal\migrate\Plugin\migrate\source\SqlBase;
    use Drupal\migrate\Row;
    
    /**
     * Migrate Source plugin.
     *
     * @MigrateSource(
     *   id = "stored_payment_method"
     * )
     */
    class PaymentMethod extends SqlBase {
    
      /**
       * {@inheritdoc}
       */
      public function query() {
    
        $query = $this->select('commerce_cardonfile', 'c')
          ->fields('c', [
            'card_id',
            'uid',
            'payment_method',
            'instance_id',
            'remote_id',
            'card_type',
            'card_name',
            'card_number',
            'card_exp_month',
            'card_exp_year',
            'instance_default',
            'status',
            'created',
            'changed',
          ]);
        return $query;
      }
    
      /**
       * {@inheritdoc}
       */
      public function fields() {
        $fields = [
          'card_id' => $this->t('Card ID'),
          'uid' => $this->t('User ID'),
          'payment_method' => $this->t('Payment method'),
          'instance_id' => $this->t('Instance ID'),
          'remote_id' => $this->t('Remote ID'),
          'card_type' => $this->t('Card type'),
          'card_name' => $this->t('Card name'),
          'card_number' => $this->t('Card number'),
          'card_exp_month' => $this->t('Card expiration month'),
          'card_exp_year' => $this->t('Card expiration year'),
          'instance_default' => $this->t('Whether the card is default'),
          'status' => $this->t('Status'),
          'created' => $this->t('Created time'),
          'changed' => $this->t('Changed time'),
        ];
    
        return $fields;
      }
    
      /**
       * {@inheritdoc}
       */
      public function getIds() {
        return [
          'card_id' => [
            'type' => 'integer',
            'alias' => 'c',
          ],
        ];
      }
    
      /**
       * {@inheritdoc}
       */
      public function prepareRow(Row $row) {
    
        // // Get Field API field values.
        // foreach (array_keys($this->getFields('flagging', $row->getSourceProperty('name'))) as $field) {
        //   $flagging_id = $row->getSourceProperty('flagging_id');
        //   $row->setSourceProperty($field, $this->getFieldValues('flagging', $field, $flagging_id));
        // }
    
        return parent::prepareRow($row);
      }
    
    }
    
    langcode: en
    status: true
    dependencies: {  }
    id: commerce1_payment_method
    class: null
    field_plugin_method: null
    cck_plugin_method: null
    migration_tags: {  }
    migration_group: migrate_drupal_7
    label: 'Payment Method'
    source:
      plugin: stored_payment_method
    process:
      type:
        plugin: default_value
        default_value: credit_card
      uid:
        -
          plugin: get
          source: uid
      payment_gateway:
        -
          plugin: static_map
          source: payment_method
          map:
            authnet_acceptjs: authorize_net_accept_js
        -
          plugin: skip_on_empty
          method: row
      payment_gateway_mode:
        -
          plugin: default_value
          default_value: test
      remote_id:
        -
          plugin: get
          source: remote_id
      reusable:
        -
          plugin: default_value
          default_value: 1
      is_default:
        -
          plugin: get
          source: instance_default
      created: created
      changed: changed
      card_exp_month:
        -
          plugin: get
          source: card_exp_month
      card_exp_year:
        -
          plugin: get
          source: card_exp_year
      card_number:
        -
          plugin: get
          source: card_number
      card_type:
        -
          plugin: default_value
          default_value: visa
    
    destination:
      plugin: 'entity:commerce_payment_method'
      destination_module: commerce_payment
    migration_dependencies:
      required: { }
    
Production build 0.71.5 2024