Reputation: 253
Steps I followed to integrate paypal.
https://devblog.paypal.com/working-with-the-new-android-sdk/
for integration.Code:
public class MainActivity extends Activity {
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
private static final String CONFIG_CLIENT_ID = "***************************";
private static final String CONFIG_RECEIVER_EMAIL = "[email protected]";
private static final int REQUEST_CODE_PAYMENT = 1;
private static final String TAG = "paymentExample";
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
.merchantName("Store");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onBuyPressed(View pressed) {
PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(MainActivity.this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_CODE_PAYMENT);
}
private PayPalPayment getThingToBuy(String paymentIntent) {
return new PayPalPayment(new BigDecimal("100.75"), "USD", "my product",
paymentIntent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm =
data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.i(TAG, confirm.toJSONObject().toString(4));
Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));
JSONObject jsonObj=new JSONObject(confirm.toJSONObject().toString());
String paymentId=jsonObj.getJSONObject("response").getString("id");
System.out.println("payment id:-=="+paymentId);
Toast.makeText(
getApplicationContext(),
"payment id"+paymentId, Toast.LENGTH_LONG)
.show();
Toast.makeText(
getApplicationContext(),
"PaymentConfirmation info received from PayPal", Toast.LENGTH_LONG)
.show();
} catch (JSONException e) {
Log.e(TAG, "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i(TAG, "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i(
TAG,
"An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
}
}
@Override
public void onDestroy() {
// Stop service when done
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
}
Response:
01-20 01:14:43.072: I/paymentExample(17759): {
01-20 01:14:43.072: I/paymentExample(17759): "response": {
01-20 01:14:43.072: I/paymentExample(17759): "state": "approved",
01-20 01:14:43.072: I/paymentExample(17759): "id": "PAY-6PU626847BSKPEWXHY",
01-20 01:14:43.072: I/paymentExample(17759): "create_time": "2014-07-18T18:46:55Z",
01-20 01:14:43.072: I/paymentExample(17759): "intent": "sale"
01-20 01:14:43.072: I/paymentExample(17759): },
01-20 01:14:43.072: I/paymentExample(17759): "client": {
01-20 01:14:43.072: I/paymentExample(17759): "platform": "Android",
01-20 01:14:43.072: I/paymentExample(17759): "paypal_sdk_version": "2.8.4",
01-20 01:14:43.072: I/paymentExample(17759): "product_name": "PayPal-Android-SDK",
01-20 01:14:43.072: I/paymentExample(17759): "environment": "mock"
01-20 01:14:43.072: I/paymentExample(17759): },
01-20 01:14:43.072: I/paymentExample(17759): "response_type": "payment"
01-20 01:14:43.072: I/paymentExample(17759): }
01-20 01:14:43.073: I/paymentExample(17759): {
01-20 01:14:43.073: I/paymentExample(17759): "short_description": "YRYCN",
01-20 01:14:43.073: I/paymentExample(17759): "amount": "100.75",
01-20 01:14:43.073: I/paymentExample(17759): "intent": "sale",
01-20 01:14:43.073: I/paymentExample(17759): "currency_code": "USD"
01-20 01:14:43.073: I/paymentExample(17759): }
My questions:
From your server, use the payment id value to look up the payment details with the REST API.
?Upvotes: 1
Views: 372
Reputation: 253
Found the answer and had done very small mistake.
[this is for without communicating to PayPal's servers ]
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_NO_NETWORK
Change to
[this is for to your test credentials from https://developer.paypal.com ]
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX
or Change to
[this is to move real money]
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_PRODUCTION
** not sure on rest two points
Upvotes: 1