/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $items = \Drupal::service('uc_cart.manager')->get()->getContents(); $paypal_config = $this->config('uc_paypal.settings'); if (empty($items)) { drupal_set_message($this->t('You do not have any items in your shopping cart.')); return; } list($desc, $subtotal) = _uc_paypal_product_details($items); $order = Order::create(['uid' => $this->currentUser()->id()]); $order->save(); $nvp_request = array('METHOD' => 'SetExpressCheckout', 'RETURNURL' => Url::fromRoute('uc_paypal.ec_review', [], ['absolute' => TRUE])->toString(), 'CANCELURL' => Url::fromRoute('uc_cart.cart', [], ['absolute' => TRUE])->toString(), 'AMT' => uc_currency_format($subtotal, FALSE, FALSE, '.'), 'CURRENCYCODE' => $order->getCurrency(), 'PAYMENTACTION' => $paypal_config->get('wpp_cc_txn_type') == 'authorize' ? 'Authorization' : 'Sale', 'DESC' => substr($desc, 0, 127), 'INVNUM' => $order->id() . '-' . REQUEST_TIME, 'REQCONFIRMSHIPPING' => $paypal_config->get('ec_rqconfirmed_addr'), 'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US', 'NOTIFYURL' => Url::fromRoute('uc_paypal.ipn', [], ['absolute' => TRUE])->toString(), 'LANDINGPAGE' => $paypal_config->get('ec_landingpage_style')); $order->products = $items; $order->save(); $nvp_response = uc_paypal_api_request($nvp_request, $paypal_config->get('wpp_server')); if ($nvp_response['ACK'] != 'Success') { drupal_set_message($this->t('PayPal reported an error: @code: @message', ['@code' => $nvp_response['L_ERRORCODE0'], '@message' => $nvp_response['L_LONGMESSAGE0']]), 'error'); return; } $session = \Drupal::service('session'); $session->set('cart_order', $order->id()); $session->set('TOKEN', $nvp_response['TOKEN']); $sandbox = ''; if (strpos($paypal_config->get('wpp_server'), 'sandbox') > 0) { $sandbox = 'sandbox.'; } header('Location: https://www.' . $sandbox . 'paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=' . $session->get('TOKEN')); exit; }
/** * Handles the review page for Express Checkout Shortcut Flow. * * @return \Symfony\Component\HttpFoundation\RedirectResponse|array * A redirect to the cart or a build array. */ public function ecReview() { $paypal_config = $this->config('uc_paypal.settings'); $session = \Drupal::service('session'); if (!$session->has('TOKEN') || !($order = Order::load($session->get('cart_order')))) { $session->remove('cart_order'); $session->remove('have_details'); $session->remove('TOKEN'); $session->remove('PAYERID'); drupal_set_message($this->t('An error has occurred in your PayPal payment. Please review your cart and try again.')); return $this->redirect('uc_cart.cart'); } $details = array(); if ($session->has('have_details')) { $details = $session->get('have_details'); } if (!isset($details[$order->id()])) { $nvp_request = array('METHOD' => 'GetExpressCheckoutDetails', 'TOKEN' => $session->get('TOKEN')); $nvp_response = uc_paypal_api_request($nvp_request, $paypal_config->get('uc_paypal_wpp_server')); $session->set('PAYERID', $nvp_response['PAYERID']); $shipname = SafeMarkup::checkPlain($nvp_response['SHIPTONAME']); if (strpos($shipname, ' ') > 0) { $order->delivery_first_name = substr($shipname, 0, strrpos(trim($shipname), ' ')); $order->delivery_last_name = substr($shipname, strrpos(trim($shipname), ' ') + 1); } else { $order->delivery_first_name = $shipname; $order->delivery_last_name = ''; } $order->delivery_street1 = SafeMarkup::checkPlain($nvp_response['SHIPTOSTREET']); $order->delivery_street2 = isset($nvp_response['SHIPTOSTREET2']) ? SafeMarkup::checkPlain($nvp_response['SHIPTOSTREET2']) : ''; $order->delivery_city = SafeMarkup::checkPlain($nvp_response['SHIPTOCITY']); $order->delivery_zone = $nvp_response['SHIPTOSTATE']; $order->delivery_postal_code = SafeMarkup::checkPlain($nvp_response['SHIPTOZIP']); $order->delivery_country = $nvp_response['SHIPTOCOUNTRYCODE']; $order->billing_first_name = SafeMarkup::checkPlain($nvp_response['FIRSTNAME']); $order->billing_last_name = SafeMarkup::checkPlain($nvp_response['LASTNAME']); $order->billing_street1 = SafeMarkup::checkPlain($nvp_response['EMAIL']); if (!$order->getEmail()) { $order->setEmail($nvp_response['EMAIL']); } $order->setPaymentMethodId('paypal_ec'); $order->save(); $details[$order->id()] = TRUE; $session->set('have_details', $details); } $build['instructions'] = array('#markup' => $this->t("Your order is almost complete! Please fill in the following details and click 'Continue checkout' to finalize the purchase.")); $build['form'] = $this->formBuilder()->getForm('\\Drupal\\uc_paypal\\Form\\ecReviewForm', $order); return $build; }
public function wppCharge($order_id, $amount, $data) { $order = Order::load($order_id); $paypal_config = $this->config('uc_paypal.settings'); if ($data['txn_type'] == UC_CREDIT_PRIOR_AUTH_CAPTURE) { $nvp_request = array('METHOD' => 'DoCapture', 'AUTHORIZATIONID' => $data['auth_id'], 'AMT' => uc_currency_format($amount, FALSE, FALSE, '.'), 'CURRENCYCODE' => $order->getCurrency(), 'COMPLETETYPE' => 'Complete'); } else { list($desc, $subtotal) = _uc_paypal_product_details($order->products); if (intval($order->payment_details['cc_exp_month']) < 10) { $expdate = '0' . $order->payment_details['cc_exp_month'] . $order->payment_details['cc_exp_year']; } else { $expdate = $order->payment_details['cc_exp_month'] . $order->payment_details['cc_exp_year']; } $cc_type = NULL; if (isset($order->payment_details['cc_type'])) { switch (strtolower($order->payment_details['cc_type'])) { case 'amex': case 'american express': $cc_type = 'Amex'; break; case 'visa': $cc_type = 'Visa'; break; case 'mastercard': case 'master card': $cc_type = 'MasterCard'; break; case 'discover': $cc_type = 'Discover'; break; } } if (is_null($cc_type)) { $cc_type = $this->cardType($order->payment_details['cc_number']); if ($cc_type === FALSE) { drupal_set_message(t('The credit card type did not pass validation.'), 'error'); \Drupal::logger('uc_paypal')->error('Could not figure out cc type: @number / @type', ['@number' => $order->payment_details['cc_number'], '@type' => $order->payment_details['cc_type']]); return array('success' => FALSE); } } // PayPal doesn't accept IPv6 addresses. $ip_address = ltrim(\Drupal::request()->getClientIp(), '::ffff:'); $nvp_request = array('METHOD' => 'DoDirectPayment', 'PAYMENTACTION' => $data['txn_type'] == UC_CREDIT_AUTH_ONLY ? 'Authorization' : 'Sale', 'IPADDRESS' => $ip_address, 'AMT' => uc_currency_format($amount, FALSE, FALSE, '.'), 'CREDITCARDTYPE' => $cc_type, 'ACCT' => $order->payment_details['cc_number'], 'EXPDATE' => $expdate, 'CVV2' => $order->payment_details['cc_cvv'], 'FIRSTNAME' => substr($order->billing_first_name, 0, 25), 'LASTNAME' => substr($order->billing_last_name, 0, 25), 'STREET' => substr($order->billing_street1, 0, 100), 'STREET2' => substr($order->billing_street2, 0, 100), 'CITY' => substr($order->billing_city, 0, 40), 'STATE' => $order->billing_zone, 'ZIP' => $order->billing_postal_code, 'COUNTRYCODE' => $order->billing_country, 'CURRENCYCODE' => $order->getCurrency(), 'DESC' => substr($desc, 0, 127), 'INVNUM' => $order_id . '-' . REQUEST_TIME, 'BUTTONSOURCE' => 'Ubercart_ShoppingCart_DP_US', 'NOTIFYURL' => Url::fromRoute('uc_paypal.ipn', [], ['absolute' => TRUE])->toString(), 'EMAIL' => substr($order->getEmail(), 0, 127), 'PHONENUM' => substr($order->billing_phone, 0, 20)); if ($order->isShippable() && !empty($order->delivery_first_name)) { $shipdata = array('SHIPTONAME' => substr($order->delivery_first_name . ' ' . $order->delivery_last_name, 0, 25), 'SHIPTOSTREET' => substr($order->delivery_street1, 0, 100), 'SHIPTOSTREET2' => substr($order->delivery_street2, 0, 100), 'SHIPTOCITY' => substr($order->delivery_city, 0, 40), 'SHIPTOSTATE' => $order->delivery_zone, 'SHIPTOZIP' => $order->delivery_postal_code, 'SHIPTOCOUNTRYCODE' => $order->delivery_country); $nvp_request += $shipdata; } if ($paypal_config->get('uc_credit_cvv_enabled')) { $nvp_request['CVV2'] = $order->payment_details['cc_cvv']; } } $nvp_response = uc_paypal_api_request($nvp_request, $paypal_config->get('wpp_server')); $types = uc_credit_transaction_types(); switch ($nvp_response['ACK']) { case 'SuccessWithWarning': \Drupal::logger('uc_paypal')->warning('<b>@type succeeded with a warning.</b>@paypal_message', array('@paypal_message' => $this->buildErrorMessages($nvp_response), '@type' => $types[$data['txn_type']], 'link' => Link::createFromRoute(t('view order'), 'entity.uc_order.canonical', ['uc_order' => $order_id])->toString())); // Fall through. // Fall through. case 'Success': $message = t('<b>@type</b><br /><b>Success: </b>@amount @currency', ['@type' => $types[$data['txn_type']], '@amount' => uc_currency_format($nvp_response['AMT'], FALSE), '@currency' => $nvp_response['CURRENCYCODE']]); if ($data['txn_type'] != UC_CREDIT_PRIOR_AUTH_CAPTURE) { $message .= '<br />' . t('<b>Address:</b> @avscode', ['@avscode' => $this->avscodeMessage($nvp_response['AVSCODE'])]); if ($paypal_config->get('uc_credit_cvv_enabled')) { $message .= '<br />' . t('<b>CVV2:</b> @cvvmatch', ['@cvvmatch' => $this->cvvmatchMessage($nvp_response['CVV2MATCH'])]); } } $result = array('success' => TRUE, 'comment' => t('PayPal transaction ID: @transactionid', ['@transactionid' => $nvp_response['TRANSACTIONID']]), 'message' => $message, 'data' => SafeMarkup::checkPlain($nvp_response['TRANSACTIONID']), 'uid' => $this->currentUser()->id()); // If this was an authorization only transaction... if ($data['txn_type'] == UC_CREDIT_AUTH_ONLY) { // Log the authorization to the order. uc_credit_log_authorization($order_id, $nvp_response['TRANSACTIONID'], $nvp_response['AMT']); } elseif ($data['txn_type'] == UC_CREDIT_PRIOR_AUTH_CAPTURE) { uc_credit_log_prior_auth_capture($order_id, $data['auth_id']); } // Log the IPN to the database. db_insert('uc_payment_paypal_ipn')->fields(array('order_id' => $order->id(), 'txn_id' => $nvp_response['TRANSACTIONID'], 'txn_type' => 'web_accept', 'mc_gross' => $amount, 'status' => 'Completed', 'payer_email' => $order->getEmail(), 'received' => REQUEST_TIME))->execute(); break; case 'FailureWithWarning': // Fall through. // Fall through. case 'Failure': $message = t('<b>@type failed.</b>', ['@type' => $types[$data['txn_type']]]) . $this->buildErrorMessages($nvp_response); $result = array('success' => FALSE, 'message' => $message, 'uid' => $this->currentUser()->id()); break; default: $message = t('Unexpected acknowledgement status: @status', ['@status' => $nvp_response['ACK']]); $result = array('success' => NULL, 'message' => $message, 'uid' => $this->currentUser()->id()); break; } uc_order_comment_save($order_id, $this->currentUser()->id(), $message, 'admin'); // Don't log this as a payment money wasn't actually captured. if (in_array($data['txn_type'], array(UC_CREDIT_AUTH_ONLY))) { $result['log_payment'] = FALSE; } return $result; }