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

323 views Asked by At

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

2

There are 2 answers

1
Ringo On BEST ANSWER

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

0
cbraun75 On

About the question to Ringo's answer.

No you needn't to create everytime another token for a existing client with a first transaction made with this specific creditcard. The token is used only in the first transaction. After you have received the paymentobject_id in the response of the first transaction, you can use this to make repeated/recurring transactions without creating another token for this paymentobject of this client. BUT if the client want to pay with another e.g. second creditcard which you won't have a paymentobject for right now. Than you need to create like before for the first transaction, a token for this paymentobject and get than after the first transaction another paymentobject_id

Best, Christian