/** * Handle amazon payment import request. * * @return The url to display on completion. */ function command_amazon_payment_import() { if (!user_access('payment_edit')) { error_register('User does not have permission: payment_edit'); return crm_url('payments'); } if (!array_key_exists('payment-file', $_FILES)) { error_register('No payment file uploaded'); return crm_url('payments&tab=import'); } $csv = file_get_contents($_FILES['payment-file']['tmp_name']); $data = csv_parse($csv); $count = 0; message_register("Processing " . count($data) . " row(s)"); foreach ($data as $row) { // Ignore withdrawals, holds, and failures if (strtolower($row['Type']) !== 'payment') { message_register("Ignoring row of type: " . $row['Type']); continue; } if (strtolower($row['To/From']) !== 'from') { message_register("Ignoring outgoing payment"); continue; } if (strtolower($row['Status']) !== 'completed') { message_register("Ignoring payment with status: " . $row['Status']); continue; } // Skip transactions that have already been imported $payment_opts = array('filter' => array('confirmation' => $row['Transaction ID'])); $data = payment_data($payment_opts); if (count($data) > 0) { message_register("Skipping previously imported payment: " . $row['Transaction ID']); continue; } // Parse value $value = payment_parse_currency($row['Amount']); // Create payment object $payment = array('date' => date('Y-m-d', strtotime($row['Date'])), 'code' => $value['code'], 'value' => $value['value'], 'description' => $row['Name'] . ' Amazon Payment', 'method' => 'amazon', 'confirmation' => $row['Transaction ID'], 'notes' => $row['notes'], 'amazon_name' => $row['Name']); // Check if the amazon name is linked to a contact $opts = array('filter' => array('amazon_name' => $row['Name'])); $contact_data = amazon_payment_contact_data($opts); if (count($contact_data) > 0) { $payment['credit_cid'] = $contact_data[0]['cid']; } // Save the payment $payment = payment_save($payment); $count++; } message_register("Successfully imported {$count} payment(s)"); return crm_url('payments'); }
// Check for success if (strpos($result, '<VerifySignatureResult><VerificationStatus>Success</VerificationStatus></VerifySignatureResult>') === false) { die; } // Check if the payment already exists // Skip transactions that have already been imported $payment_opts = array('filter' => array('confirmation' => $_POST['transactionId'])); $data = crm_get_data('payment', $payment_opts); if (count($data) > 0) { die; } // Parse the data and insert into the database // 'USD 12.34' goes to ['USD', '1234'] $parts = explode(' ', $_POST['transactionAmount']); file_put_contents($debug, print_r($parts, true) . "\n", FILE_APPEND); $payment_amount = payment_parse_currency($parts[1], $parts[0]); // Determine cid $cid = $_POST['referenceId']; if (empty($cid)) { // Check if the amazon name is linked to a contact $opts = array('filter' => array('amazon_name' => $_POST['buyerName'])); $contact_data = amazon_payment_contact_data($opts); if (count($contact_data) > 0) { $cid = $contact_data[0]['cid']; } } $payment = array('date' => date('Y-m-d', $_POST['transactionDate']), 'credit_cid' => $cid, 'code' => $payment_amount['code'], 'value' => (string) $payment_amount['value'], 'description' => $_POST['paymentReason'], 'method' => 'amazon', 'confirmation' => $_POST['transactionId'], 'amazon_name' => $_POST['buyerName']); $payment = payment_save($payment); // Log out $_SESSION['userId'] = 0; session_destroy();