function mollie_payment_listener in Mollie Payment 7.2
Same name and namespace in other branches
- 7 mollie_payment.module \mollie_payment_listener()
Listener callback.
Mollie calls this after the payment status has been changed. Mollie only gives us an id leaving us with the responsibility to get the payment status.
Parameters
string $pid: The id of the payment.
1 string reference to 'mollie_payment_listener'
- mollie_payment_menu in ./
mollie_payment.module - Implements hook_menu().
File
- ./
mollie_payment.module, line 400 - Provides Mollie integration for the Payment platform.
Code
function mollie_payment_listener($pid) {
// Load the Payment payment.
/** @var Payment $payment */
$payment = entity_load_single('payment', $pid);
// Fetch the Mollie payment id.
$parameters = drupal_get_query_parameters($_POST);
$payment_id = $parameters['id'];
// Initialize the client.
$client = mollie_payment_get_client($payment);
if ($payment && $client) {
/** @var \Mollie\Api\Resources\Payment $mollie_payment */
$mollie_payment = $client->payments
->get($payment_id);
// Update the status of the Payment payment.
mollie_payment_update_status($payment, $mollie_payment->status);
// Create a subscription once the mandate is pending or valid.
if (module_exists('payment_recurring')) {
/** @var \Mollie\Api\Resources\Customer $customer */
$customer = $client->customers
->get($mollie_payment->customerId);
$recurring_info = payment_recurring_recurring_payment_info($payment);
// There is no subscription yet.
if (!empty($recurring_info) && !isset($payment->context_data['payment']['subscription'])) {
// This is a recurring payment.
if (in_array($recurring_info['type'], array(
'subscription',
'installments',
))) {
$controller_data = $payment->method->controller_data;
$recurring_listener_path = MOLLIE_PAYMENT_RECURRING_LISTENER_PATH;
if (!empty($controller_data['webhook_base_url'])) {
$recurring_listener_path = $controller_data['webhook_base_url'] . '/' . $recurring_listener_path;
}
$mandate_status = array(
\Mollie\Api\Types\MandateStatus::STATUS_PENDING,
\Mollie\Api\Types\MandateStatus::STATUS_VALID,
);
foreach ($customer
->mandates() as $mandate) {
if (in_array($mandate->status, $mandate_status)) {
// We are allowed to charge automatically.
$subscription_data = array(
'amount' => array(
'value' => $payment
->totalAmount(TRUE),
'currency' => $payment->currency_code,
),
'interval' => $recurring_info['interval'],
'description' => $payment->description,
'webhookUrl' => url($recurring_listener_path . '/' . $payment->pid, array(
'absolute' => TRUE,
)),
);
if ($recurring_info['type'] == 'installments' && isset($recurring_info['times'])) {
$subscription_data['times'] = $recurring_info['times'];
}
if (isset($recurring_info['startDate'])) {
$subscription_data['startDate'] = $recurring_info['startDate'];
}
/** @var \Mollie\Api\Resources\Subscription $subscription */
$subscription = $customer
->createSubscription($subscription_data);
if ($subscription) {
$payment->context_data['payment']['customer'] = $subscription->customerId;
$payment->context_data['payment']['subscription'] = $subscription->id;
entity_save('payment', $payment);
}
}
}
}
}
}
}
}