phppaymill

How to assign payment details and an offer to a client in Paymill with PHP?


I'm creating a client and an offer in Paymill but I don't know how to add payment details to the client, and then assign the offer to the client. Does anyone know how I can do this?

Here is my code that creates both the client and the offer:

$params = array(
    'amount'   => '3000',       // E.g. "4200" for 42.00 EUR
    'currency' => 'GBP',        // ISO 4217 
    'interval' => '1 MONTH',    // Options: "# DAY", "# WEEK", "# MONTH" and "# YEAR"
    'name'     => 'Tier 1'
);

$apiKey        = '111111111111111111111';
$apiEndpoint   = 'https://api.paymill.com/v2/';
$offersObject  = new Services_Paymill_Offers($apiKey, $apiEndpoint);
$offer         = $offersObject->create($params);

$email         = $_POST['email'];
$description   = "Tier 1";
$clientsObject = new Services_Paymill_Clients($apiKey, $apiEndpoint);
$client        = $clientsObject->create(array(
    'email'       => $email,
    'description' => $description
    )); 

print_r($clientsObject);

echo "tier 1 success<br/><br/>";

print_r($offersObject);

I'm finding it difficult to grasp from the documentation and haven't managed to find a tutorial yet- Any help would be amazing! Thanks, Joe


Solution

  • You have to create a client, payment and an offer. After that, create a subscription with the created client, payment and offer.

    The following code will solve this issue:

    $apiKey = '111111111111111111111';
    $apiEndpoint = 'https://api.paymill.de/v2/';
    
    $clientsObject = new Services_Paymill_Clients($apiKey, $apiEndpoint);
    $clientData = array(
        'email' => $_POST['email'],
        'description' => 'Tier 1'
    );
    $client = $clientsObject->create($clientData);
    
    $paymentObject = new Services_Paymill_Payments($apiKey, $apiEndpoint);
    $paymentData = array(
        'token' => '098f6bcd4621d373cade4e832627b4f6',      //general test-token
        'client' => $client['id']
    );
    $payment = $paymentObject->create($paymentData);
    
    $offersObject = new Services_Paymill_Offers($apiKey, $apiEndpoint);
    $offersData = array(
        'amount'   => '3000',       // E.g. "4200" for 42.00 EUR
        'currency' => 'GBP',        // ISO 4217 
        'interval' => '1 MONTH',    // Options: "# DAY", "# WEEK", "# MONTH" and "# YEAR"
        'name'     => 'Tier 1'
    );
    $offer = $offersObject->create($offersData);
    
    $subscriptionObject = new Services_Paymill_Subscriptions($apiKey, $apiEndpoint);
    $subscriptionData = array(
        'client' => $client['id'],
        'offer' => $offer['id'],
        'payment' => $payment['id']
    );
    $subscription = $subscriptionObject->create($subscriptionData);
    

    Best regards

    Ringo

    Paymill Developer