Search code examples
phpcybersource

Cybersource Flex Microform Recurring Payments


I'm working with integrating flex microform into an existing php application. I have been able to get individual transactions working, however from that point I am unable to use the token for future transactions as the documentation says should be possible. Is there any way to set up a recurring subscription using the microform such that I could then charge that again in the future?


Solution

  • The Flex Microform token is a temporary token that's only useful for a few minutes.

    You need to use that to create a permanent Payment Instrument and Customer token. You can then use this in future requests to charge the same credit card.

    Our code for making a purchase and getting a permanent token is similar to this:

    // Save CC
    // Based on Samples/Payments/Payments/PaymentWithFlexTokenCreatePermanentTMSToken.php
    
    $token_types = ['customer', 'paymentInstrument', 'billingAddress'];
    
    $processingInformationAuthorizationOptionsInitiator = new CyberSource\Model\Ptsv2paymentsidProcessingInformationAuthorizationOptionsInitiator([
        "credentialStoredOnFile" => true
    ]);
    $processingInformationAuthorizationOptions = new CyberSource\Model\Ptsv2paymentsidProcessingInformationAuthorizationOptions([
        "initiator" => $processingInformationAuthorizationOptionsInitiator,
        "ignoreAvsResult" => true
    ]);
    $processingInformation = new CyberSource\Model\Ptsv2paymentsProcessingInformation([
        'actionList' => ["TOKEN_CREATE"],
        'actionTokenTypes' => $token_types,
        'capture' => $amt != 0,
        "authorizationOptions" => $processingInformationAuthorizationOptions
    ]);
    $orderInformationAmountDetails = new CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails([
        'currency' => 'USD'
    ]);
    $orderInformationLineItems = [
        new CyberSource\Model\Ptsv2paymentsOrderInformationLineItems([
            'unitPrice' => round($amt / 100, 2),
            'taxAmount' => $tax,
            'amountIncludesTax' => false
        ])
    ];
    $orderInformationBillTo = new CyberSource\Model\Ptsv2paymentsOrderInformationBillTo(formvars_to_cybersource_billto());
    $orderInformation = new CyberSource\Model\Ptsv2paymentsOrderInformation([
        'amountDetails' => $orderInformationAmountDetails,
        'billTo' => $orderInformationBillTo,
        'lineItems' => $orderInformationLineItems
    ]);
    $buyerInformation = new CyberSource\Model\Tmsv2customersBuyerInformation([
        "merchantCustomerID" => $_SESSION['username'],
        "email" => formvar('i_email')
    ]);
    $tokenInformation = new CyberSource\Model\Ptsv2paymentsTokenInformation(['transientTokenJwt' => formvar('i_provider_token')]);
    $requestObj = new CyberSource\Model\CreatePaymentRequest([
        "clientReferenceInformation" => $clientReferenceInformation,
        "processingInformation" => $processingInformation,
        "orderInformation" => $orderInformation,
        "tokenInformation" => $tokenInformation,
        "buyerInformation" => $buyerInformation
    ]);
    $api_instance = new CyberSource\Api\PaymentsApi($api_client);
    $apiResponse = $api_instance->createPayment($requestObj);
    $customer_id = $apiResponse[0]['tokenInformation']['customer']['id'];
    

    $customer_id is the permanent customer token that you can save to use in automated purchases. Our code for that looks like:

    $request = new CyberSource\Model\CreatePaymentRequest([
        "paymentInformation" => new CyberSource\Model\Ptsv2paymentsPaymentInformation([
            "customer" => new CyberSource\Model\Ptsv2paymentsPaymentInformationCustomer([
                "id" => $customerId,
            ]),
        ]),
        "clientReferenceInformation" => new CyberSource\Model\Ptsv2paymentsClientReferenceInformation([
            "code" => sprintf('payment-%s', $requestId)
        ]),
        "orderInformation" => new CyberSource\Model\Ptsv2paymentsOrderInformation([
            "amountDetails" => new CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails([
                "currency" => "USD"
            ]),
            "lineItems" => [
                new CyberSource\Model\Ptsv2paymentsOrderInformationLineItems([
                    'unitPrice' => round($amount / 100, 2),
                    'taxAmount' => $taxAmount,
                    'amountIncludesTax' => false
                ])
            ],
        ]),
        "processingInformation" => new CyberSource\Model\Ptsv2paymentsProcessingInformation([
            "commerceIndicator" => "internet",
            "authorizationOptions" => new CyberSource\Model\Ptsv2paymentsProcessingInformationAuthorizationOptions([
                "initiator" => new CyberSource\Model\Ptsv2paymentsProcessingInformationAuthorizationOptionsInitiator([
                    "type" => "merchant",
                ])
            ]),
            "capture" => true,
        ])
    ]);
    
    $response = $instance->createPayment($request);