/** * @Route ("/subscription/create") * @Secure ({"USER"}) * * @param array $params * @param ViewModel $model * @return string * @throws \Exception */ public function subscriptionCreate(array $params, ViewModel $model) { FilterParams::required($params, 'subscription'); $userService = UserService::instance(); $subscriptionsService = SubscriptionsService::instance(); $payPalApiService = PayPalApiService::instance(); $log = Application::instance()->getLogger(); $userId = Session::getCredentials()->getUserId(); $subscriptionType = $subscriptionsService->getSubscriptionType($params['subscription']); $recurring = isset($params['renew']) && $params['renew'] == '1'; $giftReceiverUsername = isset($params['gift']) && !empty($params['gift']) ? $params['gift'] : null; $giftReceiver = null; if (isset($params['sub-message']) and !empty($params['sub-message'])) { Session::set('subMessage', mb_substr($params['sub-message'], 0, 250)); } try { if (!empty($giftReceiverUsername)) { $giftReceiver = $userService->getUserByUsername($giftReceiverUsername); if (empty($giftReceiver)) { throw new Exception('Invalid giftee (user not found)'); } if ($userId == $giftReceiver['userId']) { throw new Exception('Invalid giftee (cannot gift yourself)'); } if (!$subscriptionsService->getCanUserReceiveGift($userId, $giftReceiver['userId'])) { throw new Exception('Invalid giftee (user does not accept gifts)'); } } } catch (Exception $e) { $model->title = 'Subscription Error'; $model->subscription = null; $model->error = $e; return 'order/ordererror'; } $conn = Application::instance()->getConnection(); $conn->beginTransaction(); try { // Create the NEW subscription $start = Date::getDateTime(); $end = Date::getDateTime(); $end->modify('+' . $subscriptionType['billingFrequency'] . ' ' . strtolower($subscriptionType['billingPeriod'])); $subscription = array('userId' => $userId, 'subscriptionSource' => Config::$a['subscriptionType'], 'subscriptionType' => $subscriptionType['id'], 'subscriptionTier' => $subscriptionType['tier'], 'createdDate' => $start->format('Y-m-d H:i:s'), 'endDate' => $end->format('Y-m-d H:i:s'), 'recurring' => $recurring ? 1 : 0, 'status' => SubscriptionStatus::_NEW); // If this is a gift, change the user and the gifter if (!empty($giftReceiver)) { $subscription['userId'] = $giftReceiver['userId']; $subscription['gifter'] = $userId; } // Insert subscription $subscriptionId = $subscriptionsService->addSubscription($subscription); // Send request to paypal $returnUrl = Http::getBaseUrl() . '/subscription/process?success=true&subscriptionId=' . urlencode($subscriptionId); $cancelUrl = Http::getBaseUrl() . '/subscription/process?success=false&subscriptionId=' . urlencode($subscriptionId); $token = $payPalApiService->createECResponse($returnUrl, $cancelUrl, $subscriptionType, $recurring); if (empty($token)) { throw new Exception("Error getting paypal response"); } // Commit transaction and continue to paypal. $conn->commit(); return 'redirect: ' . Config::$a['paypal']['api']['endpoint'] . urlencode($token); } catch (\Exception $e) { $log->critical("Error creating order"); $conn->rollBack(); throw $e; } }
/** * Execute the setExpressCheckout process, forwards to paypal * * @param array $order * @param array $subscription * @param bool $recurring */ public function createECResponse($responseUrl, array $order, array $subscriptionType, $recurring = false) { // @todo should pass these urls in $returnUrl = Http::getBaseUrl() . $responseUrl . '?success=true&orderId=' . urlencode($order['orderId']); $cancelUrl = Http::getBaseUrl() . $responseUrl . '?success=false&orderId=' . urlencode($order['orderId']); $setECReqDetails = new SetExpressCheckoutRequestDetailsType(); $setECReqDetails->ReqConfirmShipping = 0; $setECReqDetails->NoShipping = 1; $setECReqDetails->AllowNote = 0; $setECReqDetails->ReturnURL = $returnUrl; $setECReqDetails->CancelURL = $cancelUrl; $setECReqDetails->SolutionType = 'Sole'; if ($recurring) { // Create billing agreement for recurring payment $billingAgreementDetails = new BillingAgreementDetailsType('RecurringPayments'); $billingAgreementDetails->BillingAgreementDescription = $subscriptionType['agreement']; $setECReqDetails->BillingAgreementDetails[0] = $billingAgreementDetails; } $paymentDetails = new PaymentDetailsType(); $paymentDetails->PaymentAction = 'Sale'; $paymentDetails->NotifyURL = Config::$a['paypal']['api']['ipn']; $paymentDetails->OrderTotal = new BasicAmountType($order['currency'], $order['amount']); $paymentDetails->ItemTotal = new BasicAmountType($order['currency'], $order['amount']); $paymentDetails->Recurring = 0; $itemDetails = new PaymentDetailsItemType(); $itemDetails->Name = $subscriptionType['itemLabel']; $itemDetails->Amount = new BasicAmountType($order['currency'], $order['amount']); $itemDetails->Quantity = 1; // TODO this should be 'Digital' but Paypal requires you to change your account to a digital good account, which is a las $itemDetails->ItemCategory = 'Physical'; $itemDetails->Number = $subscriptionType['id']; $paymentDetails->PaymentDetailsItem[0] = $itemDetails; $setECReqDetails->PaymentDetails[0] = $paymentDetails; // Paypal UI settings $setECReqDetails->BrandName = Config::$a['commerce']['reciever']['brandName']; // Execute checkout $setECReqType = new SetExpressCheckoutRequestType(); $setECReqType->SetExpressCheckoutRequestDetails = $setECReqDetails; $setECReq = new SetExpressCheckoutReq(); $setECReq->SetExpressCheckoutRequest = $setECReqType; $paypalService = new PayPalAPIInterfaceServiceService(); return $paypalService->SetExpressCheckout($setECReq); }
<?php use Destiny\Common\Utils\Http; use Destiny\Common\Config; $arr = array("og:site_name" => Config::$a['meta']['shortName'], "og:title" => Config::$a['meta']['title'], "og:description" => Config::$a['meta']['description'], "og:image" => Config::cdn() . '/web/img/destinygg.png', "og:url" => Http::getBaseUrl(), "og:type" => "video.other", "og:video" => Config::$a['meta']['video'], "og:video:secure_url" => Config::$a['meta']['videoSecureUrl'], "og:video:type" => "application/x-shockwave-flash", "og:video:height" => "260", "og:video:width" => "340"); foreach ($arr as $k => $v) { echo '<meta property="' . $k . '" content="' . $v . '" />' . PHP_EOL; }
/** * Handle the Response response * @param Response $response * @throws Exception * @return void */ private function handleResponse(Response $response) { $location = $response->getLocation(); if (!empty($location)) { Http::header(Http::HEADER_LOCATION, $location); exit; } $headers = $response->getHeaders(); foreach ($headers as $header) { Http::header($header[0], $header[1]); } Http::status($response->getStatus()); $body = $response->getBody(); if (!empty($body)) { echo $body; } exit; }