Example #1
0
 /**
  * Start
  *
  * @see Pronamic_WP_Pay_Gateway::start()
  */
 public function start(Pronamic_Pay_Payment $payment)
 {
     $request = new Pronamic_WP_Pay_Gateways_Mollie_PaymentRequest();
     $payment_method = $payment->get_method();
     $request->amount = $payment->get_amount();
     $request->description = $payment->get_description();
     $request->redirect_url = $payment->get_return_url();
     $request->webhook_url = $this->get_webhook_url();
     $request->locale = Pronamic_WP_Pay_Mollie_LocaleHelper::transform($payment->get_language());
     $request->method = Pronamic_WP_Pay_Mollie_Methods::transform($payment_method);
     if (empty($request->method) && !empty($payment_method)) {
         // Leap of faith if the WordPress payment method could not transform to a Mollie method?
         $request->method = $payment_method;
     }
     // Customer ID
     $user_id = $payment->post->post_author;
     $customer_id = $this->get_customer_id_by_wp_user_id($user_id);
     if (empty($customer_id)) {
         $customer_id = $this->client->create_customer($payment->get_email(), $payment->get_customer_name());
         if ($customer_id) {
             $this->update_wp_user_customer_id($user_id, $customer_id);
         }
     }
     $payment->set_meta('mollie_customer_id', $customer_id);
     // Subscriptions
     $subscription = $payment->get_subscription();
     $subscription_methods = array(Pronamic_WP_Pay_PaymentMethods::CREDIT_CARD, Pronamic_WP_Pay_PaymentMethods::DIRECT_DEBIT_IDEAL);
     if ($subscription && in_array($payment_method, $subscription_methods, true)) {
         if (is_object($this->client->get_error())) {
             // Set error if customer could not be created
             $this->error = $this->client->get_error();
             // Prevent subscription payment from being created without customer
             return;
         }
         $request->recurring_type = Pronamic_WP_Pay_Mollie_Recurring::RECURRING;
         $request->method = Pronamic_WP_Pay_Mollie_Methods::transform($payment_method);
         if (Pronamic_WP_Pay_PaymentMethods::DIRECT_DEBIT_IDEAL === $payment_method) {
             // Use direct debit for recurring payments with payment method `Direct Debit (mandate via iDEAL)`.
             $request->method = Pronamic_WP_Pay_Mollie_Methods::DIRECT_DEBIT;
         }
         if ($subscription->has_valid_payment() && !$customer_id) {
             // Get customer ID from first payment
             $first = $subscription->get_first_payment();
             $customer_id = $first->get_meta('mollie_customer_id');
             $payment->set_meta('mollie_customer_id', $customer_id);
         }
         $can_user_interact = in_array($payment->get_source(), array('gravityformsideal'), true);
         // Mandate payment method to check for.
         $mandate_method = $payment_method;
         if (Pronamic_WP_Pay_PaymentMethods::DIRECT_DEBIT_IDEAL === $mandate_method) {
             $mandate_method = Pronamic_WP_Pay_PaymentMethods::DIRECT_DEBIT;
         }
         if (!$this->client->has_valid_mandate($customer_id, $mandate_method) && (!$subscription->has_valid_payment() || $can_user_interact)) {
             // First payment or if user interaction is possible and no valid mandates are found
             $request->recurring_type = Pronamic_WP_Pay_Mollie_Recurring::FIRST;
             if (Pronamic_WP_Pay_PaymentMethods::DIRECT_DEBIT_IDEAL === $payment_method) {
                 // Use iDEAL for first payments with payment method `Direct Debit (mandate via iDEAL)`.
                 $request->method = Pronamic_WP_Pay_Mollie_Methods::IDEAL;
             }
         }
         if (Pronamic_WP_Pay_Mollie_Recurring::RECURRING === $request->recurring_type) {
             // Recurring payment
             $payment->set_action_url($payment->get_return_url());
             if ($subscription->has_valid_payment()) {
                 // Use subscription amount if this is not the initial payment.
                 $payment->amount = $subscription->get_amount();
             }
         }
     }
     if (Pronamic_WP_Pay_PaymentMethods::IDEAL === $payment_method) {
         // If payment method is iDEAL we set the user chosen issuer ID.
         $request->issuer = $payment->get_issuer();
     }
     $request->customer_id = $customer_id;
     $result = $this->client->create_payment($request);
     if (!$result) {
         $this->error = $this->client->get_error();
         return;
     }
     if ($subscription && Pronamic_WP_Pay_Mollie_Recurring::RECURRING === $request->recurring_type) {
         $subscription->set_status(Pronamic_WP_Pay_Mollie_Statuses::transform($result->status));
     }
     $payment->set_transaction_id($result->id);
     if ('' === $payment->get_action_url()) {
         $payment->set_action_url($result->links->paymentUrl);
     }
 }
Example #2
0
 /**
  * Get formatted date and time of first valid mandate.
  *
  * @param $customer_id
  *
  * @return string
  */
 public function get_first_valid_mandate_datetime($customer_id, $payment_method = '')
 {
     $mandates = $this->get_mandates($customer_id);
     if ($mandates) {
         $valid_mandates = array();
         $mollie_method = Pronamic_WP_Pay_Mollie_Methods::transform($payment_method);
         foreach ($mandates->data as $mandate) {
             if ('' !== $payment_method && $mollie_method !== $mandate->method) {
                 continue;
             }
             if ('valid' === $mandate->status) {
                 $valid_mandates[$mandate->createdDatetime] = $mandate;
             }
         }
         ksort($valid_mandates);
         $mandate = array_shift($valid_mandates);
         return sprintf(__('%1$s at %2$s', 'pronamic_ideal'), get_date_from_gmt($mandate->createdDatetime, get_option('date_format')), get_date_from_gmt($mandate->createdDatetime, get_option('time_format')));
     }
     return null;
 }