Skip to content

Instantly share code, notes, and snippets.

@pedrorocha-net
Created February 4, 2013 17:23
Show Gist options
  • Save pedrorocha-net/4708132 to your computer and use it in GitHub Desktop.
Save pedrorocha-net/4708132 to your computer and use it in GitHub Desktop.
Exemplo de Integração do módulo de Drupal http://drupal.org/project/pagseguro, para integrar ao PagSeguro puramente, sem estar dentro de um ecommerce
pagseguro_library_load();
$PagSeguroAPI = new PagSeguroAPI();
$payment_request = $PagSeguroAPI->createPaymentRequestObject();
$payment_request->setCurrency("BRL");
$payment_request->setReference($order_reference);
$payment_request->setSender($customer_name, $order->mail);
$payment_request->setRedirectUrl("http://example.com/pagseguro/return");
// Shipping rate is handled by Drupal
$CODIGO_SEDEX = PagSeguroShippingType::getCodeByType('NOT_SPECIFIED');
$payment_request->setShippingType($CODIGO_SEDEX);
$payment_request->setShippingAddress(
$postal_code, $customer_street, NULL, NULL, $customer_district
, $customer_city, $customer_region, 'BRA'
);
foreach ($order->commerce_line_items[LANGUAGE_NONE] as $key => $value) {
$line_item_ids[] = $value['line_item_id'];
}
$line_items = commerce_line_item_load_multiple($line_item_ids);
if ($settings['individual_items']) {
$index = 1;
foreach ($line_items as $line_item) {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
if ($line_item->type == 'shipping') {
$line_item_id = $line_item_wrapper->commerce_shipping_service->value();
$product_title = commerce_line_item_title($line_item); // TRUC specific
}
else {
$line_item_id = $line_item_wrapper->commerce_product->product_id->value();
if ($line_item->type == 'moldura_galeria') {
$product_title = $line_item_wrapper->field_imagem_da_galeria->title->value() . ' - ' . $line_item_wrapper->commerce_product->title->value(); // TRUC specific
}
else {
$product_title = commerce_line_item_title($line_item); // TRUC specific
}
}
$unit_price = commerce_price_wrapper_value($line_item_wrapper, 'commerce_unit_price', TRUE);
if (!empty($unit_price['amount'])) {
$payment_request->addItem(
$line_item_id
, $product_title
, (int) $line_item->quantity
// PagSeguro uses a number format different than Drupal, that's why the
// amount is divided by 100
, number_format(($unit_price['amount'] / 100), 2)
);
}
$index++;
}
unset($index);
}
else {
// Send everything as a single item.
$payment_request->addItem(
$order->order_number
, t('Order @order_id', array('@order_id' => $order->order_number))
, 1
, number_format(($order_wrapper->commerce_order_total->amount->value() / 100), 2)
);
}
$request_url = $PagSeguroAPI->registerPaymentRequest($payment_request);
drupal_goto($request_url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment