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?