public function subscription($plan = false, $payment_interval = false, $payment = false) { if (!$this->client_id) { throw new LaraMillException('The user has to be connected to a Paymill client to make a payment.', 401); } $subscription = new \Paymill\Models\Request\Subscription(); $subscription->setClient($this->client_id); if ($plan and $payment_interval) { // Get offer id $offers = \Config::get('billing::offers'); if (!$offers or (!isset($offers[$plan]) or !isset($offers[$plan][$payment_interval]))) { throw new LaraMillException('No offers found.', 412); } $offer = $offers[$plan][$payment_interval]; $subscription->setOffer($offer); } // Get payment if ($payment) { $payment = $this->payment($payment)->details(); } else { $payments = $this->payment()->all(); $payment = $payments[count($payments) - 1]; } $subscription->setPayment($payment->getId()); $this->currentAction = 'subscription'; return new PaymillGateway($this, $subscription); }
if (isset($_POST['paymillToken'])) { $service = new Paymill\Request(PAYMILL_API_KEY); $client = new Paymill\Models\Request\Client(); $payment = new Paymill\Models\Request\Payment(); $offer = new Paymill\Models\Request\Offer(); $subscription = new Paymill\Models\Request\Subscription(); try { $client->setEmail(CUSTOMER_EMAIL); $client->setDescription('This is a Testuser.'); $clientResponse = $service->create($client); $payment->setClient($clientResponse->getId()); $payment->setToken($_POST['paymillToken']); $paymentResponse = $service->create($payment); $offer->setAmount($_POST['amount'])->setCurrency($_POST['currency'])->setInterval($_POST['interval'])->setName($_POST['offer-name']); $offerResponse = $service->create($offer); $subscription->setClient($clientResponse->getId()); $subscription->setPayment($paymentResponse->getId()); $subscription->setOffer($offerResponse->getId()); $subscriptionResponse = $service->create($subscription); $title = "<h1>We appreciate your order!</h1>"; $result = print_r($subscriptionResponse, true); } catch (\Paymill\Services\PaymillException $e) { $title = "<h1>An error has occoured!</h1>"; $result = print_r($e->getResponseCode(), true) . " <br />" . print_r($e->getResponseCode(), true) . " <br />" . print_r($e->getErrorMessage(), true); } } ?> </head> <body> <div> <?php
/** * @param $token * @param $amount * @param $subscriptionName * @param $clientId * @param string $currency * @param string $interval * @param null $paymentId * * @return bool|SubscriptionPaymentResult */ public function createSubscription($token, $amount, $subscriptionName, $clientId, $currency = 'EUR', $paymentId = null, $interval = '1 month', $trial = '1 week') { if (!$paymentId) { $payment = new \Paymill\Models\Request\Payment(); $payment->setToken($token)->setClient($clientId); try { $response = $this->request->create($payment); } catch (\Paymill\Services\PaymillException $e) { s($e->getResponseCode()); s($e->getStatusCode()); s($e->getErrorMessage()); return false; } $paymentId = $response->getId(); } $subscription = new \Paymill\Models\Request\Subscription(); $subscription->setClient($clientId); $subscription->setAmount($amount * 100); $subscription->setCurrency($currency); $subscription->setInterval($interval); $subscription->setPayment($paymentId); $subscription->setName($subscriptionName); $subscription->setPeriodOfValidity('10 YEAR'); // $subscription->setStartAt(strtotime(gmdate('Y-m-d 00:00:00', strtotime('+1 day')))); $subscription->setStartAt(time() + 3600); // $subscription->setStartAt(time() + strtotime($trial)); // trial mode try { /** @var Subscription $response */ $response = $this->request->create($subscription); } catch (\Paymill\Services\PaymillException $e) { s($e->getResponseCode()); s($e->getStatusCode()); s($e->getErrorMessage()); return false; } return new SubscriptionPaymentResult(['subscription_id' => $response->getId(), 'payment_id' => $response->getPayment()->getId()]); }