php mollie payments recurring billing - php

My code is :
$customer = $mollie->customers->create([
"name" => $name,
"email" => $email,
]);
$customer->createSubscription([
"amount" => [
"currency" => 'USD',
"value" => 20.00,
],
"interval" => '2months',
"times" => 3,
"description" => $someDescription,
"webhookUrl" => $webhook,
"method" => NULL,
]);
$payment = $customer->createPayment([
"amount" => [
"currency" => 'USD',
"value" => 20.00,
],
"description" => $someDescription,
"redirectUrl" => $siteUrl,
"webhookUrl" => $webhook,
"metadata" => [
"order_id" => $orderId,
],
"sequenceType" => \Mollie\Api\Types\SequenceType::SEQUENCETYPE_FIRST,
]);
The result is:
Fatal error: Uncaught exception 'Mollie\Api\Exceptions\ApiException'
with message 'Error executing API call (422: Unprocessable Entity): No
suitable mandates found for customer. Field: customerId.
Is something that I missing??

You're missing the customer ID for the customer you created previously.
$payment = $customer->createPayment([
"customerId" => $customer->id, /* see #3 in documentation */
"amount" => [
"currency" => 'USD',
"value" => 20.00,
],
"description" => $someDescription,
"redirectUrl" => $siteUrl,
"webhookUrl" => $webhook,
"metadata" => [
"order_id" => $orderId,
],
"sequenceType" => \Mollie\Api\Types\SequenceType::SEQUENCETYPE_FIRST,
]);

I find an answer on my own question:
In order to add a subscription for an user you must first add the payment and after that the subscription.
$customer = $mollie->customers->create([
"name" => $fullName,
"email" => $email,
]);
$payment = $customer->createPayment([
"amount" => [
"currency" => $currency,
"value" => $amount,
],
"description" => $description,
"redirectUrl" => $siteUrl,
"webhookUrl" => $webhook,
"metadata" => [
"order_id" => $orderId,
],
"sequenceType" => \Mollie\Api\Types\SequenceType::SEQUENCETYPE_FIRST,
]);
$customer->createSubscription([
"amount" => [
"currency" => $currency,
"value" => $amount,
],
"times" => $recurringLimit,
"interval" => $interval,
"description" => $description,
"webhookUrl" => $webhook,
"method" => NULL,
]);

Related

Stripe checkout: description in dashboard instead of pi

Can anyone help me? I migrate form v2 to v3 checkout.
how can I send my custom description order in stripe dashboard description column?
now I get only the payment id pi_1IrhQALKfdoxxl3X07seJ5anto
with old API by description I would do:
$charge = \Stripe\Charge::create(array(
"amount" => $_POST['amount'],
"currency" => "EUR",
"description" => "Order #".$_POST["order"],
"source" => $token,
));
with the new API :
$stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_H5ggYwtDq4fbrJ',
'quantity' => 2,
],
],
'mode' => 'payment',
]);
Thank you
according to the stripe documentation : https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-description
$stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_H5ggYwtDq4fbrJ',
'quantity' => 2,
],
],
'mode' => 'payment',
'payment_intent_data' => [
'description' => "Order #".$_POST["order"]
]
]);

2CheckOut - Error : Bad Request - Parameter error

Request Data to TwoCheckOut :
Response from TwoCheckOut.
{
"error": "Bad request - parameter error"
}
Here is the Code
$charge = Twocheckout_Charge::auth(array(
"sellerId" => '*HIDDEN*',
"merchantOrderId" => $orderID,
"token" => $token,
"currency" => $currency,
"total" => $itemPrice,
"billingAddr" => array(
"name" => $name,
"addrLine1" => $addrLine1,
"city" => $city,
"state" => $state,
"zipCode" => $zipCode,
"country" => $country,
"email" => $email,
"phoneNumber" => $phoneNumber
),
"shippingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => 'USA',
"email" => 'testingtester#2co.com',
"phoneNumber" => '555-555-5555'
),
"demo" => true
));
Althought i am getting unauthenticated if i send wrong 'token' key.
EDIT :
MY VIEW JS CODE
Feel free to drop your suggestions
"demo" => 'Y'
Demo key should be 'Y' not true was the issue here
TCO.loadPubKey('production');
loadPubKey should be 'production' it should not be empty

