PHP code example of wearesho-team / cpa-integration

1. Go to this page and download the library: Download wearesho-team/cpa-integration library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wearesho-team / cpa-integration example snippets



use Wearesho\Cpa\Postback\PostbackService;
use Wearesho\Cpa\Repository\ConversionMemoryRepository;
use Wearesho\Cpa\Postback\PostbackServiceConfig;
use Wearesho\Cpa\Lead\LeadFactory;
use Wearesho\Cpa\Exceptions\DuplicatedConversionException;
use Wearesho\Cpa\Exceptions\UnsupportedConversionTypeException;

$servicesConfig = yaml_parse('your_config_file.yml'); // Or another config loader (array must be provided), see Configuration section
$repository = new ConversionMemoryRepository(); // Or use your implementation of interface
$client = new \GuzzleHttp\Client(); // Or another implementation of \GuzzleHttp\ClientInterface
$config = new PostbackServiceConfig($servicesConfig);
$service = new PostbackService(
    $repository,
    $client,
    $config
);

$leadFactory = new LeadFactory(); 
$lead = $leadFactory->fromUrl($_REQUEST['REQUEST_URI']); // Or parse it on each request and load from database on user action
$lead = $leadFactory->fromCookie($_COOKIE['CPA_PROVIDER']); // Or you can store lead between request and load it from cookie

$userOrActionId = 1;
$conversion = $lead->createConversion($userOrActionId);

try {
    $response = $service->send($conversion);   
}
catch(DuplicatedConversionException $duplicationException) {
    // If your code may generate few conversion with same id
}
catch(UnsupportedConversionTypeException $invalidConversion) {
    // If you did not configure conversion for current lead CPA network
}
catch(\GuzzleHttp\Exception\RequestException $connectionException) {
    // If CPA network url is unavailable or your config is not accepted
}


use Wearesho\Cpa\Lead\LeadFactory;

$cookieKey = "CPA_PROVIDER";

$factory = new LeadFactory();
$lead = $factory->fromUrl($_REQUEST['REQUEST_URI']); // Or use your handling request implementation
$cookie = $factory->toCookie($lead);
setcookie($cookieKey, $cookie); // Or use your library to handle cookies


function get_config() {
    // This function may load values from file (in your implementation)
    return [
        'SomeCpaNetwork' => false, // Put false (or just not add config) if you want to switch off postback to network
        
        'SalesDoubler' => [
            'baseUrl' => 'http://rdr.salesdoubler.com.ua/', // optional
            'token' => 'YourToken',
            'id' => 'YourId',        
        ],
        
        'PrimeLead' => [
            'baseUrl' => 'https://primeadv.go2cloud.org/', // optional
            'id' => 'YourId',
        ],
    ];
}