1

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?

Alosyius
  • 8,771
  • 26
  • 76
  • 120

2 Answers2

3

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().

  • But I still don't get something very important -- how do we get the Paymill token for a *subscription*? I mean, the customer agrees to a subscription and he/she does one immediate payment (which is not a subscription fee per se). The customers want to get automatic extension of every month from there on. So then what? How do you get a Paymill token, 1 month later just like that? How exactly? I seriously cannot understand it. Could you please help me with a workflow? EDIT: btw, the only thing I could find is to use the first Payment object in the Subscription. Will that work? – dimitarvp Nov 22 '13 at 11:39
  • @dimitko: What you're missing here is that for every subscription there is an offer. An offer is like a plan which defines the amount and interval. A subscription is just the connection between a client, an offer and a so called "payment object". So, after adding the subscription, it is executed according to the defined interval of the offer. – Matthias Dietrich Nov 22 '13 at 15:34
  • Well, I can read the API, but I am still little messed up. Again, a workflow point by point will be extremely useful, if you don't mind providing that. =) – dimitarvp Nov 22 '13 at 18:38
  • @dimitko: I try to make it short here, space in the comments is limited :). If this isn't enough and you have more specific questions, please open a new topic or contact support@paymill.com. You can refer to me so the ticket will be forwarded to me. /// 1. Create an offer via the [Merchant Center](https://app.paymill.com) or the API, 2. add a client, 3. add a payment object to the client (credit card or direct debit), 4. create a subscription by submitting the offer and payment object. – Matthias Dietrich Nov 25 '13 at 09:47
  • I am looking at this too and v2.1 API now allows the creation of a subscription without the pre-creation of a plan, so arbitrary subscription amounts can be created on the fly. The workflow seems to be (I am about to test it): 1) Create Payment object (not same as transaction) 2) Create Client (if client is new) 2) Make Subscription with Client and Payment as parameters. – Sentinel Jan 18 '15 at 13:50
0

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);
Sentinel
  • 3,582
  • 1
  • 30
  • 44