Trying to add "idempotency_key" to stripe payment intent. Not showing up in the array after submitting to stripe. Is there something wrong?

The request is successfully sent, but when I check the content of the request, the indempotency value is not in the array, Do I have a syntax error or am I doing something wrong?
$idempotency_key = bin2hex(random_bytes(16));
$intent = \Stripe\PaymentIntent::create([
'amount' => $price,
'currency' => 'usd',
'customer' => ''.$_SESSION['uid'].'',
'payment_method' => ''.$payid.'',
'description' => ''.$title.'',
"metadata" => [
'task' => ''.$_POST['pid'].'',
'offer' => ''.$_POST['accept'].'',
'category' => ''.$category.'',
'location' => ''.$location.''
],
'off_session' => false,
'confirm' => true,
'confirmation_method' => 'automatic',
'use_stripe_sdk' => true,
], [
"idempotency_key" => ''.$idempotency_key.'',
]);

Payment api multidimensional array issue in php

My payment provider issued the following example code for their API:
$order = $mollie->orders->create([
"amount" => [
"value" => "1027.99",
"currency" => "EUR"
],
"billingAddress" => [
"streetAndNumber" => "Keizersgracht 313",
"postalCode" => "1016 EE",
"city" => "Amsterdam",
"country" => "nl",
"givenName" => "Luke",
"familyName" => "Skywalker",
"email" => "luke#skywalker.com",
],
"shippingAddress" => [
"streetAndNumber" => "Keizersgracht 313",
"postalCode" => "1016 EE",
"city" => "Amsterdam",
"country" => "nl",
"givenName" => "Luke",
"familyName" => "Skywalker",
"email" => "luke#skywalker.com",
],
"metadata" => [
"order_id" => $orderId
],
"consumerDateOfBirth" => "1958-01-31",
"locale" => "en_US",
"orderNumber" => strval($orderId),
"redirectUrl" => "{$protocol}://{$hostname}{$path}/orders/return.php?order_id={$orderId}",
"webhookUrl" => "{$protocol}://{$hostname}{$path}/orders/webhook.php",
"method" => "ideal",
"lines" => [
[
"sku" => "5702016116977",
"name" => "LEGO 42083 Bugatti Chiron",
"productUrl" => "https://shop.lego.com/nl-NL/Bugatti-Chiron-42083",
"imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image//LEGO/42083_alt1?$main$',
"quantity" => 2,
"vatRate" => "21.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "399.00"
],
"totalAmount" => [
"currency" => "EUR",
"value" => "698.00"
],
"discountAmount" => [
"currency" => "EUR",
"value" => "100.00"
],
"vatAmount" => [
"currency" => "EUR",
"value" => "121.14"
]
],
[
"type" => "digital",
"sku" => "5702015594028",
"name" => "LEGO 42056 Porsche 911 GT3 RS",
"productUrl" => "https://shop.lego.com/nl-NL/Porsche-911-GT3-RS-42056",
"imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image/LEGO/42056?$PDPDefault$',
"quantity" => 1,
"vatRate" => "21.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "329.99"
],
"totalAmount" => [
"currency" => "EUR",
"value" => "329.99"
],
"vatAmount" => [
"currency" => "EUR",
"value" => "57.27"
]
]
]
]);
What I'd like to do is to replace the hard coded second dimension "lines" with data from these session vars.
foreach ($_SESSION['cart_products'] as $cart_itm) {
$name = $cart_itm['cat_item_titel_' . $lang . ''];
$unitPrice = $cart_itm['cat_item_prijs'];
$sku = $cart_itm['cat_item_code'];
}
And this is the solution I came up with but this just doesn't work. The reason, I assume, is that the data in the $orders array is echoed as a string and not functional code as i should be. But I haven't got a clue how to aproach this issue.
$i = 0;
$lngth = count($_SESSION['cart_products']);
foreach ($_SESSION['cart_products'] as $cart_itm) {
$orders[] = '[';
$orders[] .= '"sku" => ' . $cart_itm['cat_item_code'] . ',';
$orders[] .= '"name" => ' . $cart_itm['cat_item_titel_en'] . ',';
$orders[] .= '"quantity" => 1,';
$orders[] .= '"vatRate" => "0.00",';
$orders[] .= '"unitPrice" => [
"currency" => "EUR",
"value" => "15.50"],';
$orders[] .= '"totalAmount" => [
"currency" => "EUR",
"value" => "15.50"],';
$orders[] .= '"vatAmount" => [
"currency" => "EUR",
"value" => "0.00"]';
if ($i == $lngth - 1) {
$orders[] .= ']';
}
else {
$orders[] .= '],';
}
$i++;
}
$echoOrders = implode($orders);
$order = $mollie->orders->create([
"amount" => [
"value" => "15.50",
"currency" => "EUR"
],
"billingAddress" => [
"streetAndNumber" => $address,
"postalCode" => $postalcode,
"city" => $city,
"country" => $country,
"givenName" => $firstname,
"familyName" => $name,
"email" => $email,
],
"shippingAddress" => [
"streetAndNumber" => $addressa,
"postalCode" => $postalcodea,
"city" => $citya,
"country" => $countrya,
"givenName" => $firstnamea,
"familyName" => $namea,
"email" => $email,
],
"metadata" => [
"order_id" => $orderId
],
"locale" => "nl_BE",
"orderNumber" => strval($orderId),
"redirectUrl" => "{$protocol}://{$hostname}{$path}/thankyou.php?order_id={$orderId}",
"webhookUrl" => "https://www.cluster-park.com/dev/includes/webhook.php",
"lines" => [ $echoOrders ]
]);
All suggestions are more than welcome.
Thanks!
You need to build an associative array, at the moment you are building a string. This should give you a starting point...
$lines = [];
foreach ($_SESSION['cart_products'] as $cart_itm) {
$lines[] = [ "sku" => $cart_itm['cat_item_code'],
"name" => $cart_itm['cat_item_titel_en'],
"quantity" => 1,
"vatRate" => "0.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "15.50"]
],
// Add all of the other data you have
];
}
then later in your code, you add them in using...
"lines" => $lines

