0

I am building a custom plugin to handle Memberships in Wordpress (CiviCRM version 5.50.1). I am using the Order and Payment APIs (version 3 - there doesn't seem to be anything in version 4?) to do this.

My code looks like this for the Order:

$params = [
  'membership_type_id' => $price_field_value[0]['membership_type_id'],
  'contact_id' => $contact_id,
  'status_id' => 'Pending',
  'source' => ...,
];

$order_data = [
  'contact_id' => $contact_id,
  'total_amount' => $total_amount,
  'financial_type_id' => $settings['civicrm_contribution_financial_type'],
  'payment_instrument_id' => $settings['civicrm_contribution_payment_instrument'],
  'receive_date' => $date->format('Y-m-d H:i:s'),
  'contribution_page_id' => $contribution_page_id,
  'contribution_status_id' => 'Pending',
  'contribution_source' => ...,
  'line_items' => [
      [
          'params' => $params,
          'line_item' => [
              [
                  'entity_table' => 'civicrm_membership',
                  'price_field_id' => $price_field_value[0]['price_field_id'],
                  'price_field_value_id' => $price_field_value[0]['id'],
                  'qty' => '1',
                  'unit_price' => $total_amount,
                  'line_total' => $total_amount,
              ]
          ]
      ]
  ]
];

$order = civicrm_api3('Order', 'create', $order_data);

And like this for the payment:

return civicrm_api3('Payment', 'create', [
  'contribution_id' => $contribution_id,
  'total_amount' => $total_amount,
  'trxn_id' => $trxn_id,
  'trxn_date' => $date->format('Y-m-d H:i:s'),
  'receipt_date' => $date->format('Y-m-d H:i:s'),
  'payment_instrument_id' => $settings['civicrm_contribution_payment_instrument'],
  'is_send_contribution_notification' => 0,
]);

Once the payment is passed, I can see the completed Contribution linked to the Membership, but the Membership remains as Pending. Am I missing something?

I am having similar difficulties with updating Memberships: when I enter an existing membership in the params array, it stays as expired after payment. Could it be that both problems are related?

2
  • Can you confirm if contribution is linked with membership? Do you see related contribution when viewing membership? Jun 15, 2022 at 22:10
  • @PradeepNayak Yes, and it indicates that the contribution is paid.
    – lapmir
    Jun 16, 2022 at 20:39

1 Answer 1

0

I seem to have found a partial solution in this thread: https://forum.civicrm.org/index.php%3Ftopic=13921.0.html

I fixed the issue by changing the Pending status' weight so that it appears further down the list (as in the default settings). However, I am still having an issue with Expired memberships not having their end date changed after an order is created and a payment is passed.

3
  • do you have the required Scheduled Job running?
    – petednz - fuzion
    Jun 26, 2022 at 20:32
  • @petednz-fuzion Yes, I ran it manually and it works for every other type of membership status change.
    – lapmir
    Jun 28, 2022 at 19:51
  • is it working as expected if you use pure Civi UI rather than your custom plugin? (I don't know WP well so can't guess why you need a custom plugin.)
    – petednz - fuzion
    Jun 28, 2022 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.