Alosyius
Alosyius

Reputation: 9121

Paymill subscription in PHP

I am trying to create a subscription i Paymill. Ive read through their examples but i do not seem to get it.

All i really want to do is to setup a subscription, here is my current code:

$token = $_POST['paymillToken'];

if ($token) {
    require "Services/Paymill/Payments.php";
    require "Services/Paymill/Transactions.php";
    require "Services/Paymill/Subscriptions.php";
    require "Services/Paymill/Offers.php";


  $params = array(
    'amount'      => '49900',  // Cent!
    'currency'    => 'SEK',   // ISO 4217
    'token'       => $token,
    'description' => 'User ID# ' . $userIdMain . ' Email: ' . $userEmail
  );

$transactionsObject = new Services_Paymill_Transactions(
    PAYMILL_API_KEY, PAYMILL_API_HOST
);
$transaction        = $transactionsObject->create($params);

echo "<br /><br />";    
print_r($transaction);

echo $transaction['client']['id'];
echo $transaction['payment']['id'];


$params = array(
    'client'   => $transaction['client']['id'],
    'offer'    => 'offer_9cdffb501f565bf827a8',
    'payment'  => $transaction['payment']['id']
);
$subscriptionsObject = new Services_Paymill_Subscriptions(PAYMILL_API_KEY, PAYMILL_API_HOST);
$subscription        = $subscriptionsObject->create($params);

echo "<br /><br />";    
print_r($subscription);

} 

The problem is that the above create two payments at once. But it seems like the subscription object requires me to first have a payment_id (see $transaction['payment']['id'] above).

What am i doing wrong here?

Upvotes: 1

Views: 1843

Answers (2)

Sentinel
Sentinel

Reputation: 3697

OK it's not PHP but I had the same kind of uncertainty following the right steps to set up a subscription. In the end it's kind of easy, so I am posting my code so far in case it helps.

The app is MVC .net and I am using the PayMill PayButton to generate a payment token. This is part of the model during the registration process. The following code calls the .net wrapper of the PayMill API.

   private static async Task<Subscription> MakePayMillSubscription(int amount, ApplicationUser user, PaymillContext paymillContext, Payment payment, Client client)
    {

        SubscriptionService subscriptionService = paymillContext.SubscriptionService;
        Subscription subscription = await subscriptionService.CreateAsync( payment, client, null, amount, "EUR", Interval.periodWithChargeDay(1, Interval.TypeUnit.MONTH), null, user.Id, Interval.period(10, Interval.TypeUnit.YEAR));
        return subscription;
    }

    private static async Task<Client> MakePayMillClient(RegisterViewModel model, ApplicationUser user, PaymillContext paymillContext)
    {

        ClientService clientService = paymillContext.ClientService;
        Client client = await clientService.CreateWithEmailAndDescriptionAsync(
            model.Email,
            user.Id
        );
        return client;
    }

    private static async Task<Payment> MakePayMillPayment(RegisterViewModel model,PaymillContext context, Client client)
    {

        Payment payment = await context.PaymentService.CreateWithTokenAndClientAsync(model.paymillToken, client);

        return payment;
    }

Then I call these during registration like this at the moment (I am still in a testing phase)

  client = await MakePayMillClient(model, user, paymillContext);
   payment = await MakePayMillPayment(model, paymillContext,client);
   subscription = await MakePayMillSubscription((int)(account.TotalAmountDue * 100.0M), user, paymillContext, payment, client);

Upvotes: 0

Matthias Dietrich
Matthias Dietrich

Reputation: 521

Creating a subscription will also create a transaction, which is correct. Add a Payment Object ( https://www.paymill.com/en-gb/documentation-3/reference/api-reference/#create-new-credit-card-payment-with ) instead of creating a new transaction and pass the id to the Subscription->create().

Upvotes: 3

Related Questions