Stripe - Create Customer and add to a plan with trial period

I'm able to create a customer and add it to a subscription plan; following code works:
\Stripe\Stripe::setApiKey("sk_test_tz7AKV73RPCd7p**********");
// create Customer
$customer = \Stripe\Customer::create(array(
"email" => $email,
"plan" => $plan_id,
"source" => $token,
"metadata" => array(
"nome" => $name,
"tel" => $tel,
"indirizzo" => $indirizzo,
"città" => $address_city,
"provincia" => $address_state,
"cap" => $address_zip
),
));
Then if I add a trial_period_days" => 30 option, I get the following error:
1 exception(s): Exception #0 (Stripe\Error\InvalidRequest): Received
unknown parameter: trial_period_days
I added it to the option hash as follow:
// create Customer
$customer = \Stripe\Customer::create(array(
"email" => $email,
"plan" => $plan_id,
"trial_period_days" => 30,
"source" => $token,
"metadata" => array(
"nome" => $name,
"tel" => $tel,
"indirizzo" => $indirizzo,
"città" => $address_city,
"provincia" => $address_state,
"cap" => $address_zip
),
));
OK, I got it; trial_period_days is an argument that should be used during Subscription creation, so I edited my code as follow:
// create Customer
$customer = \Stripe\Customer::create(array(
"email" => $email,
"source" => $token,
"metadata" => array(
"nome" => $name,
"tel" => $tel,
"indirizzo" => $indirizzo,
"città" => $address_city,
"provincia" => $address_state,
"cap" => $address_zip
),
));
// associate Customer to the Plan
\Stripe\Subscription::create(array(
"customer" => $customer,
"plan" => $plan_id,
"trial_period_days" => 30,
));

Categories